微信小程序如何获取微信用户的昵称和头像?

微信小程序如何获取微信用户的昵称和头像? 根据微信小程序用户头像昵称获取规则调整公告,wx.getUserProfile 接口、wx.getUserInfo 接口已经回收,无法获取.只能用头像昵称填写能力来获取.代码如下:

1、头像获取

需要将 button 组件 open-type 的值设置为 chooseAvatar,当用户选择需要使用的头像之后,可以通过 bind:chooseavatar 事件回调获取到头像信息的临时路径。

<button class="avatar-wrapper" open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar">
  <image class="avatar" src="{{avatarUrl}}"></image>
</button>

回调方法

Page({
  data: {
    avatarUrl: null,
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail 
    this.setData({
      avatarUrl
    })
  }
})

2、昵称填写

<input type="nickname" class="weui-input" placeholder="请输入昵称"/>

当用户点击 input,键盘弹起时,在键盘的顶部会出现当前微信用户的昵称,点击则自动填入 input。



微信扫描下方的二维码阅读本文

没有账号? 忘记密码?

社交账号快速登录