uniapp小程序微信授权获取头像昵称

最近修改小程序发现线上版的小程序通过uni.getUserProfile获取到的头像为默认灰色头像,昵称为微信用户,体验版能够正常获取到头像昵称,详参考链接小程序用户头像昵称获取规则调整公告

解决方法参考链接获取头像昵称

<!-- 头像获取 -->
<button type="primary" open-type="chooseAvatar" @chooseavatar="onChooseAvatar" plain="true"
      style="width: 150rpx;height: 150rpx;border-radius: 20rpx;margin-bottom: 100rpx;margin-top: 20vw;border: none;position: relative;">
      <image :src="avatar" mode="aspectFill" style="width: 150rpx;height: 150rpx;border-radius: 20rpx;" v-if="avatar">
      </image>
      <image src="../../static/home/noavatar.jpg" mode="aspectFill"
        style="width: 150rpx;height: 150rpx;border-radius: 20rpx;" v-else></image>
      <view class="tips">点击授权头像</view>
    </button>

<!-- 昵称获取 -->
<input type="nickname" style="font-size: 28rpx;color: #666666;flex: 1;" placeholder="请输入昵称" v-model="nickname" @blur="getNickname"/>
// 获取头像
onChooseAvatar(e){
   const { avatarUrl } = e.detail   // 此返回的路径为临时路径,需转换为永久路径保存使用
   this.avatar = avatarUrl
 },
// 获取昵称
getNickname(e){
   this.nickname = e.detail.value
 },

在需要授权才能使用的操作或跳转中先验证是否已授权,若未授权跳转授权页面,授权成功提交后返回

要实现微信授权登录,你需要按照以下步骤进行操作: 1. 在微信公众平台申请开发者账号,并创建一个小程序。 2. 在小程序管理后台中,打开“设置”->“第三方设置”,将“网页授权域名”设置为你的服务器域名。 3. 在uniapp中,使用uni.login()方法获取用户的code。 4. 将获取到的code发送到服务器,使用微信官方提供的接口获取用户的openid和access_token。 5. 将获取到的openid和access_token保存在服务器端,返回给uniapp。 6. 在uniapp中,使用uni.getUserInfo()方法获取用户的基本信息,如昵称头像等。 7. 将用户的基本信息保存在服务器端,返回给uniapp。 以下是一个简单的示例代码: ```html <!-- 登录页面 --> <template> <view> <button @tap="wxLogin">微信授权登录</button> </view> </template> <script> export default { methods: { wxLogin() { uni.login({ success(res) { // 将code发送到服务器 uni.request({ url: 'https://your.server.com/login', data: { code: res.code }, success(res) { // 获取到openid和access_token const { openid, access_token } = res.data // 将openid和access_token保存在storage中,用于后续接口请求 uni.setStorageSync('openid', openid) uni.setStorageSync('access_token', access_token) // 获取用户基本信息 uni.getUserInfo({ success(res) { // 将用户基本信息发送到服务器 uni.request({ url: 'https://your.server.com/userinfo', data: { openid, nickname: res.userInfo.nickname, avatar: res.userInfo.avatarUrl }, success(res) { // 用户信息保存成功,跳转到首页 uni.switchTab({ url: '/pages/index/index' }) } }) } }) } }) } }) } } } </script> ``` 在样式方面,可以使用uniapp提供的组件样式和自定义样式来实现。以下是一个简单的示例代码: ```html <!-- 登录页面 --> <template> <view class="container"> <image class="logo" src="/static/logo.png"></image> <view class="title">欢迎登录</view> <button class="login-btn" @tap="wxLogin">微信授权登录</button> </view> </template> <style> .container { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; } .logo { width: 100px; height: 100px; margin-bottom: 20px; } .title { font-size: 24px; font-weight: bold; margin-bottom: 20px; } .login-btn { width: 200px; height: 50px; background-color: #42b983; color: #fff; border-radius: 25px; font-size: 16px; font-weight: bold; text-align: center; line-height: 50px; } </style> ``` 以上示例代码仅供参考,具体实现方式还需根据项目需求进行调整。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值