微信小程序授权 获取用户信息
小程序昵称突然变成了“微信用户”,头像也不显示,
<!-- 近期很多小伙伴通过该方法获取头像和昵称,代码也没有做改变,突然就变成了下面这样子 -->
<open-data type="userAvatarUrl"></open-data>
<open-data type="userNickName"></open-data>
<!-- 以及通过getUserInfo获取小程序用户昵称和头像,都出现一下情况 -->
在代码没有做任何更改的情况下,导致这样的原因,坑定是微信官方改了规则
错误
正常
具体导致无法获取微信昵称和头像原因:https://developers.weixin.qq.com/community/develop/doc/000cacfa20ce88df04cb468bc52801?blockType=1
适配官网代码修改如下:
1,wxml代码修改
<!-- 修改前 -->
<button open-type='getUserInfo' bindgetuserInfo="getUserInfo" >确认</button>
<!-- 修改后-->
<button bindtap="getUserInfo" >确认</button>
2 js代码修改
//修改前
getUserInfo(e){
if(e.detail.userInfo){
let user = e.detail.userInfo
wx.setStorageSync('user',user)
}
}
//修改后
getUserInfo(e){
wx.getUserProfile({
desc: '取用户个人信息后的用途', // 声明用途,会展示在弹窗中,请谨慎填写
success: (res) => {
console.log("获取用户信息成功", res)
},
fail: res => {
console.log("获取用户信息失败", res)
}
})
}
只需要在点击事件里调用wx.getUserProfile就可以调起授权弹窗,这样就获取到头像和昵称了