首先需要看懂微信小程序开发文档的 开放接口中的 登录 和 用户信息。
wx.login(OBJECT)
调用接口获取登录凭证(code)进而换取用户登录态信息,包括用户的唯一标识(openid) 及本次登录的 会话密钥(session_key)等。用户数据的加解密通讯需要依赖会话密钥完成。
code 换取 session_key
这是一个 HTTPS 接口,开发者服务器使用登录凭证 code 获取 session_key 和 openid。
session_key 是对用户数据进行加密签名的密钥。为了自身应用安全,session_key 不应该在网络上传输。
小程序端的代码:
-
-
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: "你的后台地址",
-
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 可能查看请求是否成功
thinkphp后台代码
-
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 "数字签名失败";
-
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 "解密数据失败";
-
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 "用户已经存在";
-
}
-
}
-
-
$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;
-
}
官网有加解密的文件自行下载: https://www.w3cschool.cn/weixinapp/weixinapp-signature.html
写的不好的地方欢迎补充,这也是小编经过多处得出学习得出的