官方做出调整无法再次通过 open-data 直接获取用户个人信息 https://developers.weixin.qq.com/community/develop/doc/000e881c7046a8fa1f4d464105b001?blockType=1
解决方案
<view class="userinfo" v-if="!avatarName" @click="getUserProfile">
<view class="userinfo-avatar">
<image src="../../static/images/defaultimg.png" mode="scaleToFill"></image>
</view>
点击获取头像昵称
</view>
<view class="userinfo" v-else >
<view class="userinfo-avatar">
<image :src="avatarName.avatarUrl" mode="scaleToFill" @click="getUserProfile"></image>
</view>
{{avatarName.nickName}}
</view>
data() {
return {
avatarName:""
};
},
methods: {
getUserProfile(e) {
// 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
// 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
uni.getUserProfile({
desc: '用于头像昵称展示', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
this.avatarName = res.userInfo;
//储存到本地
uni.setStorageSync(KEY_AVATARNAME,res.userInfo)
}
})
}
},
onLoad: function(option) {
var avatarName = uni.getStorageSync(KEY_AVATARNAME);
if(avatarName){
this.avatarName = avatarName;
}
}