收藏
回答

微信小程序一键授权登录流程?微信用户等假数据

<button v-if="!showPhoneLogin" @click="loginUserInfo" open-type="getPhoneNumber" @getphonenumber="handleWxLogin"

class="login-btn wechat-btn" :disabled="!isAgreed">

一键快捷登录

</button>

const loginUserInfo = async () => {

  if (!checkAgreement()) return;

  const  resProfile = await uni.getUserProfile({

    desc: '用于完善会员资料'

  })

   const resLogin = await uni.login({ provider: 'weixin' })

  userInfoEncryptedData.value = resProfile.encryptedData

  userInfoCode.value = resLogin.code

  userInfoIv.value = resProfile.iv

}


const handleWxLogin = async (e) => {

if (!checkAgreement()) return;

if(!e.detail.errMsg) return

if (e.detail.errMsg && e.detail.errMsg !== 'getPhoneNumber:ok') {

  return uni.showToast({ title: '授权失败', icon: 'none' })

}

    

uni.login({

  provider: 'weixin',

  success: (res) => {

console.log(res,88)

    if (res.code) {

  let data = {

  code: res.code,

  iv: e.detail.iv,

  encryptedData: e.detail.encryptedData,

userInfoEncryptedData: userInfoEncryptedData.value,

userInfoCode: userInfoCode.value,

userInfoIv: userInfoIv.value,

  } 

  console.log(data, 66)

  apiLoginByWx(data).then((result) => {

  console.log(result, 7778)

  uni.showToast({

  title: '登录成功',

  icon: 'success'

  })

  loginSuccess(result)

  }).catch((err) => {

  uni.showToast({

  title: err.message || '登录失败',

  icon: 'none'

  })

  })

    }

  },

  fail: (err) => {

    uni.showToast({

    title: err.message || '登录失败',

    icon: 'none'

    })

  }

});

}

回答关注问题邀请回答
收藏

4 个回答

  • 一笑皆春
    一笑皆春
    2025-07-18

    现在不支持直接获取用户昵称和头像了,会默认返回灰色头像,可以使用头像昵称填写能力

    https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/userProfile.html

    2025-07-18
    有用 2
    回复
  • Hlxuan.
    Hlxuan.
    2025-07-18

    wx.getUserProfile 接口有调整,目前已经不能获取用户微信头像和昵称,默认返回的是灰色头像和“微信用户”,也没有授权弹窗了(除了旧版本微信[基础库版本低于2.27.1]和接口调整生效期前发布的小程序版本外),参考公告:https://developers.weixin.qq.com/community/develop/doc/00022c683e8a80b29bed2142b56c01

    当前如果有头像和昵称获取的需要,只能让用户自己填写:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/userProfile.html

    2025-07-18
    有用 1
    回复 1
    • Xiao(SEAN)
      Xiao(SEAN)
      2025-07-18
      <button v-if="!showPhoneLogin" @click="loginUserInfo" open-type="getPhoneNumber" @getphonenumber="handleWxLogin"
      class="login-btn wechat-btn" :disabled="!isAgreed">
      一键快捷登录
      </button>
      click 和 @getphonenumber写在一个BUTTON里对吗??
      2025-07-18
      回复
  • Xiao(SEAN)
    Xiao(SEAN)
    2025-07-18

    感谢2位的回复 按照链接里这样填写和选头像 完成之后直接保存了吗??不需要后端程序员再写接口了吧??

    2025-07-18
    有用
    回复
  • 智能回答 智能回答 本次回答由AI生成
    2025-07-18
    有用
登录 后发表内容