小程序使用wx.getUserProfile发布后未拿到unionid?
发布于 5 年前 作者 tao43 14788 次浏览 来自 问答
  //新登陆点击
  getUserProfileApi(){
    //未更新或无法使用新api用户无法使用以下逻辑
    console.log("v46 升级微信并可用",this.data.canIUseUserProfile,this.data.getUserApiType);
    if(!this.data.canIUseUserProfile) return false
    let userInfo = wx.getStorageSync("userInfo")
     if(userInfo){
       //无需弹窗登录
        wx.login({
          success(res) => {
            console.log('v46 wxLogin:', res)
            let openId = wx.getStorageSync('openId') ? wx.getStorageSync('openId') : ''
            let iv = wx.getStorageSync("iv") || '';
            let encryptedData = wx.getStorageSync("encryptedData") || '';
            memberModel.getOpenId({
              data: {
                code: res.code,
                encryptedData: encryptedData,
                iv: iv,
                openId: openId
              }
            }).then(res => {
              if (!res.data.success) return
              //存储unionid
              wx.setStorageSync('openId', res.data.datas.openId)
              wx.setStorageSync('unionId', res.data.datas.unionId)
              wx.showLoading()
              //登录
              this.login()
            }).catch(_ => {
              canGoNext = true
              reject()
            })
          },
          fail() {
            canGoNext = true
            reject()
          }
        })
     }else {
      wx.getUserProfile({
        desc:"完善用户基本信息",
        success:(event) => {
          console.log("v46 success",event);
          let iv = '';
          let encryptedData = ''
          if(event.userInfo){
            app.globalData.userInfo = event.userInfo
            wx.setStorageSync("userInfo",event.userInfo)
          }
          if(event && event.encryptedData){
            wx.setStorageSync('encryptedData', event.encryptedData)
            encryptedData = event.encryptedData
          }
          if(event && event.iv){
            wx.setStorageSync('iv', event.iv)
            iv = event.iv
          }
          wx.login({
            success(res) => {
              console.log('v46 wxLogin:', res)
              let openId = wx.getStorageSync('openId') ? wx.getStorageSync('openId') : ''
              memberModel.getOpenId({
                data: {
                  code: res.code,
                  encryptedData: encryptedData,
                  iv: iv,
                  openId: openId
                }
              }).then(res => {
                                console.log(res) //没有拿到unionid
                if (!res.data.success) return
                //存储unionid
                wx.setStorageSync('openId', res.data.datas.openId)
                wx.setStorageSync('unionId', res.data.datas.unionId)
                wx.showLoading()
                //登录
                this.login()
              }).catch(_ => {
                canGoNext = true
                reject()
              })
            },
            fail() {
              canGoNext = true
              reject()
            }
          })
        },
        fail:(err) => {
          console.log("v46 fail",err);
        },
      })
     }
  },

如果小程序需与开放平台绑定,通过wx.login的code调用后台接口不是可以直接拿到unionid吗?
1 回复

开发者可以直接通过 wx.login获取到用户登录凭证 (code),调用微信登录接口(auth.code2Session获取到该用户 openid + UnionID

回到顶部