微信小程序授权

 

   <button open-type="getUserInfo" bindgetuserinfo="getAuthorization"> 授权 </button> 




// 授权
  getAuthorization(){
    let that = this
    let code =  wx.login() 
    // 查看是否授权
    wx.getSetting({
      success: function (res) {
        console.log(res)
        if (res.authSetting['scope.userInfo']) {
          console.log(res.authSetting['scope.userInfo'])
                    
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              let userInfo = JSON.parse(res.rawData)
              that.setData({
                username: userInfo.nickName,
                userImg: userInfo.avatarUrl
              })
            }
          })
        }else {
          console.log("未授权=====")
          wx.authorize({
            scope: 'scope.userInfo',
            success(res) {
              // 已经授权,可以直接调用 getUserInfo 获取头像昵称
                wx.getUserInfo({
                  success: function (res) {
                    let userInfo = JSON.parse(res.rawData)
                    that.setData({
                      username: userInfo.nickName,
                      userImg: userInfo.avatarUrl
                    })
                  }
                })
            },
            fail(res){
              console.log(res)
            }
          })
        }
      }
    })
  },

 

posted @ 2020-04-17 19:29  番茄西红柿u  阅读(173)  评论(0编辑  收藏  举报