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(/\ { 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 '¥' + arr[0] + '' + 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)) }) }, // 定位授权 async getLocation() { return new Promise((resolve, reject) => { let that = this; // 1、判断手机定位服务【GPS】 是否授权 uni.getSystemInfo({ success(res) { // console.log("判断手机定位服务是否授权:", res); let locationEnabled = res.locationEnabled; //判断手机定位服务是否开启 let locationAuthorized = res.locationAuthorized; //判断定位服务是否允许微信授权 // console.log("判断手机定位服务是否开启",locationEnabled); // console.log("判断定位服务是否允许微信授权",locationAuthorized); if (locationEnabled == false || locationAuthorized == false) { //手机定位服务(GPS)未授权 uni.showToast({ title: '请打开手机GPS', icon: 'none', }); resolve(false); } else { //手机定位服务(GPS)已授权 // 2、判断微信小程序是否授权位置信息 // 微信小程序已授权位置信息 uni.authorize({ //授权请求窗口 scope: 'scope.userLocation', //授权的类型 success: async (res) => { resolve(await that.fnGetlocation()); }, fail: (err) => { err = err['errMsg']; uni.showModal({ content: '需要授权位置信息', confirmText: '确认授权', }).then((obj) => { console.log('obj', obj); if (obj.confirm) { uni.openSetting({ success: async (set) => { if (set.authSetting[ 'scope.userLocation' ]) { // 授权成功 uni.showToast({ title: '授权成功', icon: 'none', }); resolve( await that.fnGetlocation() ); } else { // 未授权 uni.showToast({ title: '授权失败,请重新授权', icon: 'none', }); uni.showModal({ title: '授权', content: '获取授权' + '失败,是否前往授权设置?', success: function(result) { if (result.confirm) { uni.openSetting(); } }, fail: function() { uni.showToast({ title: '系统错误!', icon: 'none', }); }, }); } }, }); } else { resolve(false); // 取消授权 uni.showToast({ title: '你拒绝了授权,无法获得周边信息', icon: 'none', }); } }); }, // async complete(res) { // // console.log('授权弹框', res); // if (res.errMsg == 'authorize:ok') { // resolve(await fnGetlocation()); // } else { // uni.showModal({ // title: '授权', // content: '获取授权失败,是否前往授权设置?', // success: function (result) { // if (result.confirm) { // uni.openSetting(); // } // }, // fail: function () { // uni.showToast({ // title: '系统错误!', // icon: 'none', // }); // }, // }); // } // }, }); } }, }); }); }, // 定位获取 async fnGetlocation() { return new Promise((resolve, reject) => { let that = this; let bindList = { long: '', lat: '', longlat: '', isLocated: false, }; uni.getLocation({ type: 'wgs84', //默认为 wgs84 返回 gps 坐标 geocode: 'true', isHighAccuracy: 'true', accuracy: 'best', // 精度值为20m success: function(res) { let platform = uni.getSystemInfoSync().platform; if (platform == 'ios') { //toFixed() 方法可把 Number 四舍五入为指定小数位数的数字。 bindList.long = res.longitude.toFixed(6); bindList.lat = res.latitude.toFixed(6); } else { bindList.long = res.longitude; bindList.lat = res.latitude; } resolve(bindList); }, fail(err) { if ( err.errMsg === 'getLocation:fail 频繁调用会增加电量损耗,可考虑使用 wx.onLocationChange 监听地理位置变化' ) { uni.showToast({ title: '请勿频繁定位', icon: 'none', }); } if (err.errMsg === 'getLocation:fail auth deny') { // 未授权 uni.showToast({ title: '无法定位,请重新获取位置信息', icon: 'none', }); authDenyCb && authDenyCb(); bindList.isLocated = false; resolve(bindList); } if (err.errMsg === 'getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF') { uni.showModal({ content: '请开启手机定位服务', showCancel: false, }); } }, }); }); } }