最新微信登录授权

微信授权完整流程记录

<button bindtap="login">马上登录</button>//授权要结合button使用

login() {
    
    
    let that = this
    wx.getSetting({
    
    //判断授权信息
      success(res) {
    
    
        
        if (res.authSetting['scope.userInfo']) {
    
    
           that.getUserProfile();//有授权时候直接获取信息
        } else {
    
    
          wx.authorize({
    
    
            scope: "scope.userInfo",
            success(res) {
    
    
              console.log(res)
              that.getUserProfile();
            }
          })
        }
      }
    })
getUserInfo() {
    
    
    let that = this;
    wx.getUserProfile({
    
    
      lang: '项目名',
      desc: '获取信息的原因',
      complete(res) {
    
    
        console.log(res)//有姓名//头像
        that.login();//登录获取openid,sessionKey等信息
      }
    })
  },
  login() {
    
    
    let that = this;
    wx.login({
    
    
      success(res) {
    
    
        if (res.code) {
    
    
          //发起网络请求
         //配合后台做解析openid并验证登录
        } else {
    
    
          console.log('登录失败!' + res.errMsg)
        }
      }
    })

  },

猜你喜欢

转载自blog.csdn.net/men_gqi/article/details/115040509