小程序中弹起的授权框用户点了拒绝授权如何处理

     小程序中一些信息需要用户确认授权后我们才能获取到,如位置,用户信息等等,有些授权框用户点击拒绝授权后一段时间内不会再弹出,但我们又必须需要获取相应信息,这种状况如何解决呢?

    在调起授权弹窗方法的失败回调函数里使用 wx.getSetting()=>在其成功回调里获取到授权的状态=>再调用wx.openSetting()方法会打开调起过的授权项 取消的授权可以再次选确认授权(不一定非要完全和这个不走一样,可以按自己需求进行删减或增加)

下面以获取用户当前地理位置为例,贴上代码及效果。

wx.getLocation({
      type: 'wgs84',
      success(res) {
        that.userlatitude = res.latitude;
        that.userlongitude = res.longitude;
        wx.request({
          url: Url,
          data: {
            lat: that.userlatitude,
            lon: that.userlongitude
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success(res) {
            that.addressInfo = res.data.address_info;
            that.distance = res.data.distance;
            wx.setStorage({
              key: 'distance',
              data: that.distance
            });
            that.$apply();
            console.log(res);
          },
          fail() {
          }
        });
      },
      fail() {
        //客户拒绝授权时
        wx.getSetting({
          success(res) {
            var statu = res.authSetting;
            if (!statu['scope.userLocation']) {
              wx.showModal({
                title: '是否授权当前位置',
                content: '需要获取您的地理位置,请确认授权,否则配送服务将无法正常使用',
                success(tip) {
                  if (tip.confirm) {
                    wx.openSetting({
                      success(data) {
                        if (data.authSetting["scope.userLocation"] === true) {
                          wx.showToast({
                            title: '授权成功',
                            icon: 'success',
                            duration: 1000
                          })
                          //授权成功之后,再调用wx.getLocation获取地址信息
                          wx.getLocation({
                            type: 'wgs84',
                            success(res) {
                              that.userlatitude = res.latitude;
                              that.userlongitude = res.longitude;
                              wx.request({
                                url: Url,
                                data: {
                                  lat: that.userlatitude,
                                  lon: that.userlongitude
                                },
                                header: {
                                  'content-type': 'application/json' // 默认值
                                },
                                success(res) {
                                  that.addressInfo = res.data.address_info;
                                  that.distance = res.data.distance;
                                  wx.setStorage({
                                    key: 'distance',
                                    data: that.distance
                                  });
                                  that.$apply();
                                }
                              });
                            }
                          })
                        }
                      }
                    })
                  }
                }
              })
            }
          }
        });
      }
    });

效果如下:(这是我调获取用户位置授权 用户点了取消后的, 由于我前面还获取了用户信息,所以会有两项)

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值