完善功能
This commit is contained in:
@ -1,19 +1,23 @@
|
||||
const LOCATION_EXPIRE_MS = 30 * 24 * 60 * 60 * 1000 // 定位缓存30天
|
||||
const LOCATION_DENY_INTERVAL = 60 * 60 * 1000 // 未授权定位,弹窗间隔1小时
|
||||
import { getLocationToCity } from '@/api/tea-room'
|
||||
|
||||
const LOCATION_EXPIRE_MS = 2 * 60 * 1000 // 定位缓存30天
|
||||
const LOCATION_DENY_INTERVAL = 1 * 60 * 1000 // 未授权定位,弹窗间隔1小时
|
||||
|
||||
export const LOCATION_EXPIRE_KEY = 'location_expire_time' // 定位缓存KEY
|
||||
export const LOCATION_DEFAULT_CITY = '上海市' // 默认城市
|
||||
export const LOCATION_DEFAULT_LAT = 34.757975 // 上海经度
|
||||
export const LOCATION_DEFAULT_LNG = 113.665412 // 上海纬度
|
||||
export const LOCATION_DEFAULT_LAT = 31.230393 // 上海经度
|
||||
export const LOCATION_DEFAULT_LNG = 121.473629 // 上海纬度
|
||||
export const LOCATION_DENY_TIME_KEY = 'location_deny_time' // 未授权定位,重新授权时间KEY
|
||||
export const LOCATION_CITY_KEY = 'city' // 城市缓存KEY
|
||||
export const LOCATION_LAT_KEY = 'latitude' // 城市缓存KEY
|
||||
export const LOCATION_LNG_KEY = 'longitude' // 城市缓存KEY
|
||||
|
||||
// 检查过期时间
|
||||
export function handleCheckLocationCacheHooks() {
|
||||
const expire = uni.getStorageSync(LOCATION_EXPIRE_KEY)
|
||||
if (expire && Date.now() > expire) {
|
||||
uni.removeStorageSync('latitude')
|
||||
uni.removeStorageSync('longitude')
|
||||
uni.removeStorageSync(LOCATION_LAT_KEY)
|
||||
uni.removeStorageSync(LOCATION_LNG_KEY)
|
||||
uni.removeStorageSync(LOCATION_EXPIRE_KEY)
|
||||
return false
|
||||
}
|
||||
@ -22,8 +26,8 @@ export function handleCheckLocationCacheHooks() {
|
||||
|
||||
// 设置经纬度缓存
|
||||
export function handleSetLocationCacheHooks(lat: number, lng: number) {
|
||||
uni.setStorageSync('latitude', lat)
|
||||
uni.setStorageSync('longitude', lng)
|
||||
uni.setStorageSync(LOCATION_LAT_KEY, lat)
|
||||
uni.setStorageSync(LOCATION_LNG_KEY, lng)
|
||||
uni.setStorageSync(LOCATION_EXPIRE_KEY, Date.now() + LOCATION_EXPIRE_MS)
|
||||
}
|
||||
|
||||
@ -31,13 +35,13 @@ export function handleSetLocationCacheHooks(lat: number, lng: number) {
|
||||
export async function handleEnsureLocationAuthHooks() {
|
||||
// 1. 检查缓存
|
||||
if (handleCheckLocationCacheHooks()) {
|
||||
const lat = uni.getStorageSync('latitude')
|
||||
const lng = uni.getStorageSync('longitude')
|
||||
const lat = uni.getStorageSync(LOCATION_LAT_KEY)
|
||||
const lng = uni.getStorageSync(LOCATION_LNG_KEY)
|
||||
if (lat && lng) return { lat, lng }
|
||||
}
|
||||
|
||||
// 2. 获取定位
|
||||
return new Promise<{ lat: number, lng: number }>((resolve) => {
|
||||
return new Promise<{ lat: number, lng: number, }>((resolve) => {
|
||||
uni.authorize({
|
||||
scope: 'scope.userLocation',
|
||||
success() {
|
||||
@ -45,12 +49,12 @@ export async function handleEnsureLocationAuthHooks() {
|
||||
type: 'gcj02',
|
||||
success(res) {
|
||||
handleSetLocationCacheHooks(res.latitude, res.longitude)
|
||||
resolve({ lat: res.latitude, lng: res.longitude })
|
||||
resolve({ lat: res.latitude, lng: res.longitude})
|
||||
},
|
||||
fail() {
|
||||
// 定位失败,返回默认上海
|
||||
handleSetLocationCacheHooks(LOCATION_DEFAULT_LAT, LOCATION_DEFAULT_LNG)
|
||||
resolve({ lat: LOCATION_DEFAULT_LAT, lng: LOCATION_DEFAULT_LNG })
|
||||
resolve({ lat: LOCATION_DEFAULT_LAT, lng: LOCATION_DEFAULT_LNG})
|
||||
}
|
||||
})
|
||||
},
|
||||
@ -151,6 +155,32 @@ export function setLocationCity(city: string) {
|
||||
uni.setStorageSync(LOCATION_CITY_KEY, city)
|
||||
}
|
||||
|
||||
/**
|
||||
* 经纬度转换为城市
|
||||
* @returns
|
||||
*/
|
||||
export async function handleGetLocationCity(lat: number, lng: number) {
|
||||
try {
|
||||
console.log("🚀 ~ handleGetLocationCity ~ res:", lat, lng)
|
||||
|
||||
const res = await getLocationToCity({ latitude: lat, longitude: lng })
|
||||
if (res.message == "Success") {
|
||||
const params = {
|
||||
latitude: res.result.location.lat,
|
||||
longitude: res.result.location.lng,
|
||||
city: res.result.ad_info.city
|
||||
}
|
||||
setLocationCity(params.city)
|
||||
// uni.$emit('locationUpdate', params) // 通知页面
|
||||
return params
|
||||
} else {
|
||||
setLocationCity(LOCATION_DEFAULT_CITY)
|
||||
}
|
||||
} catch (error) {
|
||||
setLocationCity(LOCATION_DEFAULT_CITY)
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否需要弹授权框
|
||||
function shouldShowAuthModal() {
|
||||
const lastDeny = uni.getStorageSync(LOCATION_DENY_TIME_KEY)
|
||||
|
||||
Reference in New Issue
Block a user