import statusManager from '@/utils/statusManager.js'; // 日期格式化原型扩展(单独维护) Date.prototype.Format = function (fmt) { const o = { "M+": this.getMonth() + 1, // 月份 "D+": this.getDate(), // 日 "h+": this.getHours(), // 小时 "m+": this.getMinutes(), // 分 "s+": this.getSeconds(), // 秒 "q+": Math.floor((this.getMonth() + 3) / 3), // 季度 "S": this.getMilliseconds() // 毫秒 }; if (/(Y+)/.test(fmt)) { fmt = fmt.replace( RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length) ); } for (const k in o) { if (new RegExp(`(${k})`).test(fmt)) { fmt = fmt.replace( RegExp.$1, RegExp.$1.length === 1 ? o[k] : (`00${o[k]}`).substr((o[k] + "").length) ); } } return fmt; }; // 数据格式化工具方法 const formatUtil = { formatTime(time, pattern) { // uview: uni.$u.date('1585926095536', 'yyyy-mm') // 过滤器: '1585926095536' | date('yyyy-mm') const times = time * 1000; let d = new Date(times).Format("YYYY-MM-DD hh:mm:ss"); if (pattern) { d = new Date(times).Format(pattern); } return d.toLocaleString(); }, // 格式化距离 formatDistance(value) { if (value == null) return '-'; const numValue = parseFloat(value); if (isNaN(numValue)) return '无效距离'; if (numValue < 0.001) return '<1m'; if (numValue < 1) return `${Math.round(numValue * 1000)}m`; return numValue % 1 === 0 ? `${numValue}km` : `${numValue.toFixed(2)}km`; }, // 格式化销售数 formatSales(sales) { if (typeof sales !== 'number' || !isFinite(sales)) return '0'; // 处理负数 sales = Math.max(0, Math.floor(sales)); if (sales < 100) return `${sales}`; if (sales < 1000) return `${Math.floor(sales / 100) * 100}+`; if (sales < 10000) return `${Math.floor(sales / 1000)}k+`; return '1w+'; }, // 格式化评价星数 formatRating(val) { if (val >= 4.5) return "超赞"; if (val >= 4.0) return "满意"; if (val >= 3.0) return "一般"; if (val >= 2.0) return "不满意"; return "非常差"; }, // 格式化数额差值 formatSubtract(num1, num2) { const len1 = (num1.toString().split('.')[1] || '').length; const len2 = (num2.toString().split('.')[1] || '').length; const base = Math.pow(10, Math.max(len1, len2)); return (Math.round(num1 * base) - Math.round(num2 * base)) / base; }, }; // 其他工具方法 const otherUtil = { getQueryStr(name) { let search = window.location.search; const hash = window.location.hash; if (hash.includes('?')) search = hash.split('?')[1] const params = new URLSearchParams(search); const val = params.get(name); return val === null ? null : decodeURIComponent(val) }, // 以token判断是否登录 checkLogin(options = {}) { const { type, modalText = '请登录账号后再操作', } = options; const token = uni.getStorageSync('access-token') || ''; //const token = (uni.getStorageSync('wx_phone') && uni.getStorageSync('access-token')) || ''; const isLogin = !!token; // 后退参数 let str = uni.$u.queryParams({ isBack: 1 }); // 跳转路由 let path = `/pages/myNew/phone${str}`; //let path = `/pages/login/wxLogin${str}`; // 未登录时,根据 type 执行对应操作 if (!isLogin) { switch (type) { case 'navigate-webview': // 跳转登录页内嵌 uni.navigateTo({ url: path+'&type=webview', success: () => { console.log('未登录,已跳转至登录页'); } }); break; case 'navigate': // 跳转登录页(带返回跳转,可返回原页面) uni.navigateTo({ url: path, success: () => { console.log('未登录,已跳转至登录页'); } }); break; case 'modal': // 弹窗提示,确认后跳转登录页 uni.showModal({ title: '温馨提示', content: modalText, confirmText: '前往登录', confirmColor: '#0879FF', success: (res) => { if (res.confirm) { uni.navigateTo({ url: path }); } } }); break; // 默认:仅返回登录状态,不执行额外操作 default: console.log('未登录,仅返回登录状态'); break; } } return isLogin; }, // 获取多级对象的长链 getProp(obj, path, defaultValue = '') { const keys = path.split('.'); let result = obj; for (const key of keys) { if (result && typeof result === 'object' && key in result) { result = result[key]; } else { return defaultValue; } } return result; }, // 获取多图拼接的值 getSplitImg(e, index = 0) { // 先处理原始数据:若e为空,返回空数组 if (!e || typeof e !== 'string') { return []; } // 分割图片字符串为数组 const imgArr = e.split(','); // 判断index是否为null/undefined(用户传空时),返回完整数组 if (index === null || index === undefined) { return imgArr; } // 处理索引:确保是整数,且在有效范围内(0 <= index < 数组长度) const validIndex = Math.floor(Number(index)); // 转为整数(避免传入字符串/小数) if (isNaN(validIndex) || validIndex < 0 || validIndex >= imgArr.length) { return ''; // 索引无效时,返回空字符串(避免显示错误图片) } // 返回指定索引的图片URL return imgArr[validIndex]; }, // 电话 makePhoneCall(e) { if (e) { uni.makePhoneCall({ phoneNumber: e }); } }, // 全局状态管理 statusManager, }; // 组合导出 export default { ...formatUtil, ...otherUtil, };