|
|
|
@ -963,11 +963,97 @@ Page({ |
|
|
|
// 请求位置授权
|
|
|
|
requestLocationAuth() { |
|
|
|
const that = this; |
|
|
|
|
|
|
|
console.log('开始请求位置授权'); |
|
|
|
|
|
|
|
wx.showLoading({ |
|
|
|
title: '请求授权中...', |
|
|
|
mask: true |
|
|
|
}); |
|
|
|
|
|
|
|
// 添加超时机制,确保加载提示不会一直显示
|
|
|
|
const loadingTimeout = setTimeout(() => { |
|
|
|
wx.hideLoading(); |
|
|
|
console.error('位置授权请求超时'); |
|
|
|
wx.showToast({ |
|
|
|
title: '请求超时,请重试', |
|
|
|
icon: 'none' |
|
|
|
}); |
|
|
|
}, 10000); // 10秒超时
|
|
|
|
|
|
|
|
// 检查用户是否已经拒绝过授权
|
|
|
|
wx.getSetting({ |
|
|
|
success(res) { |
|
|
|
clearTimeout(loadingTimeout); |
|
|
|
if (res.authSetting['scope.userLocation'] === false) { |
|
|
|
// 用户已经拒绝过授权,直接引导到设置页面
|
|
|
|
wx.hideLoading(); |
|
|
|
wx.showModal({ |
|
|
|
title: '需要位置授权', |
|
|
|
content: '请在设置中开启位置授权,以便我们为您提供相关服务', |
|
|
|
showCancel: true, |
|
|
|
cancelText: '取消', |
|
|
|
confirmText: '去授权', |
|
|
|
success: (res) => { |
|
|
|
if (res.confirm) { |
|
|
|
// 打开设置页面让用户手动开启授权
|
|
|
|
wx.openSetting({ |
|
|
|
success: (settingRes) => { |
|
|
|
console.log('设置页面返回结果:', settingRes); |
|
|
|
if (settingRes.authSetting['scope.userLocation']) { |
|
|
|
// 用户在设置中开启了位置授权
|
|
|
|
that.setData({ hasLocationAuth: true }); |
|
|
|
wx.showToast({ |
|
|
|
title: '授权成功', |
|
|
|
icon: 'success', |
|
|
|
duration: 1500 |
|
|
|
}); |
|
|
|
// 从本地存储读取位置信息,不自动获取当前位置
|
|
|
|
const savedLocationInfo = wx.getStorageSync('locationInfo'); |
|
|
|
if (savedLocationInfo) { |
|
|
|
that.setData({ locationInfo: savedLocationInfo }); |
|
|
|
} else { |
|
|
|
// 如果本地没有位置信息,再获取当前位置
|
|
|
|
that.getUserLocation(); |
|
|
|
} |
|
|
|
} else { |
|
|
|
// 用户在设置中仍未开启位置授权
|
|
|
|
that.setData({ hasLocationAuth: false }); |
|
|
|
wx.showToast({ |
|
|
|
title: '您已拒绝位置授权', |
|
|
|
icon: 'none' |
|
|
|
}); |
|
|
|
} |
|
|
|
}, |
|
|
|
fail: (err) => { |
|
|
|
console.error('打开设置失败:', err); |
|
|
|
that.setData({ hasLocationAuth: false }); |
|
|
|
wx.showToast({ |
|
|
|
title: '打开设置失败', |
|
|
|
icon: 'none' |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
} else { |
|
|
|
// 用户点击了取消
|
|
|
|
that.setData({ hasLocationAuth: false }); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
} else { |
|
|
|
// 用户未拒绝过授权,调用 authorize
|
|
|
|
wx.authorize({ |
|
|
|
scope: 'scope.userLocation', |
|
|
|
success() { |
|
|
|
// 授权成功
|
|
|
|
clearTimeout(loadingTimeout); |
|
|
|
console.log('位置授权成功'); |
|
|
|
wx.hideLoading(); |
|
|
|
that.setData({ hasLocationAuth: true }); |
|
|
|
wx.showToast({ |
|
|
|
title: '授权成功', |
|
|
|
icon: 'success', |
|
|
|
duration: 1500 |
|
|
|
}); |
|
|
|
// 从本地存储读取位置信息,不自动获取当前位置
|
|
|
|
const savedLocationInfo = wx.getStorageSync('locationInfo'); |
|
|
|
if (savedLocationInfo) { |
|
|
|
@ -977,7 +1063,10 @@ Page({ |
|
|
|
that.getUserLocation(); |
|
|
|
} |
|
|
|
}, |
|
|
|
fail() { |
|
|
|
fail(err) { |
|
|
|
clearTimeout(loadingTimeout); |
|
|
|
console.log('位置授权失败:', err); |
|
|
|
wx.hideLoading(); |
|
|
|
// 授权失败,弹出模态框引导用户重新授权
|
|
|
|
wx.showModal({ |
|
|
|
title: '需要位置授权', |
|
|
|
@ -990,9 +1079,15 @@ Page({ |
|
|
|
// 打开设置页面让用户手动开启授权
|
|
|
|
wx.openSetting({ |
|
|
|
success: (settingRes) => { |
|
|
|
console.log('设置页面返回结果:', settingRes); |
|
|
|
if (settingRes.authSetting['scope.userLocation']) { |
|
|
|
// 用户在设置中开启了位置授权
|
|
|
|
that.setData({ hasLocationAuth: true }); |
|
|
|
wx.showToast({ |
|
|
|
title: '授权成功', |
|
|
|
icon: 'success', |
|
|
|
duration: 1500 |
|
|
|
}); |
|
|
|
// 从本地存储读取位置信息,不自动获取当前位置
|
|
|
|
const savedLocationInfo = wx.getStorageSync('locationInfo'); |
|
|
|
if (savedLocationInfo) { |
|
|
|
@ -1010,7 +1105,8 @@ Page({ |
|
|
|
}); |
|
|
|
} |
|
|
|
}, |
|
|
|
fail: () => { |
|
|
|
fail: (err) => { |
|
|
|
console.error('打开设置失败:', err); |
|
|
|
that.setData({ hasLocationAuth: false }); |
|
|
|
wx.showToast({ |
|
|
|
title: '打开设置失败', |
|
|
|
@ -1026,6 +1122,18 @@ Page({ |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
}, |
|
|
|
fail(err) { |
|
|
|
clearTimeout(loadingTimeout); |
|
|
|
console.error('获取设置失败:', err); |
|
|
|
wx.hideLoading(); |
|
|
|
wx.showToast({ |
|
|
|
title: '获取设置失败', |
|
|
|
icon: 'none' |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 获取用户当前位置
|
|
|
|
|