直接使用uni.getUserInfo会出现获取不到用户信息,腾讯已经做出了修改
为优化用户体验,使用 wx.getUserInfo 接口直接弹出授权框的开发方式将逐步不再支持。从2018年4月30日开始,小程序与小游戏的体验版、开发版调用 wx.getUserInfo 接口,将无法弹出授权询问框,默认调用失败。正式版暂不受影响。开发者可使用以下方式获取或展示用户信息:
小游戏与小程序获取用户接口信息调整
按照文档加上 open-type=“getUserInfo” bindgetuserinfo=“bindgetuserinfo”
<button type="primary" open-type="getUserInfo" bindgetuserinfo="bindgetuserinfo" >微信登录</button>
会出现一个问题
does not have a method “bindgetuserinfo” to handle event “getuserinfo”.
修改成如下即可
<button type="primary" open-type="getUserInfo" @getuserinfo="getuserinfo" withCredentials="true">微信登录</button>
然后在methods中
getuserinfo() {
// wx登录
wx.login({
success(res) {
if (res.code) {
//发起网络请求
const code = res.code;
// 获取微信用户信息
wx.getUserInfo({
success: function(res) {
//用户信息
const userInfo = res.userInfo;
//性别 0:未知、1:男、2:女
const { nickName, avatarUrl, gender, province, city, country } = userInfo;
uni.setStorageSync('userInfo', userInfo);
uni.navigateTo({
url: './wxinfo/wxinfo'
});
},
fail: res => {
// 获取失败的去引导用户授权
uni.showToast({
title: '您需要授权,才能获取您的信息!'
});
}
});
} else {
//
}
}
});
}