• 微信小程序之 wx.getUserInfo引导用户授权问题


     首先,在page外定义一个函数用户判断是否为空对象

    var isEmptyObject = function (e) {
      var temp;
      for (temp in e)
        return !1;
      return !0
    }

    然后,在page中的onload里面调用授权

    onLoad: function () {
        var that = this;
        if (app.globalData.userInfo) {
          this.setData({
            userInfo: app.globalData.userInfo
          })
        } else if (this.data.canIUse) {
          // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
          // 所以此处加入 callback 以防止这种情况
          app.userInfoReadyCallback = res => {
            this.setData({
              userInfo: res.userInfo
            })
          }
        } else {
          // 在没有 open-type=getUserInfo 版本的兼容处理
          wx.getUserInfo({
            success: res => {
              app.globalData.userInfo = res.userInfo
              this.setData({
                userInfo: res.userInfo
              })
              that.checkSettingStatu();
            },
            fail: function () {
              wx.showModal({
                title: '用户未授权',
                content: '如需正常使用该小程序功能,请按确定并在授权管理中选中“用户信息”,然后点按确定。最后再重新进入小程序即可正常使用。',
                showCancel: false,
                success: function (resbtn) {
                  if (resbtn.confirm) {
                    wx.openSetting({
                      success: function success(resopen) {
                        //  获取用户数据
                        that.checkSettingStatu();
                      }
                    });
                  }
                }
              })
            }
          })
        }
      }

    最后,在page中定义一个 用于检测 当前授权的状态

    checkSettingStatu: function (cb) {
        var that = this;
        // 判断是否是第一次授权,非第一次授权且授权失败则进行提醒
        wx.getSetting({
          success: function success(res) {
            var authSetting = res.authSetting;
            if (isEmptyObject(authSetting)) {
                   //第一次
            } else {
              // 没有授权的提醒
              if (authSetting['scope.userInfo'] === false) {
                wx.showModal({
                  title: '用户未授权',
                  content: '如需正常使用该小程序功能,请按确定并在授权管理中选中“用户信息”,然后点按确定。最后再重新进入小程序即可正常使用。',
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      wx.openSetting({
                        success: function success(res) {
                          console.log()
                        }
                      });
                    }
                  }
                })
              } else if (authSetting['scope.userInfo'] === true) {
                          //该处用户获取用户的一些授权信息
                if (that.data.userInfo) {
                  var nickname = that.data.userInfo.nickName;
                  var gender = that.data.userInfo.gender
                  //性别 0:未知、1:男、2:女
                  if (gender == 1) {
                    gender = "True"
                  } else if (gender == 2) {
                    gender = "False"
                  } else {
                    gender = "True"
                  }
              
                }
              }
            }
          }
        })
      }

    简单的记录,不喜勿喷。

  • 相关阅读:
    VS批处理命令使用
    python实现域账号登陆
    Sql Server 优化技巧
    Windows 2012 R2 安装net4.6.1
    Resharper报“Possible multiple enumeration of IEnumerable”
    京东模拟点击
    使用常规方法爬取猫眼电影
    关于断点调试
    看网络开发实战书笔记
    scrapy的request的meta参数是什么意思?
  • 原文地址:https://www.cnblogs.com/wfaceboss/p/8744901.html
Copyright © 2020-2023  润新知