完善接口
This commit is contained in:
@ -179,10 +179,10 @@
|
||||
}
|
||||
const latitude = ref<number>(import.meta.env.VITE_DEFAULT_LATITUDE) // 纬度
|
||||
const longitude = ref<number>(import.meta.env.VITE_DEFAULT_LONGITUDE) // 经度
|
||||
// 存储经纬度
|
||||
uni.setStorageSync('latitude', latitude.value)
|
||||
uni.setStorageSync('longitude', longitude.value)
|
||||
|
||||
// 经纬度缓存过期处理(1小时)
|
||||
const LOCATION_EXPIRE_KEY = 'location_expire_time'
|
||||
const LOCATION_EXPIRE_MS = 60 * 60 * 1000 // 1小时
|
||||
const defaultCity = ref<string>(import.meta.env.VITE_DEFAULT_ADDRESS) // 默认城市
|
||||
const list = ref<Array<any>>([]) // 茶艺师列表
|
||||
const teaSpecialistName = ref<string>('') // 茶艺师名称
|
||||
@ -190,18 +190,8 @@
|
||||
const cityValue = ref<string>('')
|
||||
|
||||
onLoad(async () => {
|
||||
|
||||
// 授权获取地址
|
||||
await wxGetLocation((res) => {
|
||||
latitude.value = res.latitude
|
||||
longitude.value = res.longitude
|
||||
|
||||
// 授权后存储经纬度
|
||||
uni.setStorageSync('latitude', latitude.value)
|
||||
uni.setStorageSync('longitude', longitude.value)
|
||||
|
||||
Index.handleSearch()
|
||||
})
|
||||
// 检查缓存是否过期,超时则重新授权
|
||||
await Index.handleEnsureLocationAuth()
|
||||
|
||||
// 获取城市列表
|
||||
getCity({latitude: latitude.value, longitude: longitude.value}).then((res: any) => {
|
||||
@ -213,8 +203,8 @@
|
||||
})
|
||||
})
|
||||
// getDecorate({id: 1}).then((res: any) => {
|
||||
// const data = JSON.parse(res.data)
|
||||
// console.log('装修数据:', data)
|
||||
// const data = JSON.parse(res.data)
|
||||
// console.log('装修数据:', data)
|
||||
// })
|
||||
|
||||
// 等待授权完成后再加载数据
|
||||
@ -224,6 +214,52 @@
|
||||
})
|
||||
|
||||
const Index = {
|
||||
// 设置经纬度缓存
|
||||
handleSetLocationCache: (lat: number, lng: number) => {
|
||||
uni.setStorageSync('latitude', lat)
|
||||
uni.setStorageSync('longitude', lng)
|
||||
uni.setStorageSync(LOCATION_EXPIRE_KEY, Date.now() + LOCATION_EXPIRE_MS)
|
||||
},
|
||||
|
||||
// 检查经纬度缓存是否过期
|
||||
handleCheckLocationCache: () => {
|
||||
const expire = uni.getStorageSync(LOCATION_EXPIRE_KEY)
|
||||
if (expire && Date.now() > expire) {
|
||||
uni.removeStorageSync('latitude')
|
||||
uni.removeStorageSync('longitude')
|
||||
uni.removeStorageSync(LOCATION_EXPIRE_KEY)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
},
|
||||
|
||||
// 初始化经纬度
|
||||
handleEnsureLocationAuth: async () => {
|
||||
if (!Index.handleCheckLocationCache()) {
|
||||
// 超时,重新获取授权
|
||||
await wxGetLocation((res) => {
|
||||
latitude.value = res.latitude
|
||||
longitude.value = res.longitude
|
||||
Index.handleSetLocationCache(latitude.value, longitude.value)
|
||||
Index.handleSearch()
|
||||
})
|
||||
} else {
|
||||
const lat = uni.getStorageSync('latitude')
|
||||
const lng = uni.getStorageSync('longitude')
|
||||
if (lat && lng) {
|
||||
latitude.value = lat
|
||||
longitude.value = lng
|
||||
} else {
|
||||
await wxGetLocation((res) => {
|
||||
latitude.value = res.latitude
|
||||
longitude.value = res.longitude
|
||||
Index.handleSetLocationCache(latitude.value, longitude.value)
|
||||
Index.handleSearch()
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// 选择城市
|
||||
handleSelectCity: (e: any) => {
|
||||
cityValue.value = e.value
|
||||
|
||||
Reference in New Issue
Block a user