编程语言
首页 > 编程语言> > 微信小程序授权登陆 getUserProfile

微信小程序授权登陆 getUserProfile

作者:互联网

https://blog.csdn.net/lhkuxia/article/details/115163985
在这里插入图片描述
在这里插入图片描述
1.将授权登陆获取用户信息的接口调整了,新增了一个wx.getUserProfile。特说明一下授权登陆的注意事项:

2.原授权登陆流程不变,依旧是
wx.login >>> code >>> 请求接口换取openid >>> openid >>> 自定义请求态 >>> uid
只是获取用户信息的地方发生改变了,获取用户信息必须通过wx.getUserProfile获取

3.wx.getUserProfile这个API必须写在事件的最上面

<button bindtap="login">登陆</button>
login() {
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
      success: (file) => {
        console.log(file)
        wx.login({
          success: (res) => {
            console.log(res);
            wx.request({
              url: 'code获取openid的接口',
              data: {
                code: res.code
              },
              success: (open) => {
                console.log(open.data);
                wx.request({
                  url: '授权登陆接口',
                  data: {
                    openid: open.data.openid,
                    NickName: file.userInfo.nickName,
                    HeadUrl: file.userInfo.avatarUrl
                  },
                  success(data) {
                    console.log(data.data);
                  }
                })
              }
            })
          }
        })
      }
    })
  },

获取用户信息的接口变化历史:

直接用wx.getUserInfo获取用户信息,后来被限制。
使用button按钮的open-type="getUserInfo",通过bindgetuserinfo事件获取用户信息,现在叒限制。
使用API:wx.getUserProFile获取用户信息

标签:openid,微信,用户,获取,授权,data,getUserProfile,wx
来源: https://blog.csdn.net/weixin_49295874/article/details/115618856