172 lines
4.5 KiB
TypeScript
172 lines
4.5 KiB
TypeScript
// 工具函数文件
|
||
|
||
/**
|
||
* 格式化坐标信息
|
||
* @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> 用户点击确定返回true,点击取消返回false
|
||
*/
|
||
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);
|
||
}
|
||
});
|
||
});
|
||
} |