微信小程序开发-用户拒绝授权情况处理

微信小程序开发-用户拒绝授权情况处理


用户拒绝授权情况进行处理

微信小程序拒绝授权(以拒绝语音权限为例 scope.record)


  // 获取用户已经授予了哪些权限
  getSetting() {
    return new Promise((resolve, reject) => {
      wx.getSetting({
        success: res => {
          console.log(res)
          resolve(res)
        }
      })
    })
  },   

  openingMethod() {
    //获取用户授权情况
    this.getSetting().then((res) => {
      // 判断用户是否授权了此权限功能 !res.authSetting['scope.record']为未授权
      if (!res.authSetting['scope.record']) {
        this.authorize().then((res) => {
          //授权成功调用方法
        }).catch((err) => {
          //授权失败调用方法
          this.openShowModal();
        })
      } else {
        //用户如果已授权时的处理
      }
    })
  },

  // 弹出模态框提示用户是否要去设置页授权
  openShowModal() {
    wx.showModal({
      title: '检测到您没有打开录音权限,是否前往设置打开?',
      success: (res) => {
        if (res.confirm) {
          console.log('用户点击确定')
          this.openSetting() // 打开设置页面          
        } else if (res.cancel) {
          wx.showToast({
            title: '授权失败',
            icon: 'none',
            duration: 1000
          })
          console.log('用户点击取消')
        }
      }
    })
  },

  //打开设置,引导用户授权 scope.record语音授权方法
  openSetting() {
    wx.openSetting({
      success: (res) => {
        console.log(res.authSetting)
        if (!res.authSetting['scope.record']) {
          wx.showToast({
            title: '授权失败',
            icon: 'none',
            duration: 1000
          })
        } else {
          //用户回调授权时的处理
        }
      }
    })
  },

  // 发起首次授权请求
  authorize() {
    return new Promise((resolve, reject) => {
      wx.authorize({
        scope: 'scope.record',
        success: () => {
          resolve()
        },
        fail: res => { //这里是用户拒绝授权后的回调
          console.log('拒绝授权')
          reject()
        }
      })
    })
  },

通过调起授权状态,查看所有授权状态。之后判断出语音的授权状态,如果第一次调用授权走authorize方法。如果用户已经拒绝授权则走openShowModal方法调用起wx.showModal提示用户引导用户打开设置设置权限。之后通过方法回调回来状态,进行之后的判断和处理。

猜你喜欢

转载自www.cnblogs.com/molimobai/p/12059757.html