小程序授权微信登录,获取微信用户名,头像,登录code

2023-11-15

getUserProfile(e) {
    var that = this
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: (res) => {
        if (res) {
          //用户名 res.userInfo.nickName
          //头像res.userInfo.avatarUrl
          //获取code
          wx.login({
            success: (ress) => {
           //code获取 ress.code
             //app.globalData.URL是封装的接口通用地址部分
              let url = app.globalData.URL + '接口地址'
              //data是传参的值
              let data = {
                avatarUrl: res.userInfo.avatarUrl,
                code: that.data.code,
                nickName: res.userInfo.nickName,
              }
              //app.wxRequest是封装的请求方法
              app.wxRequest(
                'POST',
                url,
                data,
                (res) => {
                  if (res.code == 200) {
                  //登录成功跳转到首页
                    wx.switchTab({
                      url: '../index/index',
                    })
                  }  else {
                    that.onLoad()
                  }
                },
                (err) => {
                  console.log(err.errMsg)
                }
              )
            },
          })
        }
      },
    })
  },
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

小程序授权微信登录,获取微信用户名,头像,登录code 的相关文章

随机推荐