小程序-用户拒绝授权后提示用户手动授权

使用API

  • wx.getSetting 判断是否授权。
  • wx.authorize 提前向用户发起授权请求。
  • wx.openSetting 调起客户端小程序设置界面,返回用户设置的操作结果。
Page({
  onShow: function () {
    this.isAuto()
    if (this.isUserOpen) {
      // 用户手动打开的相机权限,返回培训计划。
      wx.reLaunch({
        url: '/pages/home/home',
      })
    }
  },
  isUserOpen: false,// 用户是否手动打开相机权限
  isAuto(){
    var that =this;
    // 获取用户是否开启用户授权相机
    wx.getSetting({
      success(res) {
        // 如果没有则获取授权
        if (!res.authSetting['scope.camera']) {
          wx.authorize({
            scope: 'scope.camera',
            success() {
              console.log('授权成功')
            },
            fail() {
              console.log('授权失败')
              wx.showModal({
                  title: '提示',
                  content: '若点击不授权,将无法使用学习功能',
                  cancelText:'不授权',
                  cancelColor:'#999',
                  confirmText:'授权',
                  confirmColor:'#f94218',
                  success(res) {
                    if (res.confirm) {
                      wx.openSetting({
                          success(res) {
                            console.log(res.authSetting)
                            if(res.authSetting['scope.camera']){
                              console.log('用户手动授权成功。')
                              that.isUserOpen = true;
                            }
                          }
                        })                                
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                      wx.navigateBack({
                        delta: 1
                      })
                    }
                  }
                })
            }
          })
        } else {
          console.log('用户已授权,继续执行')
        }
      }
    })
  },
})

你可能感兴趣的:(小程序-用户拒绝授权后提示用户手动授权)