微信小程序开发现已不支持使用wx.getUserProfile 接口获取用户头像,同时也无法使用wx.getUserInfo 接口获取用户头像和昵称
解决方案:
open-type
的值设置为 chooseAvatar
,当用户选择需要使用的头像之后,可以通过 bindchooseavatar
事件回调获取到头像信息的临时路径。type
的值设置为 nickname
,当用户在此input进行输入时,键盘上方会展示微信昵称。//js文件
// 用户选择头像
onChooseAvatar(e) {
const {
avatarUrl
} = e.detail
console.log(avatarUrl);
this.setData({
['userInfo.avatarUrl']: avatarUrl,
})
},
// 用户修改昵称
changeNickName(e) {
let name = e.detail.value;
if (name.length === 0) return;
this.setData({
['userInfo.nickName']: e.detail.value
})
},
<!-- wxml文件 -->
<button class="avatar-wrapper" open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar">
修改头像
</button>
<input type="nickname" class="nick-name-input" placeholder="请输入昵称" bindblur="changeNickName"/>
具体实现代码以及小程序项目完整功能源码可前往:https://github.com/WYXNICK/The-evening-scenery-is-like-spring
已经不能获取用户头像了,看官方文档https://developers.weixin.qq.com/community/develop/doc/000cacfa20ce88df04cb468bc52801%E3%80%82
想要做这个头像昵称的看这个文档https://developers.weixin.qq.com/community/develop/doc/00022c683e8a80b29bed2142b56c01