修改位置交互,修改代码逻辑
This commit is contained in:
@@ -1,166 +0,0 @@
|
||||
"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);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
85
miniprogram/utils/roleUtils.ts
Normal file
85
miniprogram/utils/roleUtils.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* 角色工具类 - 处理角色枚举的转换和验证
|
||||
*/
|
||||
|
||||
/**
|
||||
* 角色枚举定义
|
||||
* 服务器可能返回数字角色值,前端使用字符串枚举
|
||||
*/
|
||||
export enum Role {
|
||||
DELIVERY_PERSON = 'DELIVERY_PERSON', // 配送员
|
||||
ADMIN = 'ADMIN' // 管理员
|
||||
}
|
||||
|
||||
/**
|
||||
* 角色值映射
|
||||
* 服务器返回的数字角色值到前端字符串枚举的映射
|
||||
*/
|
||||
export const RoleMapping = {
|
||||
0: Role.DELIVERY_PERSON, // 0对应配送员
|
||||
1: Role.ADMIN // 1对应管理员
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* 将服务器返回的角色值转换为前端枚举
|
||||
* @param roleValue 服务器返回的角色值(可能是数字或字符串)
|
||||
* @returns 标准化的角色枚举值
|
||||
*/
|
||||
export function normalizeRole(roleValue: string | number): Role {
|
||||
if (typeof roleValue === 'number') {
|
||||
// 如果是数字,使用映射表转换
|
||||
return RoleMapping[roleValue as keyof typeof RoleMapping] || Role.DELIVERY_PERSON;
|
||||
}
|
||||
|
||||
// 如果是字符串,直接转换为大写进行比较
|
||||
const normalizedRole = roleValue.toUpperCase();
|
||||
|
||||
// 检查是否是有效的角色值
|
||||
if (normalizedRole === Role.ADMIN) {
|
||||
return Role.ADMIN;
|
||||
}
|
||||
|
||||
// 默认返回配送员
|
||||
return Role.DELIVERY_PERSON;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色显示文本
|
||||
* @param role 角色枚举值
|
||||
* @returns 对应的中文显示文本
|
||||
*/
|
||||
export function getRoleText(role: Role): string {
|
||||
switch (role) {
|
||||
case Role.ADMIN:
|
||||
return '管理员';
|
||||
case Role.DELIVERY_PERSON:
|
||||
return '配送员';
|
||||
default:
|
||||
return '未知角色';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证角色值是否有效
|
||||
* @param roleValue 角色值
|
||||
* @returns 是否有效
|
||||
*/
|
||||
export function isValidRole(roleValue: string | number): boolean {
|
||||
try {
|
||||
const normalized = normalizeRole(roleValue);
|
||||
return normalized === Role.ADMIN || normalized === Role.DELIVERY_PERSON;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色选项列表(用于下拉选择器)
|
||||
* @returns 角色选项数组
|
||||
*/
|
||||
export function getRoleOptions(): Array<{ value: Role; label: string }> {
|
||||
return [
|
||||
{ value: Role.DELIVERY_PERSON, label: '配送员' },
|
||||
{ value: Role.ADMIN, label: '管理员' }
|
||||
];
|
||||
}
|
@@ -1,19 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.formatTime = void 0;
|
||||
const formatTime = (date) => {
|
||||
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].map(formatNumber).join('/') +
|
||||
' ' +
|
||||
[hour, minute, second].map(formatNumber).join(':'));
|
||||
};
|
||||
exports.formatTime = formatTime;
|
||||
const formatNumber = (n) => {
|
||||
const s = n.toString();
|
||||
return s[1] ? s : '0' + s;
|
||||
};
|
Reference in New Issue
Block a user