关于thinkphp 与微信小程序授权登录的心得,加上完整代码
1.小程序端代
var app = getApp(); var url = 'http://shzujune.com/mianya/public/index/index/wxlogin'; var login = function (code, encryptedData, iv, signature, rawData) { var that = this //创建一个dialog提示 wx.showToast({ title: '正在登录...', icon: 'loading', duration: 5000 }); wx.request({ url: url, method: 'get', data: { code: code, encryptedData: encryptedData, iv: iv, signature:signature, rawData:rawData }, header: { 'Content-Type': 'application/json' }, success: function (res) { wx.hideToast() //console.log('服务器返回' + res.data) app.globalData.userInfo = res.data }, fail: function () { wx.showToast({ title: '网络错误!', duration: 2000 }) }, complete: function () { } }) } Page({ data: { //判断小程序的API,回调,参数,组件等是否在当前版本可用。 canIUse: wx.canIUse('button.open-type.getUserInfo') }, onLoad: function () { var that = this wx.login({ success: function (res) { //登录成功 //console.log(res) if (res.code) { var code = res.code wx.getUserInfo({ //getUserInfo流程 success: function (data) { //getUserInfo获取用户信息成功 //console.log(data) //encryptedData加密密文,iv偏移向量,encodeURIComponent把加密字符串解密成URI字符串 var encryptedData = encodeURIComponent(data.encryptedData); var iv = encodeURIComponent(data.iv); var signature = data.signature var rawData = data.rawData //请求自己的服务器 login(code, encryptedData, iv, signature, rawData); //已经授权的用户 wx.switchTab({ url: '../rec/rec', }) } }) } else { console.log('用户没有进行授权!' + res.errMsg) } } }); }, bindGetUserInfo: function (e) { //console.log(e) if (e.detail.userInfo) { //用户按了允许授权的按钮 var that = this wx.login({ success: function (res) { if (res.code) { var code = res.code wx.getUserInfo({ success: function (data) { var encryptedData = encodeURIComponent(data.encryptedData); var iv = encodeURIComponent(data.iv); var signature = data.signature; var rawData = data.rawData; //请求自己的服务器 login(code, encryptedData, iv, signature, rawData); } }) } } }) //授权成功后,跳转进入小程序首页 wx.switchTab({ url: '../rec/rec' }) } else { //用户按了拒绝按钮 wx.showModal({ title: '警告', content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!', showCancel: false, confirmText: '返回授权', success: function (res) { if (res.confirm) { console.log('用户点击了“返回授权”') } } }) } }, })
2.php 端代码
<?php namespace Home\Controller; use Home\Common\CommonController; /** * 小程序登录类 */ class WxloginController extends CommonController { public function httpGet($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } //解密微信用户敏感数据 public function login() { // 接收参数 $data = $_GET; //echo json_encode($data); // 引入解密文件 在微信小程序开发文档下载 vendor('Wx.wxBizDataCrypt'); vendor('Wx.errorCode'); $appid = "wx49b4769062bf"; $appsecret = "34d69fed605a49be9b6b0bc"; $grant_type = "authorization_code"; //授权(必填) $code = $data['code']; //有效期5分钟 登录会话 $encryptedData=urldecode($data['encryptedData']); //echo json_encode($encryptedData); $iv = urldecode($data['iv']); //echo json_encode($iv); $signature = $data['signature']; $rawData = $data['rawData']; // 拼接url $url = "https://api.weixin.qq.com/sns/jscode2session?"."appid=".$appid."&secret=".$appsecret."&js_code=".$code."&grant_type=".$grant_type; $res = json_decode($this->httpGet($url),true); $sessionKey = $res['session_key']; //取出json里对应的值 $signature2 = sha1(htmlspecialchars_decode($rawData).$sessionKey); // 验证签名 if ($signature2 !== $signature){ echo json_encode("验签失败"); } // 获取解密后的数据 $pc = new \WXBizDataCrypt($appid, $sessionKey); $errCode = $pc->decryptData($encryptedData, $iv, $data); if ($errCode == 0) { echo json_encode($data); } else { echo json_encode($errCode); } } }
作者:子钦加油
出处:https://www.cnblogs.com/zmdComeOn/
个性签名:努力生活,努力走路
阿里云拼团:https://www.aliyun.com/1111/home?userCode=f4ee1llo1核2G1M,86一年,229三年;2核4G5M,799三年;2核8G5M,1399三年
腾讯云三月采购计划特价:https://cloud.tencent.com/act/cps/redirect?redirect=1073&cps_key=15d0b1673287c43fe946626d9f4e2eee&from=console1核2G1M,88一年;1核2G1M,268三年;2核4G5M,998一年;4核8G5M,2888元三年
出处:https://www.cnblogs.com/zmdComeOn/
个性签名:努力生活,努力走路
阿里云拼团:https://www.aliyun.com/1111/home?userCode=f4ee1llo1核2G1M,86一年,229三年;2核4G5M,799三年;2核8G5M,1399三年
腾讯云三月采购计划特价:https://cloud.tencent.com/act/cps/redirect?redirect=1073&cps_key=15d0b1673287c43fe946626d9f4e2eee&from=console1核2G1M,88一年;1核2G1M,268三年;2核4G5M,998一年;4核8G5M,2888元三年