From 8eedc51dbc64869f3dd96b57c87372f3785967aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Sun, 22 Feb 2026 16:27:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BD=8D=E7=BD=AE=E6=8E=88?= =?UTF-8?q?=E6=9D=83=E6=8C=89=E9=92=AE=E7=82=B9=E5=87=BB=E6=97=A0=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/profile/index.js | 208 +++++++++++++++++++++++++++++++---------- 1 file changed, 158 insertions(+), 50 deletions(-) diff --git a/pages/profile/index.js b/pages/profile/index.js index ab6dfdd..54a1f41 100644 --- a/pages/profile/index.js +++ b/pages/profile/index.js @@ -963,66 +963,174 @@ Page({ // 请求位置授权 requestLocationAuth() { const that = this; - wx.authorize({ - scope: 'scope.userLocation', - success() { - // 授权成功 - that.setData({ hasLocationAuth: true }); - // 从本地存储读取位置信息,不自动获取当前位置 - const savedLocationInfo = wx.getStorageSync('locationInfo'); - if (savedLocationInfo) { - that.setData({ locationInfo: savedLocationInfo }); - } else { - // 如果本地没有位置信息,再获取当前位置 - that.getUserLocation(); - } - }, - fail() { - // 授权失败,弹出模态框引导用户重新授权 - wx.showModal({ - title: '需要位置授权', - content: '请在设置中开启位置授权,以便我们为您提供相关服务', - showCancel: true, - cancelText: '取消', - confirmText: '去授权', - success: (res) => { - if (res.confirm) { - // 打开设置页面让用户手动开启授权 - wx.openSetting({ - success: (settingRes) => { - if (settingRes.authSetting['scope.userLocation']) { - // 用户在设置中开启了位置授权 - that.setData({ hasLocationAuth: true }); - // 从本地存储读取位置信息,不自动获取当前位置 - const savedLocationInfo = wx.getStorageSync('locationInfo'); - if (savedLocationInfo) { - that.setData({ locationInfo: savedLocationInfo }); + + 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.getUserLocation(); + // 用户在设置中仍未开启位置授权 + that.setData({ hasLocationAuth: false }); + wx.showToast({ + title: '您已拒绝位置授权', + icon: 'none' + }); } - } else { - // 用户在设置中仍未开启位置授权 + }, + fail: (err) => { + console.error('打开设置失败:', err); that.setData({ hasLocationAuth: false }); wx.showToast({ - title: '您已拒绝位置授权', + title: '打开设置失败', icon: 'none' }); } - }, - fail: () => { - 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) { + that.setData({ locationInfo: savedLocationInfo }); + } else { + // 如果本地没有位置信息,再获取当前位置 + that.getUserLocation(); + } + }, + fail(err) { + clearTimeout(loadingTimeout); + console.log('位置授权失败:', err); + 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 { - // 用户点击了取消 - that.setData({ hasLocationAuth: false }); } - } + }); + } + }, + fail(err) { + clearTimeout(loadingTimeout); + console.error('获取设置失败:', err); + wx.hideLoading(); + wx.showToast({ + title: '获取设置失败', + icon: 'none' }); } });