您的位置 首页 php

微信小程序getUserProfile详解,CRMEB系统修复流程

小程序所有新进用户的昵称全部变成了“微信昵称”,当时我就

吓得我赶紧爬起来翻文档(需要代码直接往后翻)

  1. wx.getUserProfile(Object object)
  1. 基础库 2.10.4 开始支持,低版本需做兼容处理。
  1. 获取用户信息。每次请求都会弹出授权窗口,用户同意后返回 userInfo。
  1. getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息 即wx.getUserInfo接口的返回参数不变,但开发者获取的userInfo为匿名信息。

  1. 为优化用户的使用体验,平台将进行以下调整:
  1. 2021年2月23日起,若小程序已在微信开放平台进行绑定,则通过wx.login接口获取的登录凭证可直接换取unionID
  1. 2021年4月13日后发布的小程序新版本,无法通过wx.getUserInfo与<button open-type=”getUserInfo”/>获取用户个人信息(头像、昵称、性别与地区),将直接获取匿名数据(包括userInfo与encryptedData中的用户个人信息),获取加密后的openID与unionID数据的能力不做调整。此前发布的小程序版本不受影响,但如果要进行版本更新则需要进行适配。
  1. 新增getUserProfile接口(基础库2.10.4版本开始支持),可获取用户头像、昵称、性别及地区信息,开发者每次通过该接口获取用户个人信息均需用户确认。具体接口文档:《getUserProfile接口文档》
  1. 由于getUserProfile接口从2.10.4版本基础库开始支持(覆盖微信7.0.9以上版本),考虑到开发者在低版本中有获取用户头像昵称的诉求,对于未支持getUserProfile的情况下,开发者可继续使用getUserInfo能力。开发者可参考getUserProfile接口文档中的示例代码进行适配。
  1. 请使用了wx.getUserInfo接口或<button open-type=”getUserInfo”/>的开发者尽快适配。开发者工具1.05.2103022版本开始支持getUserProfile接口调试,开发者可下载该版本进行改造。

原因就像微信所说的,很多开发者在打开小程序时就通过组件方式唤起getUserInfo弹窗,如果用户点击拒绝,无法使用小程序,这种做法打断了用户正常使用小程序的流程,同时也不利于小程序获取新用户。

这里我会给出Uni-app的适配代码,针对CRMEB系统进行修复,各位同学举一反三(4.13号之前发布的正式包暂不影响)

1.修改pages/users/wechat_login/index.vue中关于微信登录的按钮

<button v-if=”canUseGetUserProfile && code” hover-class=”none” @tap=”getUserProfile”

class=”bg-green btn1″>微信登录</button>

<button v-else hover-class=”none” open-type=”getUserInfo” @getuserinfo=”setUserInfo”

class=”bg-green btn1″>微信登录</button>

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’,

desc: ‘用于完善会员资料’, // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写

success(user) {

if (code) user.code = code;

resolve({

userInfo: user,

islogin: false

});

},

fail(res) {

reject(res);

}

})

})

}

这里要注意

  1. `desc`为必填项,并且将来有可能会将信息提示在弹窗内,所以要谨慎填写;
  2. `getUserProfile`API不是通过`open-type` 吊起 而是使用 `@tap=”getUserProfile”` 或 `bindtap=”getUserProfile”` 调用的,且必须为直接调用,不能通过其他函数回调调用。否则会报`getUserProfile:fail can only be invoked by user TAP gesture.`错误
  1. 后台代码修改地方比较少集中在app/services/wechat/RoutineServices.php文件中

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’];

修复完成之后重新编译小程序就可以解决授权之后微信用户的问题啦。

文章来源:智云一二三科技

文章标题:微信小程序getUserProfile详解,CRMEB系统修复流程

文章地址:https://www.zhihuclub.com/153331.shtml

关于作者: 智云科技

热门文章

网站地图