小程序用户信息接口调整兼容处理
小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。
微信官方接口调整
考虑到近期开发者对小程序登录、用户信息相关接口调整的相关反馈,为优化开发者调整接口的体验,《小程序登录、用户信息相关接口调整说明》公告中关于小程序回收 wx.getUserInfo 接口可获取用户授权的个人信息能力的截止时间调整至2021年4月28日24时。
在此期间,未调整的小程序可能会在微信开发者工具收到“平台 getUserInfo 接口能力调整,请尽快适配”提醒,建议开发者尽快适配 wx.getUserInfo 接口回收场景。
后续开发者可以使用 wx.getUserProfile 接口获取用户授权的个人信息。
HTML代码
<button v-if="!is_getinfo && !canIUseGetUserProfile" open-type="getUserInfo" @getuserinfo="GetUserInfo"></button>
<button v-if="!is_getinfo && canIUseGetUserProfile" @click="getUserProfile"></button>
JS代码
export default {
onLoad() {
if(wx.getUserProfile){
this.canIUseGetUserProfile = true // 兼容代码
}
},
data(){
return {
is_getinfo:false,
canIUseGetUserProfile:false, // 兼容代码
}
},
method:{
// 新版微信个人信息授权
getUserProfile(e) {
// 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
// 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
wx.getUserProfile({
desc: '用于更新头像', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: result => {
console.log("🚀 ~ result", result)
this.is_getinfo = true;
},
fail: () => {
console.log("用户拒绝授权")
}
})
},
// 旧版微信个人信息授权
GetUserInfo(e) {
console.log(e)
if (e.detail.errMsg == 'getUserInfo:ok') {
this.is_getinfo = true;
}
},
}