小程序路径 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);
}
相关文档:https://developers.weixin.qq.com/community/develop/doc/000cacfa20ce88df04cb468bc52801?highLine=login