/** * 获取当前页面路径 * @returns 当前页面路径 */ export function getCurrentPath() { const pages = getCurrentPages() const currentPage = pages[pages.length - 1] return currentPage.route || '' } /** * 检测当前环境是否为微信小程序 */ export function isWxMiniProgram() { return new Promise((resolve, reject) => { const apltInfo = uni.getDeviceInfo() console.log(apltInfo, '当前环境=======================') if (apltInfo.platform === 'windows') { useGlobalToast().show('不支持电脑操作地址,请使用手机打开') reject(new Error('不支持电脑操作地址,请使用手机打开')) } resolve(1) }) } /** * 将角度转换为弧度 */ function toRadians(degrees: number): number { return degrees * Math.PI / 180 } /** * 将弧度转换为角度 */ function toDegrees(radians: number): number { return radians * 180 / Math.PI } /** * 计算两个经纬度的中心点(球面插值法) * 适用于任意距离,考虑地球曲率,更精确 * * @param pointA 第一个经纬度坐标 * @param pointB 第二个经纬度坐标 * @returns 中心点经纬度坐标 */ export function calculateCenterPointSpherical( pointA: { lat: number, lng: number }, pointB: { lat: number, lng: number }, ): { lat: number, lng: number } { // 将经纬度转换为弧度 const lat1 = toRadians(pointA.lat) const lng1 = toRadians(pointA.lng) const lat2 = toRadians(pointB.lat) const lng2 = toRadians(pointB.lng) // 计算纬度的差值 const dLng = lng2 - lng1 // 使用球面插值公式计算中心点 const Bx = Math.cos(lat2) * Math.cos(dLng) const By = Math.cos(lat2) * Math.sin(dLng) const centerLat = Math.atan2( Math.sin(lat1) + Math.sin(lat2), Math.sqrt((Math.cos(lat1) + Bx) * (Math.cos(lat1) + Bx) + By * By), ) const centerLng = lng1 + Math.atan2(By, Math.cos(lat1) + Bx) // 将结果转换回角度 return { lat: toDegrees(centerLat), lng: toDegrees(centerLng), } } /** * 订单统一枚举 */ export enum OrderStatus { /** * 待支付 */ PaddingPay = 0, /** * 订单已接单 */ OrderAccepted = 20, /** * 订单待配送 */ OrderWaitDelivery = 30, /** * 订单配送中 */ OrderDelivering = 40, /** * 订单取消审核 */ OrderCancelAudit = 50, /** * 订单取消 */ OrderCancel = 60, /** * 订单已过期 */ OrderExpired = 62, /** * 大健康退款 */ OrderRefund = 61, /** * 订单已送达 */ OrderArrived = 70, /** * 订单完成 */ OrderCompleted = 80, } /** * 把true转为数字true为1,false是0 * * @param value */ export function boolToNumber(value: boolean): number { return value ? 1 : 0 } /** * 输入框格式校验工具类 */ export class InputFormatUtil { /** * 手机号校验 * @param phone */ static isPhone(phone: string) { return /^1[3-9]\d{9}$/.test(phone) } /** * QQ号校验 * @param qq */ static isQQ(qq: string) { return /^[1-9]\d{4,10}$/.test(qq) } } /** * 手机号*号 */ export function phoneFormat(phone: string) { return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') } /** * 从 URL 中解析查询参数 * 支持已编码和未编码的 URL * @param url URL 字符串,可以是完整 URL 或仅查询字符串部分 * @returns 解析后的参数对象 * @example */ export function parseUrlParams(url: string): Record { if (!url) return {} try { // 尝试解码(处理多次编码的情况) let decodedUrl = url while (decodedUrl.includes('%')) { const newDecoded = decodeURIComponent(decodedUrl) if (newDecoded === decodedUrl) break decodedUrl = newDecoded } // 提取查询字符串部分 let queryString = decodedUrl const questionIndex = decodedUrl.indexOf('?') if (questionIndex !== -1) { queryString = decodedUrl.substring(questionIndex + 1) } // 移除可能存在的 hash 部分 const hashIndex = queryString.indexOf('#') if (hashIndex !== -1) { queryString = queryString.substring(0, hashIndex) } if (!queryString) return {} // 解析参数 const params: Record = {} const pairs = queryString.split('&') pairs.forEach((pair) => { const equalIndex = pair.indexOf('=') if (equalIndex > 0) { const key = decodeURIComponent(pair.substring(0, equalIndex)) const val = decodeURIComponent(pair.substring(equalIndex + 1)) params[key] = val } else if (pair) { params[decodeURIComponent(pair)] = '' } }) return params } catch { return {} } } /** *处理富文本图片溢出 * @param html - 富文本字符串 * @returns */ export function fixImgStyle(html: string) { if (!html) return html // 处理已有 style 属性的 img 标签:将样式追加到现有 style 中 let result = html.replace(/]*)style\s*=\s*["']([^"']*)["']([^>]*)>/gi, (match, before, existingStyle, after) => { // 移除可能存在的 width/height 内联样式,然后添加防溢出样式 const cleanedStyle = existingStyle.replace(/\bwidth\s*:[^;]*;?/gi, '').replace(/\bheight\s*:[^;]*;?/gi, '') const newStyle = `${cleanedStyle};max-width:100%;height:auto;display:block;`.replace(/^;+|;+$/g, '').replace(/;{2,}/g, ';') return `` }) // 处理没有 style 属性的 img 标签:添加新的 style result = result.replace(/]*style\s*=)([^>]*)>/gi, '') return result } // 从地址字符串中提取城市名称 export function getCityName(address: string): string { if (!address) return '' // 安全正则,无 ESLint 警告 const regex = /^[^省]+省([^市]+)/ const match = address.match(regex) if (match) { return match[1] } // 兼容直辖市(北京 / 上海 / 重庆 / 天津) const municipality = address.match(/(北京|上海|重庆|天津)/) return municipality ? municipality[1] : '' }