123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- import {
- configShare
- } from "@/api/home.js"
- export default {
- //处理图片地址
- imgUrls: (val) => {
- if (val) {
- return val.indexOf('http') === -1 ? 'https://guess-shop.oss-cn-beijing.aliyuncs.com' + val : val
- }
- },
- // 富文本处理图片显示
- richTextImg: (val) => {
- if (val) {
- return val.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;" ')
- }
- },
- // 高德地图坐标转腾讯地图坐标的转换函数
- gcj02tobd09: (lng, lat) => {
- var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
- var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_pi);
- var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_pi);
- var bd_lng = z * Math.cos(theta) + 0.0065;
- var bd_lat = z * Math.sin(theta) + 0.006;
- return [bd_lng, bd_lat];
- },
- // 距离显示格式处理
- distance: (val) => {
- if (val) {
- let distance = val / 1000
- if (distance < 1) {
- return Math.round(distance * 1000) + 'm'
- } else {
- return Math.round(distance * 100) / 100 + 'km'
- }
- }
- return ''
- },
- // 格式化日期为 YYYY-MM-DD
- formatDate: (date) => {
- const d = new Date(date);
- const year = d.getFullYear();
- const month = (d.getMonth() + 1).toString().padStart(2, '0');
- const day = d.getDate().toString().padStart(2, '0');
- return `${year}-${month}-${day}`;
- },
- // 格式化日期为 MM/DD
- formatDate1: (dateString) => {
- const date = new Date(dateString);
- const month = String(date.getMonth() + 1).padStart(2, '0');
- const day = String(date.getDate()).padStart(2, '0');
- return `${month}/${day}`;
- },
- /**
- * 格式化时间戳
- * @param {number} timestamp - 时间戳
- * @param {string} format - 格式类型,支持 'yyyy-MM-dd HH:mm', 'yyyy-MM-dd HH:mm:ss', 'yyyy-MM-dd'
- * @returns {string} 格式化后的日期字符串
- */
- mFormatDate(timestamp, format = 'yyyy-MM-dd HH:mm:ss') {
- if (!timestamp) return '';
- const date = new Date(timestamp);
- const year = date.getFullYear();
- const month = String(date.getMonth() + 1).padStart(2, '0');
- const day = String(date.getDate()).padStart(2, '0');
- const hours = String(date.getHours()).padStart(2, '0');
- const minutes = String(date.getMinutes()).padStart(2, '0');
- const seconds = String(date.getSeconds()).padStart(2, '0');
- switch (format) {
- case 'yyyy-MM-dd HH:mm':
- return `${year}-${month}-${day} ${hours}:${minutes}`;
- case 'yyyy-MM-dd':
- return `${year}-${month}-${day}`;
- case 'yyyy-MM-dd HH:mm:ss':
- default:
- return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
- }
- },
-
- //复制
- copyTxt(text) {
- uni.setClipboardData({
- data: text,
- success: () => {
- uni.showToast({
- title: '复制成功',
- icon: 'none'
- });
- },
- fail: (err) => {
- uni.showToast({
- title: '复制失败',
- icon: 'none'
- });
- console.error(err);
- }
- });
- },
- // 处理价格样式
- // priceStyle: (val) => {
- // if (val) {
- // let arr = val.split('.')
- // return '¥<span>' + arr[0] + '</span>' + arr[1]
- // }
- // return ''
- // }
- // skipWeb: (path, title) => {
- // if (path === "#") {
- // uni.showToast({
- // icon: 'none',
- // title: '敬请期待'
- // })
- // return false
- // }
- // uni.navigateTo({
- // url: `/pages/web-view/web?path=${encodeURIComponent(path)}&title=${title || ''}`,
- // complete: () => {
- // oldApps = null;
- // }
- // });
- // },
- openPage: (data = {}) => {
- switch (Number(data.jumpType)) {
- case 0:
- // 内部页面
- try {
- uni.navigateTo({
- url: data.innerPageCode,
- fail: err => {
- uni.switchTab({
- url: data.innerPageCode
- });
- }
- });
- } catch (err) {
- console.log('未配置页面')
- }
- break;
- case 1:
- // 外部链接
- // skipWeb(data.outsideAddress, data.name)
- if (data.outsideAddress === "#") {
- // uni.showToast({
- // icon: 'none',
- // title: '敬请期待'
- // })
- return false
- }
- uni.navigateTo({
- url: `/pages/web-view/web?path=${encodeURIComponent(data.outsideAddress)}&title=${data.name || ''}`,
- complete: () => {
- // oldApps = null;
- }
- });
- break;
- case 2:
- // 内部资源模型
- if (data.resourceModel == 1) {
- // 公告
- uni.navigateTo({
- url: `/pages/home/noticeDetails?noticeId=${data.resourceModelId}`
- })
- } else if (data.resourceModel == 2) {
- // 积分商品
- uni.navigateTo({
- url: `/pages/giftPackageCenter/goodsDetails?productId=${data.resourceModelId}`
- })
- } else if (data.resourceModel == 3) {
- // 商户
- uni.navigateTo({
- url: `/pages/home/scenicSpotDetails?businessId=${data.resourceModelId}`
- })
- } else {
- uni.showToast({
- icon: 'none',
- title: '敬请期待'
- })
- break;
- }
- }
- },
- pagination(total, currentPageNum, currentPageSize) {
- let totalPage = 0
- if (total > 0) {
- if (total / currentPageSize > parseInt(total / currentPageSize)) {
- totalPage = parseInt(total / currentPageSize) + 1
- } else {
- totalPage = parseInt(total / currentPageSize);
- }
- if (currentPageNum < totalPage) {
- return 'more'
- }
- }
- return 'noMore'
- },
- /**
- * @param {Function} func
- * @param {number} wait
- * @param {boolean} immediate
- * @return {*}
- */
- debounce(func, wait, immediate) {
- let timeout, args, context, timestamp, result
- const later = function() {
- // 据上一次触发时间间隔
- const last = +new Date() - timestamp
- // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
- if (last < wait && last > 0) {
- timeout = setTimeout(later, wait - last)
- } else {
- timeout = null
- // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
- if (!immediate) {
- result = func.apply(context, args)
- if (!timeout) context = args = null
- }
- }
- }
- return function(...args) {
- context = this
- timestamp = +new Date()
- const callNow = immediate && !timeout
- // 如果延时不存在,重新设定延时
- if (!timeout) timeout = setTimeout(later, wait)
- if (callNow) {
- result = func.apply(context, args)
- context = args = null
- }
- return result
- }
- },
- // // 获取元素高度
- // getElHeight (el) {
- // return new Promise(resolve => {
- // const query = uni.createSelectorQuery().in(this)
- // query.select(el).boundingClientRect(data => {
- // resolve(data.height)
- // }).exec()
- // })
- // },
- // 手机号脱敏
- desensitizePhoneNumber(phoneNumber) {
- if (typeof phoneNumber !== 'string' || phoneNumber.length !== 11) {
- throw new Error('请输入有效的11位手机号');
- }
- return phoneNumber.slice(0, 3) + '****' + phoneNumber.slice(7);
- },
- // 获取分享配置
- getConfigShare() {
- return new Promise((re, so) => {
- configShare().then(res => {
- re(res.data)
- }).catch(e => so(e))
- })
- }
- }
|