人人商城小程序用户授权登录失败,getUserProfile小程序登录接口升级
微信小程序2021年4月13日调整回收wx.getUserInfo接口可获取用户授权的个人信息能力
相关文档:https://developers.weixin.qq.com/community/develop/doc/000cacfa20ce88df04cb468bc52801?highLine=login
调整后4月13日之后发布的小程序wx.getUserInfo的接口都将失效,因此导致小程序的登录会出现灰色头像和微信用户
后台也是获取不到数据导致界面一度尴尬
而且TX的开发文档写得十分含糊,这个旧不吐槽了,避坑wx.getUserProfile 接口返回的解密数据里不包含openId
思来想去不想对登录流程进行太多改动于是wx.getUserInfo接口的解密数据获取openId还是可以,只是无法获取用户基本信息getUserProfile可以既然如果那就
先用getUserProfile获取 userInfo用户对象信息,在用wx.getUserInfo 获取encryptedData和iv 至此后面的登录流程不变只是提交数据的时候多提交一个userInfo信息到后台
小程序支持库最少升到2.10.4
小程序路径pages/auth/index.wxml 立即登录按钮需要修改
<image class='logo' src='{{shop_logo}}'></image>
<view class='title'><text class='title-underline'>{{shop_name}}</text></view>
<view class='message'><text>您尚未登录需要获取您的授权后进入商城</text></view><button bindtap='close'
class='cancelBtn'>暂不登录</button>
<button bindtap='bindGetUserInfobu' class='authBtn' >立即登录</button>
小程序路径pages/auth/index.js 多加了一个方法bindGetUserInfobu() 然后获取到userInfo用户对象信息在用wx.getUserInfo 把对象结构拼装一下,这样基本没有太多改动
提交时多提交一个userInfo用户对象信息
// 登录修正
bindGetUserInfobu(){
//getUserProfile获取用户信息
wx.getUserProfile({
lang:'zh_CN',
desc: '用于完善会员资料',
success: (res) =>{
console.log(res)
//getUserInfo获取认证oid解密
wx.getUserInfo({
lang: 'zh_CN',
}).then(r=>{
console.log(r)
r.userInfo=res.userInfo
let n={
detail:r
}
this.bindGetUserInfo(n)
})
}
})
},
//登录修正
bindGetUserInfo: function (o) {
console.log(o)
wx.showLoading({
title: "加载中"
})
wx.login({
success: function (t) {
console.log(t)
n.post("wxapp/login", {
code: t.code
}, function (tt) {
console.log(tt)
tt.error ? n.alert("获取用户登录态失败:" + tt.message) : n.get("wxapp/auth", {
data: o.detail.encryptedData,
iv: o.detail.iv,
sessionKey: tt.session_key,
userInfo:o.detail.userInfo// 登录修正
}, function (n) {
console.log(n)
1 == n.isblack && wx.showModal({
title: "无法访问",
content: "您在商城的黑名单中,无权访问!",
success: function (n) {
n.confirm && e.close(), n.cancel && e.close()
}
}), o.detail.userInfo.openid = n.openId, o.detail.userInfo.id = n.id, o.detail.userInfo.uniacid = n.uniacid, e.setCache("userinfo", o.detail.userInfo), e.setCache("userinfo_openid", o.detail.userInfo.openid), e.setCache("userinfo_id", n.id), e.getSet(), wx.navigateBack({
changed: !0
})
})
})
},
fail: function () {
n.alert("获取用户信息失败!")
},
complete: function () {
wx.hideLoading()
}
})
},
后台代码 ewei_shopv2/plugin/app/core/mobile/wxapp.php
只需要修改 auth() 加4行代码 见注释 登录修正
/**
* 微信小程序登录
*/
public function auth()
{
global $_GPC;
global $_W;
$encryptedData = trim($_GPC["data"]);
$iv = trim($_GPC["iv"]);
$sessionKey = trim($_GPC["sessionKey"]);
//登录修正
$userInfo=$_GPC["userInfo"];
//登录修正
if (empty($encryptedData) || empty($iv)) {
return app_error(AppError::$ParamsError);
}
$pc = new WXBizDataCrypt($this->appid, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $data);
if ($errCode == 0) {
$data = json_decode($data, true);
$this->refine($data["openId"]);
$member = m("member")->getMember("sns_wa_" . $data["openId"]);
//登录修正
if(is_array($userInfo)){
$data=array_merge($data, $userInfo);
}
//登录修正
if (empty($member)) {
$member = array("uniacid" => $_W["uniacid"], "uid" => 0, "openid" => "sns_wa_" . $data["openId"], "nickname"=>!empty($data["nickName"]) ? $data["nickName"] : "", "avatar" => !empty($data["avatarUrl"]) ? $data["avatarUrl"] : "", "gender" => !empty($data["gender"]) ? $data["gender"] : "-1", "openid_wa" => $data["openId"], "comefrom" => "sns_wa", "createtime" => time(), "status" => 0);
pdo_insert("ewei_shop_member", $member);
$id = pdo_insertid();
$data["id"] = $id;
$data["uniacid"] = $_W["uniacid"];
if (method_exists(m("member"), "memberRadisCountDelete")) {
m("member")->memberRadisCountDelete();
}
} else {
$updateData = array("nickname" => !empty($data["nickName"]) ? $data["nickName"] : "", "avatar" => !empty($data["avatarUrl"]) ? $data["avatarUrl"] : "", "gender" => !empty($data["gender"]) ? $data["gender"] : "-1");
pdo_update("ewei_shop_member", $updateData, array("id" => $member["id"], "uniacid" => $member["uniacid"]));
$data["id"] = $member["id"];
$data["uniacid"] = $member["uniacid"];
$data["isblack"] = $member["isblack"];
}
if (p("commission")) {
p("commission")->checkAgent($member["openid"]);
}
return app_json($data, $data["openId"]);
}
return app_error(AppError::$WxAppError, "登录错误, 错误代码: " . $errCode);
}
对人人整个流程不是特别熟悉本着能少改动代码尽量少改动,缺什么补什么吧,这个我朋友上线测试之前没有获取到用户个人数据的账号,在重新登录后头像昵称数据会更新过来
希望能帮到大家共度难关,要是能混个打赏就更好了,哈哈哈,白日梦中....