Browse Source

修复位置授权按钮点击无响应的问题

Xfy
徐飞洋 2 weeks ago
parent
commit
8eedc51dbc
  1. 114
      pages/profile/index.js

114
pages/profile/index.js

@ -963,11 +963,97 @@ Page({
// 请求位置授权 // 请求位置授权
requestLocationAuth() { requestLocationAuth() {
const that = this; 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({ wx.authorize({
scope: 'scope.userLocation', scope: 'scope.userLocation',
success() { success() {
// 授权成功 clearTimeout(loadingTimeout);
console.log('位置授权成功');
wx.hideLoading();
that.setData({ hasLocationAuth: true }); that.setData({ hasLocationAuth: true });
wx.showToast({
title: '授权成功',
icon: 'success',
duration: 1500
});
// 从本地存储读取位置信息,不自动获取当前位置 // 从本地存储读取位置信息,不自动获取当前位置
const savedLocationInfo = wx.getStorageSync('locationInfo'); const savedLocationInfo = wx.getStorageSync('locationInfo');
if (savedLocationInfo) { if (savedLocationInfo) {
@ -977,7 +1063,10 @@ Page({
that.getUserLocation(); that.getUserLocation();
} }
}, },
fail() { fail(err) {
clearTimeout(loadingTimeout);
console.log('位置授权失败:', err);
wx.hideLoading();
// 授权失败,弹出模态框引导用户重新授权 // 授权失败,弹出模态框引导用户重新授权
wx.showModal({ wx.showModal({
title: '需要位置授权', title: '需要位置授权',
@ -990,9 +1079,15 @@ Page({
// 打开设置页面让用户手动开启授权 // 打开设置页面让用户手动开启授权
wx.openSetting({ wx.openSetting({
success: (settingRes) => { success: (settingRes) => {
console.log('设置页面返回结果:', settingRes);
if (settingRes.authSetting['scope.userLocation']) { if (settingRes.authSetting['scope.userLocation']) {
// 用户在设置中开启了位置授权 // 用户在设置中开启了位置授权
that.setData({ hasLocationAuth: true }); that.setData({ hasLocationAuth: true });
wx.showToast({
title: '授权成功',
icon: 'success',
duration: 1500
});
// 从本地存储读取位置信息,不自动获取当前位置 // 从本地存储读取位置信息,不自动获取当前位置
const savedLocationInfo = wx.getStorageSync('locationInfo'); const savedLocationInfo = wx.getStorageSync('locationInfo');
if (savedLocationInfo) { if (savedLocationInfo) {
@ -1010,7 +1105,8 @@ Page({
}); });
} }
}, },
fail: () => { fail: (err) => {
console.error('打开设置失败:', err);
that.setData({ hasLocationAuth: false }); that.setData({ hasLocationAuth: false });
wx.showToast({ wx.showToast({
title: '打开设置失败', title: '打开设置失败',
@ -1026,6 +1122,18 @@ Page({
}); });
} }
}); });
}
},
fail(err) {
clearTimeout(loadingTimeout);
console.error('获取设置失败:', err);
wx.hideLoading();
wx.showToast({
title: '获取设置失败',
icon: 'none'
});
}
});
}, },
// 获取用户当前位置 // 获取用户当前位置

Loading…
Cancel
Save