网友真实露脸自拍10p,成人国产精品秘?久久久按摩,国产精品久久久久久无码不卡,成人免费区一区二区三区

小程序模板網(wǎng)

thinkphp后臺(tái)獲取微信小程序用戶信息

發(fā)布時(shí)間:2017-12-30 09:12 所屬欄目:小程序開(kāi)發(fā)教程
首先需要看懂微信小程序開(kāi)發(fā)文檔的  開(kāi)放接口中的  登錄 和 用戶信息。
 
wx.login(OBJECT)

調(diào)用接口獲取登錄憑證(code)進(jìn)而換取用戶登錄態(tài)信息,包括用戶的唯一標(biāo)識(shí)(openid) 及本次登錄的 會(huì)話密鑰(session_key)等。用戶數(shù)據(jù)的加解密通訊需要依賴會(huì)話密鑰完成。

code 換取 session_key

?這是一個(gè) HTTPS 接口,開(kāi)發(fā)者服務(wù)器使用登錄憑證 code 獲取 session_key 和 openid。

session_key 是對(duì)用戶數(shù)據(jù)進(jìn)行加密簽名的密鑰。為了自身應(yīng)用安全,session_key 不應(yīng)該在網(wǎng)絡(luò)上傳輸

小程序端的代碼:

[javascript] view plain copy
 
 print?
  1. //app.js  
  2. App({  
  3.   onLaunch: function () {  
  4.     //調(diào)用API從本地緩存中獲取數(shù)據(jù)  
  5.     var logs = wx.getStorageSync('logs') || []  
  6.     logs.unshift(Date.now())  
  7.     wx.setStorageSync('logs', logs)  
  8.   },  
  9.   getUserInfo:function(cb){  
  10.     var that = this  
  11.     if(this.globalData.userInfo){  
  12.       typeof cb == "function" && cb(this.globalData.userInfo)  
  13.     }else{  
  14.       //調(diào)用登錄接口  
  15.       wx.login({  
  16.         success: function (res) {  
  17.               var code = res.code  
  18.                 // success  
  19.                 // 獲取用戶信息  
  20.               wx.getUserInfo({  
  21.                   success: function (data) {  
  22.                     that.globalData.userInfo = data.userInfo  
  23.                     typeof cb == "function" && cb(that.globalData.userInfo)  
  24.                     var rawData = data.rawData;  
  25.                     var signature = data.signature;  
  26.                     var encryptedData = data.encryptedData;  
  27.                     var iv = data.iv;  
  28.                     wx.request({  
  29.                       url: "你的后臺(tái)地址",  
  30.                       data: {  
  31.                           "code" : code,  
  32.                          " rawData" : rawData,  
  33.                           "signature" : signature,  
  34.                          " iv" : iv,  
  35.                           "encryptedData" : encryptedData  
  36.                       },  
  37.                       method: 'GET',  
  38.                       success: function(res){  
  39.                         // success  
  40.                         console.log(res)  
  41.                         console.log(rawData)  
  42.                       }  
  43.                     })  
  44.                   }  
  45.                 })  
  46.         }  
  47.       })  
  48.     }  
  49.   },  
  50.   globalData:{  
  51.     userInfo:null  
  52.   },  
  53. })  

查看微信小程序端的 network 可能查看請(qǐng)求是否成功
 

thinkphp后臺(tái)代碼

[php] view plain copy
 
 print?
  1. public function sendCode(){  
  2.     $APPID = '################APPID';  
  3.     $AppSecret = '#################';  
  4.     $code = input('get.code');  
  5.     $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$APPID.'&secret='.$AppSecret.'&js_code='.$code.'&grant_type=authorization_code';  
  6.     $arr = $this -> vegt($url);  
  7.   
  8.     $arr = json_decode($arr,true);  
  9.     // $openid = $arr['openid'];  
  10.     $session_key = $arr['session_key'];  
  11.   
  12.     // 數(shù)字簽名校驗(yàn)  
  13.     $signature = input('get.signature');  
  14.     $signature2 = sha1($_GET['rawData'].$session_key);  
  15.     if($signature != $signature2){  
  16.         echo "數(shù)字簽名失敗";  
  17.         die;  
  18.     }  
  19.     // 獲取信息,對(duì)接口進(jìn)行解密  
  20.     Vendor("PHP.wxBizDataCrypt");  
  21.     $encryptedData = $_GET['encryptedData'];  
  22.     $iv = $_GET['iv'];  
  23.     if(empty($signature) || empty($encryptedData) || empty($iv)){  
  24.         echo "傳遞信息不全";  
  25.     }  
  26.     include_once "PHP/wxBizDataCrypt.php";  
  27.     $pc = new \WXBizDataCrypt($APPID,$session_key);  
  28.     $errCode = $pc->decryptData($encryptedData,$iv,$data);  
  29.     if($errCode != 0){  
  30.         echo "解密數(shù)據(jù)失敗";  
  31.         die;  
  32.     }else {  
  33.         $data = json_decode($data,true);  
  34.         session('myinfo',$data);  
  35.         $save['openid'] = $data['openId'];  
  36.         $save['uname'] = $data['nickName'];  
  37.         $save['unex'] = $data['gender'];  
  38.         $save['address'] = $data['city'];  
  39.         $save['time'] = time();  
  40.         $map['openid'] = $data['openId'];  
  41.         !empty($data['unionId']) && $save['unionId'] = $data['unionId'];  
  42.   
  43.         $res = \think\Db::name('user') -> where($map) -> find();  
  44.         if(!$res){  
  45.             $db = \think\Db::name('user') -> insert($save);   
  46.             if($db !== false){  
  47.                 echo "保存用戶成功";  
  48.             }else{  
  49.                 echo "error";  
  50.             }  
  51.         }else{  
  52.             echo "用戶已經(jīng)存在";  
  53.         }  
  54.     }  
  55. //生成第三方3rd_session  
  56. $session3rd  = null;  
  57. $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";  
  58. $max = strlen($strPol)-1;  
  59. for($i=0;$i<16;$i++){  
  60.     $session3rd .=$strPol[rand(0,$max)];  
  61. }  
  62. // echo $session3rd;  
  63. }  
  64. public function vegt($url){  
  65.     $info = curl_init();  
  66. curl_setopt($info,CURLOPT_RETURNTRANSFER,true);  
  67.    curl_setopt($info,CURLOPT_HEADER,0);  
  68.    curl_setopt($info,CURLOPT_NOBODY,0);  
  69.    curl_setopt($info,CURLOPT_SSL_VERIFYPEER, false);  
  70.    curl_setopt($info,CURLOPT_SSL_VERIFYHOST, false);  
  71.    curl_setopt($info,CURLOPT_URL,$url);  
  72.    $output= curl_exec($info);  
  73.    curl_close($info);  
  74.    return $output;  
  75. }  
 
官網(wǎng)有加解密的文件自行下載:  https://www.w3cschool.cn/weixinapp/weixinapp-signature.html
寫(xiě)的不好的地方歡迎補(bǔ)充,這也是小編經(jīng)過(guò)多處得出學(xué)習(xí)得出的


易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開(kāi)源 碼云倉(cāng)庫(kù):starfork
本文地址:http://www.xiuhaier.com/wxmini/doc/course/18330.html 復(fù)制鏈接 如需定制請(qǐng)聯(lián)系易優(yōu)客服咨詢:800182392 點(diǎn)擊咨詢
QQ在線咨詢
AI智能客服 ×