場景描述:
在微信小程序中,我們可能用到很多種登陸方式,例如用手機作為標識登陸亦或者用微信信息作為標識登陸(但這寫都要看你的項目需要),在這里我說一下如何使用微信信息作為標識登陸。
編程思路:
分三步走,第一步微信信息獲取發送給后台-》第二步解密微信信息驗證數據庫-》登陸成功保存緩存並且更新token
小程序前端處理
1 //調用登錄接口,獲取 code 2 wx.login({ 3 success: function (res) { 4 //微信js_code 5 that.setData({ wxcode: res.code }); 6 //獲取用戶信息 7 wx.getUserInfo({ 8 success: function (res) { 9 //獲取用戶敏感數據密文和偏移向量 10 that.setData({ encryptedData: res.encryptedData }) 11 that.setData({ iv: res.iv }) 12 } 13 }) 14 } 15 })
為了讓大家看得更清楚,我在這里直接打印到視圖層(),數據獲取到了,接下來將數據發送到后台,有時候后台解密用戶信息失敗,可能是因為你獲取小程序的用戶信息再獲取用戶的code導致的,所以先獲取code碼再獲取用戶加密信息
1 //獲取post數組,thinkphp5封裝好的input函數,而tp3是I()函數;如果前端使用的是json類型的數據,tp3I函數是獲取不了的,只能使用輸入流的形式 file_get_contents('php://input'); 2 //這里缺少對數據的驗證,大家不要學我,tp5是有驗證層 3 4 //tp5,獲取json和formdata類型 5 $data = input('post.'); 6 7 //獲取微信信息的基本url,其中%s是轉換符,這條url應該存到全局文件中,怎么傳自己想 8 $baseInfoUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code"; 9 $infoUrl = sprintf($baseInfoUrl, '填寫你的微信小程序appid','填寫你的小程序secret_key',$data['code'); 10 11 $apiData = json_decode(httpCurl($apiUrl), true); //這個是異步調用api,方法在底部 12 13 //獲取微信用戶session_key 14 (!isset($apiData['session_key'])) ? 15 throw new Exception('獲取微信信息失敗') 16 $errCode = $this->decryptData('你的aooppid', $apiData['session_key'], $data['encryptedData'], $data['iv'], $data); //方法在文章底部 17 18 //判斷解密是否出錯 19 ($errCode != 0) ? 20 throw new Exception('解密微信信息失敗') 21 $data = json_decode($data, true);//通過引用改變了data信息,解密后獲取用戶unionId和session_key前者辨別公眾號信息,后者獲取手機信息(個人認為) 22 23 //到這一步就獲取到用戶的所有信息了$data 24 25 //判斷用戶是否存在,不存在就新增,存在就更新用戶信息 26 27 //更新用戶信息Token和其他基本信息
附帶異步調用api和解密微信信息
1 /** 2 * 檢驗數據的真實性,並且獲取解密后的明文. 3 * @param $encryptedData string 加密的用戶數據 4 * @param $iv string 與用戶數據一同返回的初始向量 5 * @param $data string 解密后的原文 6 * 7 * @return int 成功0,失敗返回對應的錯誤碼 8 */ 9 public function decryptData($appid, $sessionKey, $encryptedData, $iv, &$data) 10 { 11 $OK = 0; 12 $IllegalAesKey = -41001;//encodingAesKey 非法 13 $IllegalIv = -41002; 14 $IllegalBuffer = -41003;//aes 解密失敗 15 $DecodeBase64Error = -41004;//解密后得到的buffer非法 16 17 if (strlen($sessionKey) != 24) { 18 return $IllegalAesKey; 19 } 20 $aesKey = base64_decode($sessionKey); 21 22 23 if (strlen($iv) != 24) { 24 return $IllegalIv; 25 } 26 $aesIV = base64_decode($iv); 27 28 $aesCipher = base64_decode($encryptedData); 29 30 $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV); 31 32 $dataObj = json_decode($result); 33 if ($dataObj == NULL) { 34 return $IllegalBuffer; 35 } 36 if ($dataObj->watermark->appid != $appid) { 37 return $IllegalBuffer; 38 } 39 $data = $result; 40 return $OK; 41 } 42 43 44 /** 45 * curl請求訪問 46 * @param $url string 地址 47 * @param $data string post數據 48 * @return mixed string 返回結果 49 */ 50 function httpCurl($url,$data = "") 51 { 52 $curl = curl_init(); 53 curl_setopt($curl, CURLOPT_URL, $url); 54 curl_setopt($curl, CURLOPT_REFERER, $url); 55 curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Linux; Android 5.0; SM-N9100 Build/LRX21V) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile Safari/537.36 V1_AND_SQ_5.3.1_196_YYB_D QQ/5.3.1.2335 NetType/WIFI"); 56 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 57 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 58 if($data != ''){ 59 curl_setopt($curl,CURLOPT_POST,1); 60 curl_setopt($curl,CURLOPT_POSTFIELDS,$data); 61 } 62 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 63 $result = curl_exec($curl); 64 return $result; 65 }