Files
WXProgram/miniprogram/utils/helpers.ts

172 lines
4.5 KiB
TypeScript
Raw Normal View History

2025-10-16 21:32:16 +08:00
// 工具函数文件
/**
*
* @param longitude
* @param latitude
* @param decimalPlaces 6
* @returns
*/
export function formatCoordinate(longitude: number, latitude: number, decimalPlaces: number = 6): string {
const formattedLongitude = longitude.toFixed(decimalPlaces);
const formattedLatitude = latitude.toFixed(decimalPlaces);
return `经度 ${formattedLongitude}, 纬度 ${formattedLatitude}`;
}
/**
*
* @param coordinate
* @param decimalPlaces 6
* @returns
*/
export function formatSingleCoordinate(coordinate: number, decimalPlaces: number = 6): string {
return coordinate.toFixed(decimalPlaces);
}
/**
*
* @param lng1
* @param lat1
* @param lng2
* @param lat2
* @returns
*/
export function calculateDistance(lng1: number, lat1: number, lng2: number, lat2: number): number {
// 地球半径(单位:米)
const EARTH_RADIUS = 6378137;
// 将角度转换为弧度
const radLat1 = (lat1 * Math.PI) / 180.0;
const radLat2 = (lat2 * Math.PI) / 180.0;
const a = radLat1 - radLat2;
const b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0;
// 应用haversine公式计算球面距离
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 * EARTH_RADIUS;
s = Math.round(s * 10000) / 10000; // 保留4位小数
return s;
}
/**
*
* @param distance
* @returns
*/
export function formatDistance(distance: number): string {
if (distance < 1000) {
return `${Math.round(distance)}`;
} else {
return `${(distance / 1000).toFixed(1)} 千米`;
}
}
/**
*
* @param title
* @param icon 'none'
* @param duration 2000
*/
export function showToast(title: string, icon: 'success' | 'loading' | 'none' = 'none', duration: number = 2000): void {
wx.showToast({
title,
icon,
duration
});
}
/**
*
* @param title '加载中...'
*/
export function showLoading(title: string = '加载中...'): void {
wx.showLoading({
title
});
}
/**
*
*/
export function hideLoading(): void {
wx.hideLoading();
}
/**
*
* @param avatarUrl URL地址
* @returns Promise<string> URL
*/
export async function cacheUserAvatar(avatarUrl: string): Promise<string> {
if (!avatarUrl) return '';
try {
// 检查是否已缓存
const cachedPath = wx.getStorageSync(`avatar_${avatarUrl}`);
if (cachedPath) {
console.log('使用缓存的头像:', cachedPath);
return cachedPath;
}
// 下载头像
const downloadRes = await new Promise<any>((resolve) => {
wx.downloadFile({
url: avatarUrl,
success: (res) => resolve(res),
fail: (err) => {
console.error('下载头像失败:', err);
resolve({ statusCode: 0 }); // 返回失败状态
}
});
});
if (downloadRes.statusCode === 200 && downloadRes.tempFilePath) {
// 缓存头像路径
wx.setStorageSync(`avatar_${avatarUrl}`, downloadRes.tempFilePath);
console.log('头像下载并缓存成功:', downloadRes.tempFilePath);
return downloadRes.tempFilePath;
} else {
console.warn('头像下载失败使用原始URL:', avatarUrl);
return avatarUrl;
}
} catch (error) {
console.error('缓存头像过程中发生错误:', error);
return avatarUrl;
}
}
/**
*
* @param title
* @param content
* @returns Promise<boolean> truefalse
*/
export function showConfirmDialog(title: string, content: string): Promise<boolean> {
return new Promise((resolve) => {
wx.showModal({
title,
content,
success: (res) => {
if (res.confirm) {
resolve(true);
} else if (res.cancel) {
resolve(false);
}
},
fail: () => {
resolve(false);
}
});
});
}