Browse Source

fix 获取地理位置更新

master
wangwei_123 9 hours ago
parent
commit
581a28298a
  1. 1
      server/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/convert/《芋道 Spring Boot 对象转换 MapStruct 入门》.md
  2. 56
      web/src/utils/lock.ts

1
server/yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/convert/《芋道 Spring Boot 对象转换 MapStruct 入门》.md

@ -1 +0,0 @@
<http://www.iocoder.cn/Spring-Boot/MapStruct/?yudao>

56
web/src/utils/lock.ts

@ -17,42 +17,52 @@ type Location = {
longitude: number longitude: number
accuracy: number accuracy: number
} }
export const getCurrentLocation = async (): Promise<Location> => { export const getCurrentLocation = async (): Promise<Location> => {
if (appStore.isWorkWechat) { if (appStore.isWorkWechat) {
const res = await ww.getLocation()
return {
latitude: res.latitude,
longitude: res.longitude,
accuracy: res.accuracy
try {
// 企业微信的 ww.getLocation 已经是 Promise-based,直接使用
const res = await ww.getLocation();
return {
latitude: res.latitude,
longitude: res.longitude,
accuracy: res.accuracy,
};
} catch (error) {
ElMessage.error('在企业微信中获取位置失败');
// 抛出错误,让调用者可以捕获
return Promise.reject(error);
} }
} else if (window.navigator.geolocation) {
}
if (window.navigator.geolocation) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
window.navigator.geolocation.getCurrentPosition( window.navigator.geolocation.getCurrentPosition(
(position) => { (position) => {
resolve({ resolve({
latitude: position.coords.latitude, latitude: position.coords.latitude,
longitude: position.coords.longitude, longitude: position.coords.longitude,
accuracy: position.coords.accuracy
})
accuracy: position.coords.accuracy,
});
}, },
(error) => { (error) => {
ElMessage.error('获取位置失败')
resolve({
latitude: 0,
longitude: 0,
accuracy: 0
})
// 在 Promise 中,错误情况应该调用 reject
ElMessage.error('获取位置失败');
reject(error); // [9, 10, 12]
},
// 可以增加一些选项来优化定位,例如超时设置 [13, 14]
{
timeout: 10000, // 10秒超时
enableHighAccuracy: true,
} }
)
})
} else {
return Promise.resolve({
latitude: 0,
longitude: 0,
accuracy: 0
})
);
});
} }
}
// 如果环境完全不支持地理位置,也应该 reject
ElMessage.error('您的环境不支持获取地理位置');
return Promise.reject(new Error('Geolocation is not supported.'));
};
// 统一执行绑定逻辑:校验锁状态 -> 获取位置 -> 记录工单 -> 批量更新状态 // 统一执行绑定逻辑:校验锁状态 -> 获取位置 -> 记录工单 -> 批量更新状态
async function performBind(detail: any, lockId: number, operatorId: number): Promise<void> { async function performBind(detail: any, lockId: number, operatorId: number): Promise<void> {

Loading…
Cancel
Save