167 lines
5.0 KiB
JavaScript
167 lines
5.0 KiB
JavaScript
"use strict";
|
||
// 工具函数文件
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.formatCoordinate = formatCoordinate;
|
||
exports.formatSingleCoordinate = formatSingleCoordinate;
|
||
exports.calculateDistance = calculateDistance;
|
||
exports.formatDistance = formatDistance;
|
||
exports.showToast = showToast;
|
||
exports.showLoading = showLoading;
|
||
exports.hideLoading = hideLoading;
|
||
|
||
exports.cacheUserAvatar = cacheUserAvatar;
|
||
exports.showConfirmDialog = showConfirmDialog;
|
||
/**
|
||
* 格式化坐标信息
|
||
* @param longitude 经度
|
||
* @param latitude 纬度
|
||
* @param decimalPlaces 保留小数位数,默认6位
|
||
* @returns 格式化后的坐标字符串
|
||
*/
|
||
function formatCoordinate(longitude, latitude, decimalPlaces = 6) {
|
||
const formattedLongitude = longitude.toFixed(decimalPlaces);
|
||
const formattedLatitude = latitude.toFixed(decimalPlaces);
|
||
return `经度 ${formattedLongitude}, 纬度 ${formattedLatitude}`;
|
||
}
|
||
/**
|
||
* 格式化单个坐标值
|
||
* @param coordinate 坐标值
|
||
* @param decimalPlaces 保留小数位数,默认6位
|
||
* @returns 格式化后的坐标字符串
|
||
*/
|
||
function formatSingleCoordinate(coordinate, decimalPlaces = 6) {
|
||
return coordinate.toFixed(decimalPlaces);
|
||
}
|
||
/**
|
||
* 计算两点之间的距离(简单的平面距离,非球面距离)
|
||
* @param lng1 第一个点的经度
|
||
* @param lat1 第一个点的纬度
|
||
* @param lng2 第二个点的经度
|
||
* @param lat2 第二个点的纬度
|
||
* @returns 两点之间的距离(单位:米)
|
||
*/
|
||
function calculateDistance(lng1, lat1, lng2, lat2) {
|
||
// 地球半径(单位:米)
|
||
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 格式化后的距离字符串
|
||
*/
|
||
function formatDistance(distance) {
|
||
if (distance < 1000) {
|
||
return `${Math.round(distance)} 米`;
|
||
}
|
||
else {
|
||
return `${(distance / 1000).toFixed(1)} 千米`;
|
||
}
|
||
}
|
||
/**
|
||
* 显示消息提示
|
||
* @param title 提示信息
|
||
* @param icon 图标类型,默认'none'
|
||
* @param duration 显示时长,默认2000毫秒
|
||
*/
|
||
function showToast(title, icon = 'none', duration = 2000) {
|
||
wx.showToast({
|
||
title,
|
||
icon,
|
||
duration
|
||
});
|
||
}
|
||
/**
|
||
* 显示加载提示
|
||
* @param title 提示信息,默认'加载中...'
|
||
*/
|
||
function showLoading(title = '加载中...') {
|
||
wx.showLoading({
|
||
title
|
||
});
|
||
}
|
||
/**
|
||
* 隐藏加载提示
|
||
*/
|
||
function hideLoading() {
|
||
wx.hideLoading();
|
||
}
|
||
|
||
/**
|
||
* 缓存用户头像图片
|
||
* @param avatarUrl 头像URL地址
|
||
* @returns Promise<string> 返回缓存后的头像路径或原始URL
|
||
*/
|
||
async function cacheUserAvatar(avatarUrl) {
|
||
if (!avatarUrl)
|
||
return '';
|
||
try {
|
||
// 检查是否已缓存
|
||
const cachedPath = wx.getStorageSync(`avatar_${avatarUrl}`);
|
||
if (cachedPath) {
|
||
console.log('使用缓存的头像:', cachedPath);
|
||
return cachedPath;
|
||
}
|
||
// 下载头像
|
||
const downloadRes = await new Promise((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
|
||
*/
|
||
function showConfirmDialog(title, content) {
|
||
return new Promise((resolve) => {
|
||
wx.showModal({
|
||
title,
|
||
content,
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
resolve(true);
|
||
}
|
||
else if (res.cancel) {
|
||
resolve(false);
|
||
}
|
||
},
|
||
fail: () => {
|
||
resolve(false);
|
||
}
|
||
});
|
||
});
|
||
}
|