login: function(e) {
var that = this;
wx.login({
success: function(res){
var code = res.code;
wx.getUserInfo({
success: function(data){
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(info){
console.log(info);
}
})
}
})
},
})
}
php代码
- 小程序登录文档
- 数据进行加密签名的密钥(必须要下载引入你的类) 加密数据解密算法 有Demo文件
namespace app\index\controller;
use app\BaseController;
use app\index\controller\Wxgetuserinfo;
use think\facade\view;
use think\facade\DB;
use app\index\service\Weixin;
class WxMini extends BaseController
{
public function Decode()
{
$APPID = '';
$AppSecret = '';
$code = $this->request->param('code','','trim');
$url="https://api.weixin.qq.com/sns/jscode2session?appid=".$APPID."&secret=".$AppSecret."&js_code=".$code."&grant_type=authorization_code";
$arr = $this->vget($url);
$arr = json_decode($arr,true);
$openid = $arr['openid'];
$session_key = $arr['session_key'];
$signature = $this->request->param('signature','','trim');
$signature2 = sha1($_GET['rawData'].$session_key);
if ($signature != $signature2) {
echo '数据签名验证失败!';die;
}
include_once "wxBizDataCrypt.php";
$encryptedData = $_GET['encryptedData'];
$iv = $_GET['iv'];
$pc = new \WXBizDataCrypt($APPID, $session_key);
$errCode = $pc->decryptData($encryptedData, $iv, $data);
if ($errCode != 0) {
echo '解密数据失败!';die;
}
$session3rd = null;
$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
$max = strlen($strPol)-1;
for($i=0;$i<16;$i++){
$session3rd .=$strPol[rand(0,$max)];
}
echo $session3rd;
}
public function vget($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;
}
}