小程序授权登录

onLoad:function(){

    this.getSetting()

  },

  getSetting: function () {

    var that = this;

    wx.getSetting({

      success: res => {

        // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框

        if (res.authSetting['scope.userInfo']) {

          that.setData({

            warrant: false

          })

          that.getUserInfo();

        } else {

          that.setData({

            warrant: true

          })

        }

      }

    });

  },

  bindGetUserInfo: function (e) {

    var that = this;

    if (e.detail.userInfo) {

      that.setData({

        warrant: false

      })

      that.getUserInfo();

    } else {

      //用户按了拒绝按钮

      wx.showModal({

        title: '警告',

        content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',

        showCancel: false,

        confirmText: '返回授权',

        success: function (res) {

          if (res.confirm) {

            console.log('用户点击了“返回授权”')

          }

        }

      })

    }

  },

  getUserInfo: function () {

    var that = this;

    wx.getUserInfo({

      success: res => {

        console.log(res.userInfo, "用户信息");

      }

    })

  },

你可能感兴趣的:(小程序授权登录)