zoukankan      html  css  js  c++  java
  • 微信小程序之 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"
                  }
              
                }
              }
            }
          }
        })
      }

    简单的记录,不喜勿喷。

  • 相关阅读:
    使用Wireshark 抓取数据包
    React Native 开发之 (07) 常用组件-View
    Swift开发之 (01) 语法
    React Native 开发之 (05) flexbox布局
    React Native 开发之 (06) JSX
    深入浅出Redis02 使用Redis数据库(String类型)
    React Native 开发之 (04) 例子讲解
    npm命令大全
    npm
    node.js
  • 原文地址:https://www.cnblogs.com/wfaceboss/p/8744901.html
Copyright © 2011-2022 走看看