|
|
@@ -1,6 +1,7 @@
|
|
|
import { resolve } from 'path'
|
|
|
import { defineStore } from 'pinia'
|
|
|
import { ref } from 'vue'
|
|
|
+import { getCity } from "@/api/home";
|
|
|
|
|
|
let initCurrentCity = ref({
|
|
|
name: '全部地区',
|
|
|
@@ -8,8 +9,24 @@ let initCurrentCity = ref({
|
|
|
})
|
|
|
|
|
|
|
|
|
+// 从本地存储恢复城市信息
|
|
|
+const restoreCityFromStorageInit = (): { name: string; value: string } => {
|
|
|
+ const storedCityName = uni.getStorageSync('cityName')
|
|
|
+ const storedCityCode = uni.getStorageSync('cityCode')
|
|
|
+
|
|
|
+ if (storedCityName && storedCityName !== '') {
|
|
|
+ return {
|
|
|
+ name: storedCityName,
|
|
|
+ value: storedCityCode || storedCityName.replace('市', ''),
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return { name: '全部地区', value: 'all' }
|
|
|
+}
|
|
|
+
|
|
|
export const useAreaStore = defineStore('area', () => {
|
|
|
- const currentCity = ref(initCurrentCity.value)
|
|
|
+ // 初始化时自动从本地存储恢复城市信息
|
|
|
+ const restoredCity = restoreCityFromStorageInit()
|
|
|
+ const currentCity = ref(restoredCity)
|
|
|
const lastSelectedCity = ref([])
|
|
|
|
|
|
const addLastSelectedCity = (city) => {
|
|
|
@@ -37,16 +54,17 @@ export const useAreaStore = defineStore('area', () => {
|
|
|
|
|
|
/**
|
|
|
* 获取当前地理位置
|
|
|
+ * 优先使用GPS定位,如果用户拒绝则降级使用IP定位
|
|
|
* @returns Promise 包含城市信息
|
|
|
*/
|
|
|
const getLocationAndSetCity = (): Promise<{ name: string; value: string }> => {
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
- // 1. 获取经纬度
|
|
|
+ // 1. 尝试获取GPS定位
|
|
|
uni.getLocation({
|
|
|
type: 'gcj02',
|
|
|
geocode: true,
|
|
|
success: async (res) => {
|
|
|
- console.log('获取地理位置成功:', res)
|
|
|
+ console.log('获取GPS地理位置成功:', res)
|
|
|
|
|
|
// 保存定位信息
|
|
|
uni.setStorageSync('positioning', {
|
|
|
@@ -57,23 +75,49 @@ export const useAreaStore = defineStore('area', () => {
|
|
|
// 2.天地图逆地理编码 (经纬度 -> 获取城市信息)
|
|
|
await updateGeocode(res.latitude, res.longitude);
|
|
|
|
|
|
- resolve()
|
|
|
+ // 更新当前城市
|
|
|
+ if (initCurrentCity.value.name !== '全部地区') {
|
|
|
+ currentCity.value = initCurrentCity.value
|
|
|
+ }
|
|
|
+ resolve(currentCity.value)
|
|
|
|
|
|
},
|
|
|
- fail: (err) => {
|
|
|
- console.error('获取地理位置失败:', err)
|
|
|
- // 获取失败时使用默认城市
|
|
|
- const defaultCity = {
|
|
|
- name: '太原市',
|
|
|
- value: '140100',
|
|
|
- }
|
|
|
- initCurrentCity.value = defaultCity
|
|
|
- resolve(defaultCity)
|
|
|
+ fail: async (err) => {
|
|
|
+ console.error('获取GPS地理位置失败,降级使用IP定位:', err)
|
|
|
+
|
|
|
+ await getIPLocation()
|
|
|
+ // IP定位成功后更新当前城市
|
|
|
+ if (initCurrentCity.value.name !== '全部地区') {
|
|
|
+ currentCity.value = initCurrentCity.value
|
|
|
+ }
|
|
|
+ resolve(currentCity.value)
|
|
|
},
|
|
|
})
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+ // 获取 IP 定位
|
|
|
+ const getIPLocation = (): Promise<void> => {
|
|
|
+ return new Promise(async (resolve) => {
|
|
|
+ try {
|
|
|
+ const res: any = await getCity().send()
|
|
|
+ if (res ) {
|
|
|
+ // 保存定位信息
|
|
|
+ uni.setStorageSync('positioning', {
|
|
|
+ latitude: res.lat,
|
|
|
+ longitude: res.lng
|
|
|
+ })
|
|
|
+
|
|
|
+ // 天地图逆地理编码 (经纬度 -> 获取城市信息)
|
|
|
+ await updateGeocode(res.lat || 37.860262, res.lng || 112.556272);
|
|
|
+ }
|
|
|
+ resolve();
|
|
|
+ } catch (e) {
|
|
|
+ console.error('IP定位失败:', e)
|
|
|
+ resolve();
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
/**
|
|
|
* 获取当前城市的逆地理编码信息
|
|
|
* @returns Promise 包含城市信息
|
|
|
@@ -136,7 +180,8 @@ export const useAreaStore = defineStore('area', () => {
|
|
|
addLastSelectedCity,
|
|
|
resetCurrentCity,
|
|
|
getLocationAndSetCity,
|
|
|
- restoreCityFromStorage
|
|
|
+ restoreCityFromStorage,
|
|
|
+ getIPLocation
|
|
|
}
|
|
|
}, {
|
|
|
persist: {
|