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))
})
}
}