import Taro from '@tarojs/taro'; // 经纬度计算距离 export function calculateDistance( la1: number, lo1: number, la2: number, lo2: number, ): any { const radLat1 = (la1 * Math.PI) / 180.0; const radLat2 = (la2 * Math.PI) / 180.0; const a = radLat1 - radLat2; const b = (lo1 * Math.PI) / 180.0 - (lo2 * Math.PI) / 180.0; let s = 2 * Math.asin( Math.sqrt( Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2), ), ); s = s * 6378.137; s = Math.round(s * 10000) / 10000; return s.toFixed(2) + 'km'; } // 将角度转换为弧度 function toRadians(degrees: number): number { return (degrees * Math.PI) / 180; } // url转对象 interface UrlParams { type?: string; gid?: string; bid?: string; } export function parseQueryString(url: string) { const queryString = url.split('?')[1]; if (!queryString) { return {}; } const keyValuePairs = queryString.split('&'); const result: UrlParams = {}; keyValuePairs.forEach(keyValue => { const [key, value] = keyValue.split('='); result[key] = decodeURIComponent(value); }); return result; } // 格式化时间 export function formatTime(time: number): string { const date = new Date(time); const year = date.getFullYear(); const month = date.getMonth() + 1; const day = date.getDate(); const hour = date.getHours(); const minute = date.getMinutes(); const second = date.getSeconds(); return `${year}-${month}-${day} ${hour}:${minute}:${second}`; } // 字符串脱敏 export function maskString(str: string, start: number, end: number): string { const maskLength = Math.min(str.length, end) - Math.max(0, start); const maskedPart = '*'.repeat(maskLength); const beginning = str.slice(0, Math.max(0, start)); const endPart = str.slice(end); return beginning + maskedPart + endPart; } // tips export function showTips(msg: string) { Taro.showToast({ title: msg, icon: 'none', }); } // 防抖函数 export function debounce(fn: Function, delay: number = 500) { let timer: any = null; return function (...args: any[]) { if (timer) { clearTimeout(timer); } timer = setTimeout(() => { fn.apply(this, args); }, delay); }; } /* @param fn 节流函数 * @param delay 节流时间 */ export function throttle(fn: Function, delay: number) { let timer: any = null; return function (...args: any[]) { if (!timer) { timer = setTimeout(() => { fn.apply(this, args); timer = null; }, delay); } }; }