调试接口
This commit is contained in:
@ -5,8 +5,8 @@ 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_LAT = 31.230393 // 上海经度
|
||||
export const LOCATION_DEFAULT_LNG = 121.473629 // 上海纬度
|
||||
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
|
||||
@ -33,50 +33,112 @@ export function handleSetLocationCacheHooks(lat: number, lng: number) {
|
||||
|
||||
// 初始化经纬度
|
||||
export async function handleEnsureLocationAuthHooks() {
|
||||
// 1. 检查缓存
|
||||
if (handleCheckLocationCacheHooks()) {
|
||||
const SEARCH_CONFIRM_KEY = 'search_nearby_confirmed'
|
||||
const confirmed = uni.getStorageSync(SEARCH_CONFIRM_KEY)
|
||||
|
||||
// 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) => {
|
||||
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({})
|
||||
// 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 })
|
||||
}
|
||||
// 返回默认上海
|
||||
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)
|
||||
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 })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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 })
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
export function wxPay(opt) {
|
||||
export function wechatPay(opt) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let params;
|
||||
// #ifdef MP-WEIXIN
|
||||
@ -27,7 +27,7 @@ export function wxPay(opt) {
|
||||
resolve('success');
|
||||
},
|
||||
cancel: res => {
|
||||
resolve('fail');
|
||||
resolve('cancel');
|
||||
},
|
||||
fail: res => {
|
||||
resolve('fail');
|
||||
|
||||
Reference in New Issue
Block a user