diff --git a/niucloud/public/wap/index.html b/niucloud/public/wap/index.html
index 32d512117..d65ea37bc 100644
--- a/niucloud/public/wap/index.html
+++ b/niucloud/public/wap/index.html
@@ -14,7 +14,7 @@
-
+
diff --git a/uni-app/.env.development b/uni-app/.env.development
index c91d54be7..2cb61c2cb 100644
--- a/uni-app/.env.development
+++ b/uni-app/.env.development
@@ -16,4 +16,4 @@ VITE_REQUEST_HEADER_TOKEN_KEY='token'
VITE_REQUEST_HEADER_CHANNEL_KEY='channel'
# 应用版本
-VITE_APP_VERSION='1.0.1'
\ No newline at end of file
+VITE_APP_VERSION='1.0.1'
diff --git a/uni-app/src/addon/components/diy/group/index.vue b/uni-app/src/addon/components/diy/group/index.vue
index 79184057a..a3463d13c 100644
--- a/uni-app/src/addon/components/diy/group/index.vue
+++ b/uni-app/src/addon/components/diy/group/index.vue
@@ -123,7 +123,6 @@
-
diff --git a/uni-app/src/app/pages/member/address_edit.vue b/uni-app/src/app/pages/member/address_edit.vue
index ceba0c4f6..abe241cfe 100644
--- a/uni-app/src/app/pages/member/address_edit.vue
+++ b/uni-app/src/app/pages/member/address_edit.vue
@@ -85,6 +85,8 @@ import useSystemStore from '@/stores/system';
import { useLocation } from '@/hooks/useLocation'
const systemStore = useSystemStore();
+const { refresh: refreshLocation } = useLocation(true);
+
const formData: any = ref({
id: 0,
name: '',
@@ -111,6 +113,7 @@ const isSelectMap = ref(2) // 值为1,该地址需要有经纬度,反之不
const wxPrivacyPopupRef: any = ref(null)
onLoad((data: any) => {
+ refreshLocation();
isSelectMap.value = data.isSelectMap || '';
const selectAddress = uni.getStorageSync('selectAddressCallback')
if (data.id) {
@@ -260,8 +263,12 @@ const save = () => {
// 选择地址
const chooseLocation = () => {
- let latitude = systemStore.diyAddressInfo ? systemStore.diyAddressInfo.latitude : '';
- let longitude = systemStore.diyAddressInfo ? systemStore.diyAddressInfo.longitude : '';
+ // 安全取值,空对象时直接返回空字符串
+ const latitude = systemStore.diyAddressInfo?.latitude ?? ''
+ const longitude = systemStore.diyAddressInfo?.longitude ?? ''
+
+ // let latitude = systemStore.diyAddressInfo ? systemStore.diyAddressInfo.latitude : '';
+ // let longitude = systemStore.diyAddressInfo ? systemStore.diyAddressInfo.longitude : '';
// #ifndef H5
uni.chooseLocation({
@@ -276,6 +283,12 @@ const chooseLocation = () => {
if (res.latitude && res.longitude) {
let latng = res.latitude + ',' + res.longitude;
getAddress(latng);
+ // 关键:选完地址后,存入仓库 + 本地缓存
+ systemStore.setAddressInfo({
+ latitude: res.latitude,
+ longitude: res.longitude,
+ address: res.address
+ })
}
},
fail: (res) => {
@@ -283,12 +296,15 @@ const chooseLocation = () => {
if (res.errMsg && res.errno) {
if (res.errno == 104) {
let msg = '用户未授权隐私权限,选择位置失败';
- uni.showToast({ title: msg, icon: 'none' })
+ // uni.showToast({ title: msg, icon: 'none' })
+ console.log(msg)
} else if (res.errno == 112) {
let msg = '隐私协议中未声明,打开地图选择位置失败';
- uni.showToast({ title: msg, icon: 'none' })
+ // uni.showToast({ title: msg, icon: 'none' })
+ console.log(msg)
} else {
- uni.showToast({ title: res.errMsg, icon: 'none' })
+ // uni.showToast({ title: res.errMsg, icon: 'none' })
+ console.log(res.errMsg)
}
}
}
@@ -385,7 +401,7 @@ const choosegAddress = () =>{
// 匹配市id(基于省id)
formData.value.city_id = findIdByNameAndParentId(areaTree.value, res.cityName, formData.value.province_id);
// 匹配区id(基于市id)
- formData.value.district_id = findIdByNameAndParentId(areaTree.value, res.countyName, formData.value.cityId);
+ formData.value.district_id = findIdByNameAndParentId(areaTree.value, res.countyName, formData.value.city_id);
},
fail(err) {
loadingchoosegAddress.value = false
@@ -393,7 +409,14 @@ const choosegAddress = () =>{
});
}
-onMounted(()=>{
+onMounted(async ()=>{
+ // 读取本地缓存的历史定位地址
+ const cacheLocation = uni.getStorageSync('location_address')
+ if(cacheLocation && Object.keys(cacheLocation).length) {
+ // 调用仓库方法,把缓存塞进去
+ systemStore.setAddressInfo(cacheLocation)
+ }
+
// #ifdef MP
uni.authorize({
scope: 'scope.userLocation',
@@ -410,7 +433,7 @@ onMounted(()=>{
success: (settingRes) => {
if (settingRes.authSetting['scope.userLocation']) {
const locationVal = useLocation(true);
- locationVal.init();
+ locationVal.refresh();
}
}
});
diff --git a/uni-app/src/components/share-poster/share-poster.vue b/uni-app/src/components/share-poster/share-poster.vue
index 46210e458..d6ab89dd1 100644
--- a/uni-app/src/components/share-poster/share-poster.vue
+++ b/uni-app/src/components/share-poster/share-poster.vue
@@ -205,15 +205,19 @@ const savePoster = () => {
fail: (err) => {
if (err.errno == 104) {
let msg = '用户未授权隐私权限,将图像保存到相册失败';
- uni.showToast({ title: msg, icon: 'none' })
+ // uni.showToast({ title: msg, icon: 'none' })
+ console.log(msg)
} else if (err.errMsg == "saveImageToPhotosAlbum:fail auth deny" ||
err.errMsg == "saveImageToPhotosAlbum:fail:auth denied") {
show.value = true;
+ console.log(err.errMsg)
} else if (err.errMsg == "saveImageToPhotosAlbum:fail cancel") {
let msg = '用户取消将图片保存到相册';
- uni.showToast({ title: msg, icon: 'none' })
+ // uni.showToast({ title: msg, icon: 'none' })
+ console.log(msg)
} else {
- uni.showToast({ title: err.errMsg, icon: 'none' })
+ // uni.showToast({ title: err.errMsg, icon: 'none' })
+ console.log(err.errMsg)
}
}
});
diff --git a/uni-app/src/hooks/useLocation.ts b/uni-app/src/hooks/useLocation.ts
index 632cf27ab..b5f6f2e76 100644
--- a/uni-app/src/hooks/useLocation.ts
+++ b/uni-app/src/hooks/useLocation.ts
@@ -4,6 +4,10 @@ import { getAddressByLatlng } from '@/app/api/system';
import wechat from '@/utils/wechat'
import useSystemStore from '@/stores/system';
+// 单例定位服务
+// 使用全局 Promise 确保整个应用中只有一个定位请求在执行
+let locationPromise: Promise
| null = null;
+
export function useLocation(isOpenLocation: any) {
const systemStore = useSystemStore();
@@ -13,6 +17,80 @@ export function useLocation(isOpenLocation: any) {
isOpen = false;
}
+ const init = () => {
+ if (!isOpen) return false;
+
+ // 使用 locationPromise 作为唯一的锁源
+ if (locationPromise) {
+ return;
+ }
+
+ // 立即设置空 Promise 作为锁
+ locationPromise = new Promise(() => {});
+
+ // 排除手动地图选择位置的情况
+ if (!uni.getStorageSync('manually_select_location_from_map')) {
+ // #ifdef H5
+ if (isWeixinBrowser() && systemStore.mapConfig.is_open && !uni.getStorageSync('location_address')) {
+ wechat.init(() => {
+ wechat.getLocation(res => {
+ locationPromise = null;
+ let latlng = res.latitude + ',' + res.longitude;
+ getAddressByLatlngFn(latlng);
+ });
+ });
+ } else {
+ locationPromise = null;
+ }
+ // #endif
+
+ // #ifndef H5
+ if (systemStore.mapConfig.is_open && !uni.getStorageSync('location_address')) {
+ uni.getLocation({
+ // #ifndef APP-PLUS
+ type: 'gcj02',
+ // #endif
+ success: res => {
+ locationPromise = null;
+ let latlng = res.latitude + ',' + res.longitude;
+ getAddressByLatlngFn(latlng);
+ },
+ fail: (res) => {
+ locationPromise = null;
+ // uni.showToast({ title: res.errMsg, icon: 'none', duration: 15000 });
+ systemStore.defaultPositionAddress = '定位失败';
+ if (res.errno) {
+ if (res.errno == 104) {
+ let msg = '用户未授权隐私权限,获取位置失败';
+ // uni.showToast({ title: msg, icon: 'none' });
+ console.log(msg)
+ } else if (res.errno == 112) {
+ let msg = '隐私协议中未声明,获取位置失败';
+ // uni.showToast({ title: msg, icon: 'none' });
+ console.log(msg)
+ }
+ }
+ if (res.errMsg) {
+ if (res.errMsg.indexOf('getLocation:fail') != -1 || res.errMsg.indexOf('deny') != -1 || res.errMsg.indexOf('denied') != -1) {
+ let msg = '用户未授权获取位置权限,将无法提供服务';
+ // uni.showToast({ title: msg, icon: 'none' });
+ console.log(msg)
+ } else {
+ // uni.showToast({ title: res.errMsg, icon: 'none' });
+ console.log(res.errMsg)
+ }
+ }
+ }
+ });
+ } else {
+ locationPromise = null;
+ }
+ // #endif
+ } else {
+ locationPromise = null;
+ }
+ };
+
// 监听页面加载
const onLoadLifeCycle = (callback: any = '') => {
onLoad((option: any) => {
@@ -25,76 +103,112 @@ export function useLocation(isOpenLocation: any) {
});
}
- const init = () => {
+ // 刷新页面时需要调用的
+ const refreshLocation = () => {
if (!isOpen) return false;
+
+ // 使用 locationPromise 作为唯一的锁源
+ // 如果已经有定位在进行,直接返回
+ if (locationPromise) {
+ return;
+ }
+
+ // 立即设置空 Promise 作为锁(这是关键!必须在任何检查之前设置)
+ // 第二个调用者会在上面的检查中看到 locationPromise 已存在,直接返回
+ locationPromise = new Promise(() => {});
+
+ // 只读取一次本地缓存,减少重复存储调用
+ const locationCache = uni.getStorageSync('location_address');
+ const manualSelect = uni.getStorageSync('manually_select_location_from_map');
+
+ // 存在手动选点标记,直接终止逻辑
+ if (manualSelect) {
+ locationPromise = null;
+ return;
+ }
+
+ // 有定位缓存
+ if (locationCache) {
+ const cacheInfo = locationStorage();
+ // 缓存未过期,恢复定位信息
+ if (!cacheInfo.is_expired) {
+ // 检查经纬度是否有效(非空且非0)
+ const hasValidLatLng = locationCache.latitude && locationCache.longitude &&
+ locationCache.latitude !== 0 && locationCache.longitude !== 0;
+ if (hasValidLatLng) {
+ systemStore.setAddressInfo(locationCache);
+ locationPromise = null;
+ return; // 有有效缓存,直接返回,不触发定位
+ }
+ // 经纬度无效,继续往下走重新定位
+ }
+ // 缓存过期或经纬度无效,删除缓存
+ uni.removeStorageSync('location_address');
+ }
+
+ // 无缓存或缓存无效/过期,直接执行定位
// 排除手动地图选择位置的情况
if (!uni.getStorageSync('manually_select_location_from_map')) {
// #ifdef H5
- if (isWeixinBrowser() && systemStore.mapConfig.is_open && !uni.getStorageSync('location_address')) {
+ if (isWeixinBrowser() && systemStore.mapConfig.is_open) {
wechat.init(() => {
wechat.getLocation(res => {
- let latlng = res.latitude + ',' + res.longitude; // 纬度(浮点数,范围为90 ~ -90),经度(浮点数,范围为180 ~ -180)
- getAddressByLatlngFn(latlng)
- })
+ locationPromise = null;
+ let latlng = res.latitude + ',' + res.longitude;
+ getAddressByLatlngFn(latlng);
+ });
});
-
+ } else {
+ locationPromise = null;
}
// #endif
// #ifndef H5
- if (systemStore.mapConfig.is_open && !uni.getStorageSync('location_address')) {
+ if (systemStore.mapConfig.is_open) {
uni.getLocation({
// #ifndef APP-PLUS
type: 'gcj02',
// #endif
success: res => {
- let latlng = res.latitude + ',' + res.longitude; // 纬度(浮点数,范围为90 ~ -90),经度(浮点数,范围为180 ~ -180)
+ locationPromise = null;
+ let latlng = res.latitude + ',' + res.longitude;
getAddressByLatlngFn(latlng);
},
fail: (res) => {
- uni.showToast({ title: res.errMsg, icon: 'none', duration: 15000 })
+ locationPromise = null;
+ // uni.showToast({ title: res.errMsg, icon: 'none', duration: 15000 });
systemStore.defaultPositionAddress = '定位失败';
if (res.errno) {
if (res.errno == 104) {
let msg = '用户未授权隐私权限,获取位置失败';
- uni.showToast({ title: msg, icon: 'none' })
+ // uni.showToast({ title: msg, icon: 'none' });
+ console.log(msg)
} else if (res.errno == 112) {
let msg = '隐私协议中未声明,获取位置失败';
- uni.showToast({ title: msg, icon: 'none' })
+ // uni.showToast({ title: msg, icon: 'none' });
+ console.log(msg)
}
}
if (res.errMsg) {
if (res.errMsg.indexOf('getLocation:fail') != -1 || res.errMsg.indexOf('deny') != -1 || res.errMsg.indexOf('denied') != -1) {
let msg = '用户未授权获取位置权限,将无法提供服务';
- uni.showToast({ title: msg, icon: 'none' })
+ // uni.showToast({ title: msg, icon: 'none' });
+ console.log(msg)
} else {
- uni.showToast({ title: res.errMsg, icon: 'none' })
+ // uni.showToast({ title: res.errMsg, icon: 'none' });
+ console.log(res.errMsg)
}
}
}
});
+ } else {
+ locationPromise = null;
}
// #endif
+ } else {
+ locationPromise = null;
}
- }
-
- // 刷新页面时需要调用的
- const refreshLocation = () => {
- if (!isOpen) return false;
- if (!uni.getStorageSync('manually_select_location_from_map') && uni.getStorageSync('location_address')) {
- if (locationStorage() && !locationStorage().is_expired) {
- systemStore.setAddressInfo(uni.getStorageSync('location_address'))
- } else {
- uni.removeStorageSync('location_address');
- }
-
- }
-
- // 定位信息过期后,重新获取定位
- if (!uni.getStorageSync('manually_select_location_from_map') && locationStorage() && locationStorage().is_expired) {
- init();
- }
- }
+ };
// 通过经纬度获取地址信息
const getAddressByLatlngFn = (latlng: any = '') => {
@@ -161,12 +275,15 @@ export function useLocation(isOpenLocation: any) {
if (res.errMsg && res.errno) {
if (res.errno == 104) {
let msg = '用户未授权隐私权限,选择位置失败';
- uni.showToast({ title: msg, icon: 'none' })
+ // uni.showToast({ title: msg, icon: 'none' })
+ console.log(msg)
} else if (res.errno == 112) {
let msg = '隐私协议中未声明,打开地图选择位置失败';
- uni.showToast({ title: msg, icon: 'none' })
+ // uni.showToast({ title: msg, icon: 'none' })
+ console.log(msg)
} else {
- uni.showToast({ title: res.errMsg, icon: 'none' })
+ // uni.showToast({ title: res.errMsg, icon: 'none' })
+ console.log(res.errMsg)
}
}
}
diff --git a/uni-app/src/pages.json b/uni-app/src/pages.json
index 8e766e6e2..d7b29d463 100644
--- a/uni-app/src/pages.json
+++ b/uni-app/src/pages.json
@@ -210,6 +210,7 @@
],
"subPackages": [
// {{ PAGE_BEGAIN }}
+
// {{ PAGE_END }}
{
"root": "app/components",
diff --git a/uni-app/src/stores/system.ts b/uni-app/src/stores/system.ts
index a1b7fd701..54644c7d2 100644
--- a/uni-app/src/stores/system.ts
+++ b/uni-app/src/stores/system.ts
@@ -61,7 +61,7 @@ const useSystemStore = defineStore('system', {
},
shareCallback: null,
defaultPositionAddress: '定位中',
- diyAddressInfo: null,
+ diyAddressInfo: {},
currTabbar: {
path: '',
query: {}
diff --git a/uni-app/src/utils/common.ts b/uni-app/src/utils/common.ts
index c36ad9096..56ea4eebc 100644
--- a/uni-app/src/utils/common.ts
+++ b/uni-app/src/utils/common.ts
@@ -576,12 +576,15 @@ export function copy(value: any, callback: any) {
if (res.errMsg && res.errno) {
if (res.errno == 104) {
let msg = '用户未授权隐私权限,设置剪贴板数据失败';
- uni.showToast({ title: msg, icon: 'none' })
+ // uni.showToast({ title: msg, icon: 'none' })
+ console.log(msg)
} else if (res.errno == 112) {
let msg = '隐私协议中未声明,设置剪贴板数据失败';
- uni.showToast({ title: msg, icon: 'none' })
+ // uni.showToast({ title: msg, icon: 'none' })
+ console.log(msg)
} else {
- uni.showToast({ title: res.errMsg, icon: 'none' })
+ // uni.showToast({ title: res.errMsg, icon: 'none' })
+ console.log(res.errMsg)
}
}
}
@@ -845,7 +848,7 @@ export function openMapSelector(backurl: string, params = '') {
// 天地图的位置选择器URL
// 注意:天地图的位置选择器API可能与腾讯地图不同,需要根据实际API文档调整
const baseUrl = (import.meta.env.VITE_APP_BASE_URL || `${ location.origin }`).replace(/\/api\/$/, '')
- const url = baseUrl + `/tianmap/index.html?search=1&type=0&backurl=${ encodeURIComponent(backurl) }&key=${ tiandituKey }` + (params ? '&' + params : '')
+ const url = baseUrl + `/tianmap/index.html?search=1&type=0&backurl=${ encodeURIComponent(backurl) }&key=${ tiandituKey }&t=${Date.now()}` + (params ? '&' + params : '')
window.location.href = url;
}
}
\ No newline at end of file