小程序用户授权
发布于 5 年前 作者 xqiao 14432 次浏览 来自 问答

用户拒绝授权后如何再次调用授权接口?

1 回复

我是这样写的:

// 检测用户是否拒绝了授权
ifGetUser: function (callback) {
  wx.getSetting ? wx.getSetting({
    success: settings => {
      var can = settings.authSetting['scope.userInfo']
      this.data.noUser = can
      console.log('是否已授权', can)
      if (!this.data.noUser) {
        wx.hideLoading();
        wx.showModal({
          content: '拒绝了授权,是否重新开启',
          confirmText: '前往开启',
          showCancel: false,
          success: res => {
            wx.openSetting({
              success: (res) => { }
            });
          },
        });
        return;
      } else {
        callback && callback(true);
      }
    }
  }) : wx.showModal({
    content: '您的小程序版本太低,请更新微信',
    showCancel: false
  });
},

然后这样使用:

// 请求用户授权获得信息
getInfo: function (callbcak) {
  wx.getUserInfo({
    lang: 'zh_CN',
    withCredentials: true,
    complete: res => {
      this.ifGetUser(can => { // 判断是否已授权
        console.log('用户信息', res.userInfo)
        this.data.userInfo = res.userInfo
        callbcak && callbcak(res)
      })
    }
  })
},
回到顶部