前言
之前做了微信登录,所以总结一下微信授权登录并获取用户信息这个功能的开发流程。
配置
1.首先得在微信公众平台申请一下微信小程序账号并获取到小程序的AppID和AppSecret
https://mp.weixin.qq.com/cgi-bin/loginpage?url=%2Fwxamp%2Fwacodepage%2Fgetcodepage%3Ftoken%3D418035161%26lang%3Dzh_CN
2.申请认证,企业认证300/年,个人好像是30/年,得认证,不然没有微信登录的权限。
3.配置前端uniapp的项目,在主目录下找到manifest.json文件->微信小程序配置->将你的小程序的AppID填写上去
到此基本配置就已经完毕。
登录流程
1.在实现登录之前,首先得了解登录的流程,这是微信登录的时序图
2.具体步骤为:
①小程序 wx.checkSession 校验登陆态,success :接口调用成功,session_key未过期;fail :接口调用失败,session_key已过期;
②因为微信公众平台发布了《关于小程序收集用户手机号行为的规范》中提到部分开发者在处理用户手机号过程中,存在不规范收集行为,影响了用户的正常使用体验,所以平台在向用户申请获取手机号时应明确向用户说明收集的必要原因,并提供用户隐私协议由用户主动同意;所以登录通过需通过@getphonenumber获取用户的encryptedData、iv,再通过wx.login获取用户的授权临时票据code参数;
③.服务端接收到参数后随即通过请求Appid + appSecret + code 到微信方服务器 https://api.weixin.qq.com/sns/jscode2session 获取 session_key & openid;
④.获取到openid&&session_key后随机根据getphonenumber获取到的encryptedData、iv对用户的手机号码进行解密;
流程实现(后端)(PHP)
public function login()
{
$code = input('code');
$encryptedData = input('mobileEncryptedData');
$iv = input('mobileIv');
if ($code) {
$appID = 'wxa***************'; //微信公众平台->小程序AppID
$appSecret = '****************';//微信公众平台->小程序AppSecret
// 使用 code 换取 session_key 和 openid
$url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appID}&secret={$appSecret}&js_code={$code}&grant_type=authorization_code";
$result = file_get_contents($url);
$data = json_decode($result, true);
// 获取用户openid&&session_key成功
if(isset($data['openid'])){
// 解密用户手机信息
$aesKey=base64_decode($data['session_key']);
$aesIV=base64_decode($iv);
$aesCipher=base64_decode($encryptedData);
$result2=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
// 用户电话号码 $userPhone['phoneNumber']
$userPhone=json_decode( $result2, true);
$phone=$userPhone['phoneNumber'];
$business=$this->BusinessModel->where('mobile',$phone)->find();
if($business){
// 已注册
}else{
// 未注册
}
}else{
$this->result([],'0','登录失败!','json');
}
} else {
return "缺少 code 参数";
}
}
流程实现(前端)(Vue)(uniapp)
//html
<button class="wx_login" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
手机号快捷登录
</button>
//js
getPhoneNumber(e) {
wx.login({
success: (res) => {
this.userInfo.code = res.code
this.userInfo.mobileEncryptedData = e.detail.encryptedData
this.userInfo.mobileIv = e.detail.iv
this.login()
},
fail() {
this.m_Toast('获取code失败')
}
})
}
login() {
this.$api.user.wx_login(this.userInfo).then(res => {
if (res.code == 1) {
uni.setStorageSync('userInfo', res.data);
uni.showToast({
title: res.msg,
icon: 'success',
duration: 1000
})
//其他处理
} else {
uni.showToast({
title: res.msg,
icon: 'error',
duration: 1500
})
}
})
}
1.本站内容仅供参考,不作为任何法律依据。用户在使用本站内容时,应自行判断其真实性、准确性和完整性,并承担相应风险。
2.本站部分内容来源于互联网,仅用于交流学习研究知识,若侵犯了您的合法权益,请及时邮件或站内私信与本站联系,我们将尽快予以处理。
3.本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
4.根据《计算机软件保护条例》第十七条规定“为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。”您需知晓本站所有内容资源均来源于网络,仅供用户交流学习与研究使用,版权归属原版权方所有,版权争议与本站无关,用户本人下载后不能用作商业或非法用途,需在24个小时之内从您的电脑中彻底删除上述内容,否则后果均由用户承担责任;如果您访问和下载此文件,表示您同意只将此文件用于参考、学习而非其他用途,否则一切后果请您自行承担,如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
5.本站是非经营性个人站点,所有软件信息均来自网络,所有资源仅供学习参考研究目的,并不贩卖软件,不存在任何商业目的及用途
暂无评论内容