微信小程序在用户拒绝授权后无法使用wx.opensetting再次获取定位授权

javascript 复制代码
 _getLocation(callback,errCall) {
      if (app.globalData.isLocationIng) {
        setTimeout(() => {
          this._getLocation(callback);
        }, 100);
        return;
      }
      app.globalData.isLocationIng = true;
      wx.getSetting({
        success: (resData) => {
          // 检查永久拒绝状态
          if (resData.authSetting["scope.userLocation"] === false) {
            app.globalData.isLocationIng = false;
            errCall && errCall({ type: 'permanent_deny' });
            return;
          }

          if (!resData.authSetting["scope.userLocation"]) {
            wx.authorize({
              scope: "scope.userLocation",
              success: () => this._getlocaldata(callback, errCall),
              fail: () => {
                // 新增:首次拒绝处理
                if (!this.data.hasTriggeredSetting) {
                  this.setData({ hasTriggeredSetting: true });
                  wx.showModal({
                    title: '位置信息授权申请',
                    content: 'xxx为了给您更好的xx服务,我们需要您的授权',
                    confirmText: '去授权',
                    cancelText: '残忍拒绝',
                    success: (res) => {
                      if (res.confirm) {
                        wx.openSetting({
                          success: (settingdata) => {
                            if (settingdata.authSetting["scope.userLocation"]) {
                              this._getlocaldata(callback);
                            } else {
                              app.globalData.isLocationIng = false;
                              errCall && errCall({ type: 'permanent_deny' });
                            }
                          }
                        });
                      } else {
                        app.globalData.isLocationIng = false;
                        errCall && errCall({ type: 'permanent_deny' });
                      }
                    }
                  });
                } else {
                  app.globalData.isLocationIng = false;
                  errCall && errCall({ type: 'permanent_deny' });
                }
              }
            });
          } else {
            this._getlocaldata(callback, errCall);
          }
        },
        fail: (msgData) => {
          console.log(msgData);
          app.globalData.isLocationIng = false;
        }
      });
    },
    _getlocaldata(callback,errCall) {
      if (wx.getStorageSync("address-point")) {
        let point = wx.getStorageSync("address-point").point;
        this._setaddressData({
          lat: point.lat,
          lng: point.lng,
          from: 'storage'
        },
        callback
        );
      } else {
        wx.getLocation({
          type: "gcj02",
          isHighAccuracy: true,
          success: (res) => {
            const latitude = res.latitude;
            const longitude = res.longitude;
            // const speed = res.speed;
            // const accuracy = res.accuracy;
            this._setaddressData({
              lat: latitude,
              lng: longitude,
            },
            callback
            );
          },
          fail: (err) => {
            if (err.errMsg === "getLocation:fail auth deny") {
              console.log("当初用户拒绝,再次发起授权");
              //此方法无用,因为微信不允许在异步回调中直接调用 wx.openSetting,必须由用户主动触发(如点击按钮),所以直接在_getLocation方法里进行处理
              wx.openSetting({
                success: (settingdata) => {
                  if (settingdata.authSetting["scope.userLocation"]) {
                    wx.showToast({
                      icon: "none",
                      title: "获取权限成功",
                      duration: 1500,
                    });
                    this._getlocaldata(callback);
                  } else {
                    app.globalData.isLocationIng = false;
                    wx.showToast({
                      icon: "none",
                      title: "获取权限失败,定位需要用户授权",
                      duration: 1500,
                    });
                  }
                },
                fail:(err)=>{
                  try {
                    this.is_errCall(errCall)
                  } catch (error) {
                    console.log(error)
                  }
                }
              });
            }else{
              app.globalData.isLocationIng = false;
            }
          },
        });
      }
    },

在myhehavior组件里无法在用户已经拒绝授权后再调用wx.opensetting()。wx.opensetting()必须由用户主动触发(如点击按钮),不能在wx.getLocation 的fail 回调中直接调用。这是为了防止开发者强制跳转设置页干扰用户,所以我直接在getlocation里直接增加二次确认弹窗,为了防止用户第一次拒绝定位授权。

相关推荐
00后程序员张1 小时前
Web 前端工具全流程指南 从开发到调试的完整生态体系
android·前端·ios·小程序·uni-app·iphone·webview
说私域4 小时前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的零售流量重构研究
人工智能·小程序·开源
鹧鸪云光伏与储能软件开发6 小时前
光伏开发小程序:快速获客,成交项目更迅速
大数据·微信小程序·小程序·光伏
晨旭缘8 小时前
解决小程序滚动穿透问题
小程序
游戏开发爱好者811 小时前
iOS 崩溃日志分析工具全指南,多工具协同构建稳定性分析体系
android·macos·ios·小程序·uni-app·cocoa·iphone
00后程序员张21 小时前
如何提高 IPA 安全性 多工具组合打造可复用的 iOS 加固与反编译防护体系(IPA 安全 iOS 加固 无源码混淆 Ipa Guard 实战)
android·安全·ios·小程序·uni-app·iphone·webview
游戏开发爱好者81 天前
Fiddler抓包实战教程 从安装配置到代理设置,详解Fiddler使用方法与调试技巧(HTTPHTTPS全面指南)
前端·测试工具·小程序·https·fiddler·uni-app·webview
sen_shan1 天前
《微信小程序》第五章:登录-API封装
微信小程序·小程序
2501_916007471 天前
Fastlane 结合 开心上架 命令行版本实现跨平台上传发布 iOS App
android·ios·小程序·https·uni-app·iphone·webview
00后程序员张1 天前
iOS 26 内存占用监控 多工具协同下的性能稳定性分析实战
android·macos·ios·小程序·uni-app·cocoa·iphone