地址改为初始版本授权提示,不需要弹出是否搜索附近茶室
This commit is contained in:
@ -4,13 +4,17 @@ const LOCATION_EXPIRE_MS = 30 * 24 * 60 * 60 * 1000 // 定位缓存30天
|
||||
const LOCATION_DENY_INTERVAL = 60 * 60 * 1000 // 未授权定位,弹窗间隔1小时
|
||||
|
||||
export const LOCATION_EXPIRE_KEY = 'location_expire_time' // 定位缓存KEY
|
||||
export const LOCATION_DEFAULT_CITY = '上海市' // 默认城市
|
||||
export const LOCATION_DEFAULT_LNG = 121.473629 // 上海经度
|
||||
export const LOCATION_DEFAULT_LAT = 31.230393 // 上海纬度
|
||||
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 const LOCATION_CURRENT = 'current_city' // 城市缓存KEY
|
||||
|
||||
export const LOCATION_DEFAULT_CITY = '上海市' // 默认城市
|
||||
export const LOCATION_DEFAULT_LNG = 121.473629 // 上海经度
|
||||
export const LOCATION_DEFAULT_LAT = 31.230393 // 上海纬度
|
||||
|
||||
|
||||
|
||||
// 检查过期时间
|
||||
export function handleCheckLocationCacheHooks() {
|
||||
@ -31,121 +35,171 @@ export function handleSetLocationCacheHooks(lat: number, lng: number) {
|
||||
uni.setStorageSync(LOCATION_EXPIRE_KEY, Date.now() + LOCATION_EXPIRE_MS)
|
||||
}
|
||||
|
||||
// 初始化经纬度
|
||||
export async function handleEnsureLocationAuthHooks() {
|
||||
const SEARCH_CONFIRM_KEY = 'search_nearby_confirmed'
|
||||
const confirmed = uni.getStorageSync(SEARCH_CONFIRM_KEY)
|
||||
// TODO VERSION2: 在获取经纬度之前还有弹出是否搜索附近茶室?-初始化经纬度
|
||||
// export async function handleEnsureLocationAuthHooks() {
|
||||
// const SEARCH_CONFIRM_KEY = 'search_nearby_confirmed'
|
||||
// const confirmed = uni.getStorageSync(SEARCH_CONFIRM_KEY)
|
||||
|
||||
// 1. 检查缓存和是否已确认搜索附近茶室
|
||||
if (confirmed && handleCheckLocationCacheHooks()) {
|
||||
// // 1. 检查缓存和是否已确认搜索附近茶室
|
||||
// if (confirmed && handleCheckLocationCacheHooks()) {
|
||||
// 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) => {
|
||||
// if (confirmed) {
|
||||
// // 已确认过,直接走授权
|
||||
// uni.authorize({
|
||||
// scope: 'scope.userLocation',
|
||||
// success() {
|
||||
// uni.getLocation({
|
||||
// type: 'gcj02',
|
||||
// success(res) {
|
||||
// handleSetLocationCacheHooks(res.latitude, 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 })
|
||||
// }
|
||||
// })
|
||||
// },
|
||||
// fail() {
|
||||
// // 用户拒绝授权
|
||||
// if (shouldShowAuthModal()) {
|
||||
// uni.setStorageSync(LOCATION_DENY_TIME_KEY, Date.now())
|
||||
// uni.removeStorageSync(SEARCH_CONFIRM_KEY) // 授权失败,下次启动继续弹窗
|
||||
// uni.showModal({
|
||||
// title: '提示',
|
||||
// content: '需要获取您的地理位置,请授权定位服务',
|
||||
// showCancel: false,
|
||||
// success: () => {
|
||||
// uni.openSetting({})
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
// handleSetLocationCacheHooks(LOCATION_DEFAULT_LAT, LOCATION_DEFAULT_LNG)
|
||||
// resolve({ lat: LOCATION_DEFAULT_LAT, lng: LOCATION_DEFAULT_LNG })
|
||||
// }
|
||||
// })
|
||||
// } else {
|
||||
// // 未确认,弹窗
|
||||
// uni.showModal({
|
||||
// title: '提示',
|
||||
// content: '是否搜索附近茶室?',
|
||||
// showCancel: true,
|
||||
// confirmText: '是',
|
||||
// cancelText: '否',
|
||||
// success: (modalRes) => {
|
||||
// console.log("🚀 ~ handleEnsureLocationAuthHooks ~ modalRes:", modalRes)
|
||||
// if (modalRes.confirm) {
|
||||
// // 用户点击“是”,标记已确认,下次不再弹窗
|
||||
// uni.setStorageSync(SEARCH_CONFIRM_KEY, true)
|
||||
// uni.authorize({
|
||||
// scope: 'scope.userLocation',
|
||||
// success() {
|
||||
// uni.getLocation({
|
||||
// type: 'gcj02',
|
||||
// success(res) {
|
||||
// handleSetLocationCacheHooks(res.latitude, res.longitude)
|
||||
// console.log("🚀 ~ handleEnsureLocationAuthHooks ~ LOCATION_DEFAULT_LAT:", LOCATION_DEFAULT_LAT, LOCATION_DEFAULT_LNG)
|
||||
|
||||
// resolve({ lat: res.latitude, lng: res.longitude })
|
||||
// },
|
||||
// fail() {
|
||||
// handleSetLocationCacheHooks(LOCATION_DEFAULT_LAT, LOCATION_DEFAULT_LNG)
|
||||
// resolve({ lat: LOCATION_DEFAULT_LAT, lng: LOCATION_DEFAULT_LNG })
|
||||
// }
|
||||
// })
|
||||
// },
|
||||
// fail() {
|
||||
// // 用户拒绝授权
|
||||
// if (shouldShowAuthModal()) {
|
||||
// uni.setStorageSync(LOCATION_DENY_TIME_KEY, Date.now())
|
||||
// uni.removeStorageSync(SEARCH_CONFIRM_KEY) // 授权失败,下次启动继续弹窗
|
||||
// uni.showModal({
|
||||
// title: '提示',
|
||||
// content: '需要获取您的地理位置,请授权定位服务',
|
||||
// showCancel: false,
|
||||
// success: () => {
|
||||
// uni.openSetting({})
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
// handleSetLocationCacheHooks(LOCATION_DEFAULT_LAT, LOCATION_DEFAULT_LNG)
|
||||
// console.log("🚀 ~ handleEnsureLocationAuthHooks ~ LOCATION_DEFAULT_LAT:", LOCATION_DEFAULT_LAT, LOCATION_DEFAULT_LNG)
|
||||
|
||||
// resolve({ lat: LOCATION_DEFAULT_LAT, lng: LOCATION_DEFAULT_LNG })
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
|
||||
// if (modalRes.cancel) {
|
||||
// // 用户点击“否”,直接返回默认上海
|
||||
// handleSetLocationCacheHooks(LOCATION_DEFAULT_LAT, LOCATION_DEFAULT_LNG)
|
||||
// console.log("🚀 ~ handleEnsureLocationAuthHooks ~ LOCATION_DEFAULT_LAT:", LOCATION_DEFAULT_LAT, LOCATION_DEFAULT_LNG)
|
||||
|
||||
// resolve({ lat: LOCATION_DEFAULT_LAT, lng: LOCATION_DEFAULT_LNG })
|
||||
// }
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
|
||||
// VERSION1: 初始化经纬度
|
||||
export async function handleEnsureLocationAuthHooks() {
|
||||
// 1. 检查缓存
|
||||
if (handleCheckLocationCacheHooks()) {
|
||||
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) => {
|
||||
if (confirmed) {
|
||||
// 已确认过,直接走授权
|
||||
uni.authorize({
|
||||
scope: 'scope.userLocation',
|
||||
success() {
|
||||
uni.getLocation({
|
||||
type: 'gcj02',
|
||||
success(res) {
|
||||
handleSetLocationCacheHooks(res.latitude, 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 })
|
||||
// 2. 获取定位
|
||||
return new Promise<{ lat: number, lng: number, }>((resolve) => {
|
||||
uni.authorize({
|
||||
scope: 'scope.userLocation',
|
||||
success() {
|
||||
uni.getLocation({
|
||||
type: 'gcj02',
|
||||
success(res) {
|
||||
handleSetLocationCacheHooks(res.latitude, 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})
|
||||
}
|
||||
})
|
||||
},
|
||||
fail() {
|
||||
// 用户拒绝授权
|
||||
if (shouldShowAuthModal()) {
|
||||
uni.setStorageSync(LOCATION_DENY_TIME_KEY, Date.now())
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '需要获取您的地理位置,请授权定位服务',
|
||||
showCancel: false,
|
||||
success: () => {
|
||||
// 可引导用户去设置页面
|
||||
uni.openSetting({})
|
||||
}
|
||||
})
|
||||
},
|
||||
fail() {
|
||||
// 用户拒绝授权
|
||||
if (shouldShowAuthModal()) {
|
||||
uni.setStorageSync(LOCATION_DENY_TIME_KEY, Date.now())
|
||||
uni.removeStorageSync(SEARCH_CONFIRM_KEY) // 授权失败,下次启动继续弹窗
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '需要获取您的地理位置,请授权定位服务',
|
||||
showCancel: false,
|
||||
success: () => {
|
||||
uni.openSetting({})
|
||||
}
|
||||
})
|
||||
}
|
||||
handleSetLocationCacheHooks(LOCATION_DEFAULT_LAT, LOCATION_DEFAULT_LNG)
|
||||
resolve({ lat: LOCATION_DEFAULT_LAT, lng: LOCATION_DEFAULT_LNG })
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// 未确认,弹窗
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '是否搜索附近茶室?',
|
||||
showCancel: true,
|
||||
confirmText: '是',
|
||||
cancelText: '否',
|
||||
success: (modalRes) => {
|
||||
console.log("🚀 ~ handleEnsureLocationAuthHooks ~ modalRes:", modalRes)
|
||||
if (modalRes.confirm) {
|
||||
// 用户点击“是”,标记已确认,下次不再弹窗
|
||||
uni.setStorageSync(SEARCH_CONFIRM_KEY, true)
|
||||
uni.authorize({
|
||||
scope: 'scope.userLocation',
|
||||
success() {
|
||||
uni.getLocation({
|
||||
type: 'gcj02',
|
||||
success(res) {
|
||||
handleSetLocationCacheHooks(res.latitude, res.longitude)
|
||||
console.log("🚀 ~ handleEnsureLocationAuthHooks ~ LOCATION_DEFAULT_LAT:", LOCATION_DEFAULT_LAT, LOCATION_DEFAULT_LNG)
|
||||
|
||||
resolve({ lat: res.latitude, lng: res.longitude })
|
||||
},
|
||||
fail() {
|
||||
handleSetLocationCacheHooks(LOCATION_DEFAULT_LAT, LOCATION_DEFAULT_LNG)
|
||||
resolve({ lat: LOCATION_DEFAULT_LAT, lng: LOCATION_DEFAULT_LNG })
|
||||
}
|
||||
})
|
||||
},
|
||||
fail() {
|
||||
// 用户拒绝授权
|
||||
if (shouldShowAuthModal()) {
|
||||
uni.setStorageSync(LOCATION_DENY_TIME_KEY, Date.now())
|
||||
uni.removeStorageSync(SEARCH_CONFIRM_KEY) // 授权失败,下次启动继续弹窗
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
content: '需要获取您的地理位置,请授权定位服务',
|
||||
showCancel: false,
|
||||
success: () => {
|
||||
uni.openSetting({})
|
||||
}
|
||||
})
|
||||
}
|
||||
handleSetLocationCacheHooks(LOCATION_DEFAULT_LAT, LOCATION_DEFAULT_LNG)
|
||||
console.log("🚀 ~ handleEnsureLocationAuthHooks ~ LOCATION_DEFAULT_LAT:", LOCATION_DEFAULT_LAT, LOCATION_DEFAULT_LNG)
|
||||
|
||||
resolve({ lat: LOCATION_DEFAULT_LAT, lng: LOCATION_DEFAULT_LNG })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (modalRes.cancel) {
|
||||
// 用户点击“否”,直接返回默认上海
|
||||
handleSetLocationCacheHooks(LOCATION_DEFAULT_LAT, LOCATION_DEFAULT_LNG)
|
||||
console.log("🚀 ~ handleEnsureLocationAuthHooks ~ LOCATION_DEFAULT_LAT:", LOCATION_DEFAULT_LAT, LOCATION_DEFAULT_LNG)
|
||||
|
||||
resolve({ lat: LOCATION_DEFAULT_LAT, lng: LOCATION_DEFAULT_LNG })
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
// 返回默认上海
|
||||
handleSetLocationCacheHooks(LOCATION_DEFAULT_LAT, LOCATION_DEFAULT_LNG)
|
||||
resolve({ lat: LOCATION_DEFAULT_LAT, lng: LOCATION_DEFAULT_LNG })
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查并弹窗授权地理位置(每小时弹一次,授权后自动获取定位)
|
||||
* 返回 Promise<boolean>,true 表示已授权,false 表示未授权
|
||||
@ -237,6 +291,7 @@ export async function handleGetLocationCity(lat: number, lng: number) {
|
||||
city: res.result.ad_info.city
|
||||
}
|
||||
setLocationCity(params.city)
|
||||
uni.setStorageSync(LOCATION_CURRENT, params)
|
||||
// uni.$emit('locationUpdate', params) // 通知页面
|
||||
return params
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user