微信重新授权

2019-01-19  本文已影响70人  holy俊辉

微信授权

问题

小程序有时需要调用一些权限才能使用。比如用户信息,获取地理位置,获取录音等,这时就会涉及用户体验等
有些授权并不是刚需的,所以也给出未授权的格式就可以了,但是有些授权是刚需的,也就需要用户打开授权。
不管是不是刚需,如果需要再重新授权就比较麻烦了。所以一下就是解决如果用户点击拒绝的情况下,某些功能需要再次授权


image.png

wx.openSetting()
调起客户端小程序设置页面,返回用户设置的操作结果,设置页面只会出现小程序已经向用户请求过得权限

      wx.getSetting({
      //getSetting不管授权还是未授权都会进入success 但是返回的值会是false 
        success(res) {
          if (!res.authSetting['scope.record']) { // 未授权录音
            wx.authorize({
              scope: 'scope.record',
              success(res) {
                that.startRecording();// 这个是调用成功后的函数
              },
              fail(res) {
                // INTERACTION.showToast('请授权录音功能');
                that.openShowSetting() //再次调用openSetting去打开设置

              }
            });
          }else{
          // 授权录音
            that.startRecording()
          }
        }
      });


  openShowSetting:function(){
      var that = this;
      wx.showModal({
        title: '提示',
        content: '小程序需要获取录音权限才能使用录音,请点击确认',
        success:function(res){
          console.log("openseting",res)
          if(res.confirm){
            wx.openSetting({
              success: (res) => {
                console.log("openssssssss", res)
                var userInfo = res.authSetting['scope.record'];
                if (!userInfo) {
                  wx.authorize({
                    scope: 'scope.record',
                    success(res) {
                      console.log("111", res)
                      that.startRecording();
                    },
                    fail(res) {
                      // INTERACTION.showToast('请授权录音功能');
                      that.openShowSetting()
                    }
                  });
                } else {
                  that.startRecording()
                }
              }
            })
          }else{
            console.log("点击取消了")
          }
        
        }
      })
    },

当出现授权页面用户点击拒绝之后,刚需的直接给用户提示,并提醒用户开启权限,否则功能不能用,使用openSeting 就会出现小程序的设置页面
下面是需要授权的scope


image.png
上一篇下一篇

猜你喜欢

热点阅读