首先需要看懂微信小程序開(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ò)上傳輸。
小程序端的代碼:
-
-
App({
-
onLaunch: function () {
-
-
var logs = wx.getStorageSync('logs') || []
-
logs.unshift(Date.now())
-
wx.setStorageSync('logs', logs)
-
},
-
getUserInfo:function(cb){
-
var that = this
-
if(this.globalData.userInfo){
-
typeof cb == "function" && cb(this.globalData.userInfo)
-
}else{
-
-
wx.login({
-
success: function (res) {
-
var code = res.code
-
-
-
wx.getUserInfo({
-
success: function (data) {
-
that.globalData.userInfo = data.userInfo
-
typeof cb == "function" && cb(that.globalData.userInfo)
-
var rawData = data.rawData;
-
var signature = data.signature;
-
var encryptedData = data.encryptedData;
-
var iv = data.iv;
-
wx.request({
-
url: "你的后臺(tái)地址",
-
data: {
-
"code" : code,
-
" rawData" : rawData,
-
"signature" : signature,
-
" iv" : iv,
-
"encryptedData" : encryptedData
-
},
-
method: 'GET',
-
success: function(res){
-
-
console.log(res)
-
console.log(rawData)
-
}
-
})
-
}
-
})
-
}
-
})
-
}
-
},
-
globalData:{
-
userInfo:null
-
},
-
})
查看微信小程序端的 network 可能查看請(qǐng)求是否成功
thinkphp后臺(tái)代碼
-
public function sendCode(){
-
$APPID = '################APPID';
-
$AppSecret = '#################';
-
$code = input('get.code');
-
$url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$APPID.'&secret='.$AppSecret.'&js_code='.$code.'&grant_type=authorization_code';
-
$arr = $this -> vegt($url);
-
-
$arr = json_decode($arr,true);
-
-
$session_key = $arr['session_key'];
-
-
-
$signature = input('get.signature');
-
$signature2 = sha1($_GET['rawData'].$session_key);
-
if($signature != $signature2){
-
echo "數(shù)字簽名失敗";
-
die;
-
}
-
-
Vendor("PHP.wxBizDataCrypt");
-
$encryptedData = $_GET['encryptedData'];
-
$iv = $_GET['iv'];
-
if(empty($signature) || empty($encryptedData) || empty($iv)){
-
echo "傳遞信息不全";
-
}
-
include_once "PHP/wxBizDataCrypt.php";
-
$pc = new \WXBizDataCrypt($APPID,$session_key);
-
$errCode = $pc->decryptData($encryptedData,$iv,$data);
-
if($errCode != 0){
-
echo "解密數(shù)據(jù)失敗";
-
die;
-
}else {
-
$data = json_decode($data,true);
-
session('myinfo',$data);
-
$save['openid'] = $data['openId'];
-
$save['uname'] = $data['nickName'];
-
$save['unex'] = $data['gender'];
-
$save['address'] = $data['city'];
-
$save['time'] = time();
-
$map['openid'] = $data['openId'];
-
!empty($data['unionId']) && $save['unionId'] = $data['unionId'];
-
-
$res = \think\Db::name('user') -> where($map) -> find();
-
if(!$res){
-
$db = \think\Db::name('user') -> insert($save);
-
if($db !== false){
-
echo "保存用戶成功";
-
}else{
-
echo "error";
-
}
-
}else{
-
echo "用戶已經(jīng)存在";
-
}
-
}
-
-
$session3rd = null;
-
$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
-
$max = strlen($strPol)-1;
-
for($i=0;$i<16;$i++){
-
$session3rd .=$strPol[rand(0,$max)];
-
}
-
-
}
-
public function vegt($url){
-
$info = curl_init();
-
curl_setopt($info,CURLOPT_RETURNTRANSFER,true);
-
curl_setopt($info,CURLOPT_HEADER,0);
-
curl_setopt($info,CURLOPT_NOBODY,0);
-
curl_setopt($info,CURLOPT_SSL_VERIFYPEER, false);
-
curl_setopt($info,CURLOPT_SSL_VERIFYHOST, false);
-
curl_setopt($info,CURLOPT_URL,$url);
-
$output= curl_exec($info);
-
curl_close($info);
-
return $output;
-
}
官網(wǎng)有加解密的文件自行下載: https://www.w3cschool.cn/weixinapp/weixinapp-signature.html
寫(xiě)的不好的地方歡迎補(bǔ)充,這也是小編經(jīng)過(guò)多處得出學(xué)習(xí)得出的