小程序所有新进用户的昵称全部变成了“微信昵称”,当时我就
吓得我赶紧爬起来翻文档(需要代码直接往后翻)
原因就像微信所说的,很多开发者在打开小程序时就通过组件方式唤起getUserInfo弹窗,如果用户点击拒绝,无法使用小程序,这种做法打断了用户正常使用小程序的流程,同时也不利于小程序获取新用户。
这里我会给出Uni-app的适配代码,针对CRMEB系统进行修复,各位同学举一反三(4.13号之前发布的正式包暂不影响)
1.修改pages/users/wechat_login/index.vue中关于微信登录的按钮
<button span=""
class="bg-green btn1">微信登录
<button span=""
class="bg-green btn1">微信登录
2.默认data中添加canUseGetUserProfile: false,然后在加载页面调用的方法里面增加uni.getUserProfile的判断,是否显示新的按钮。
canUseGetUserProfile: false
if (uni.getUserProfile) {
this.canUseGetUserProfile = true
}
3.方法中新增getUserProfile方法用户获取用户信息
//小程序授权api替换 getUserInfo
getUserProfile() {
uni.showLoading({
title: '正在登录中'
});
let self = this;
Routine.getUserProfile()
.then(res => {
let userInfo = res.userInfo;
userInfo.code = this.code;
userInfo.spread_spid = app.globalData.spid; //获取推广人ID
userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
Routine.authUserInfo(userInfo)
.then(res => {
if (res.data.key !== undefined && res.data.key) {
uni.hideLoading();
self.authKey = res.data.key;
self.isPhoneBox = true;
} else {
uni.hideLoading();
let time = res.data.expires_time - self.$Cache.time();
self.$store.commit('LOGIN', {
token: res.data.token,
time: time
});
this.getUserInfo()
}
})
.catch(res => {
uni.hideLoading();
uni.showToast({
title: res.msg,
icon: 'none',
duration: 2000
});
});
})
.catch(res => {
uni.hideLoading();
});
},
4.然后在libs/routine.js中增加getUserProfile方法
/**
* 新版小程序获取用户信息 2021 4.13微信小程序开始正式启用
*/
getUserProfile(code) {
return new Promise((resolve, reject) => {
uni.getUserProfile({
lang: 'zh_CN',
success(user) {
if (code) user.code = code;
resolve({
userInfo: user,
islogin: false
});
},
fail(res) {
reject(res);
}
})
})
}
这里要注意
if (!isset($userInfoCong['openid'])) {
throw new ValidateException('openid获取失败');
}
if (!isset($userInfoCong['openid'])) {
throw new ValidateException('openid获取失败');
}
userInfo['unionId'] = isset( userInfo [′ unionId ′]= isset (userInfoCong['unionid']) ? $userInfoCong['unionid'] : '';
userInfo['openId'] = userInfo [′ openId ′]=openid = $userInfoCong['openid'];
修复完成之后重新编译小程序就可以解决授权之后微信用户的问题啦。