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); } } }