wx.authorize在没有某一权限的情况下调用仍然会走失败的方法
发布于 6 年前 作者 zhuwei 2807 次浏览 来自 官方Issues

wx.getSetting({

success(res) {

console.log(res)

if (!res.authSetting[‘scope.userLocation’]) {

console.log(11111)

wx.authorize({

scope: ‘scope.userLocation’,

success() {

console.log(111)

},

fail(err){

console.log(err)

}

})

}else{

that.fnGetLocation()

}

}

})

比如说我要定位,但是用户开始拒绝了授权定位,我想用户再次授权调用上述一系列方法并不会成功,wx.authorize仍然会走fail的方法,无法通过它改变授权状态

3 回复

//判断是否有相应权限,true则直接执行,false需要跳转设置页

  //name取值例:

  /*scope.userInfo wx.getUserInfo 用户信息

    scope.userLocation wx.getLocation, wx.chooseLocation 地理位置

    scope.address wx.chooseAddress 通讯地址

    scope.invoiceTitle wx.chooseInvoiceTitle 发票抬头

    scope.invoice wx.chooseInvoice 获取发票

    scope.werun wx.getWeRunData 微信运动步数

    scope.record wx.startRecord 录音功能

    scope.writePhotosAlbum wx.saveImageToPhotosAlbum, wx.saveVideoToPhotosAlbum 保存到相册

    scope.camera <camera /> 组件 摄像头

  */

  getAuth:function(name,callback){

    wx.getSetting({

      success(res) {

        if (res.authSetting[‘scope.’ + name] != undefined && res.authSetting[‘scope.’ + name] != true) {

          //用户主动取消过

          callback(false);

        } else if (res.authSetting[‘scope.’ + name] == undefined) {

          //第一次向用户获取

          callback(true);

        } else {

          //用户已授权

          callback(true);

        }

      }

    })

  },

//以上是我将所有权限进行了封装在app.js里面,下面举个例(获取微信步数)调用:

const app=getApp();

app.getAuth(‘werun’, function (res) {

        if (res)

          wx.getWeRunData({

            success(res) {

              //这个里面是表示成功直接处理业务

              // 拿 encryptedData 到开发者后台解密开放数据

              const encryptedData = res.encryptedData

              // 或拿 cloudID 通过云调用直接获取开放数据

              const iv = res.iv

              console.log(res)

              that.getRunData(encryptedData, iv)//这个请无视,业务函数

            }

          })

        else

          wx.showModal({

            title: ‘获取权限’,

            content: ‘请前往开启微信步数权限’,

            success: function (res) {

              if (res.confirm) {

                wx.openSetting()

              } else if (res.cancel) {

                console.log(‘用户点击取消’)

              }

            }

          })

      })  

提示用户自己去右上角把关闭的权限打开

回到顶部