public function get_session_by_code() {
$code = input(‘post.code‘,‘‘);
$iv = input(‘post.iv‘,‘‘);
$encryptedData = input(‘post.encryptedData‘,‘‘);
$userInfo = input(‘post.userInfo‘,‘‘);
// 接受code 判断为空
if (empty($code)) {
return json([‘code‘=>1001,‘msg‘=>‘无效的 code‘]);
}
// 获取配置好的appid
$wechat_app_id = config(‘wechat.app_id‘);
$wechat_app_secret = config(‘wechat.app_secret‘);
$url = "https://api.weixin.qq.com/sns/jscode2session?"."appid=".$wechat_app_id."&secret=".$wechat_app_secret."&js_code=".$code."&grant_type=authorization_code";
$res = json_decode($this->httpGet($url),true);
$sessionKey = $res[‘session_key‘];
$data[‘session_key‘]=$res[‘session_key‘];
$data[‘open_id‘]=$res[‘openid‘];
$data[‘name‘]=$this->removeEmoji($userInfo[‘nickName‘]);
$data[‘img‘]=$userInfo[‘avatarUrl‘];
$data[‘add_time‘]=time();
$pc = new WXBizDataCrypt($wechat_app_id, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $data1 );
$open_id = Db::name(‘user‘)->where(‘open_id‘,$res[‘openid‘])->find();
if(empty($open_id)){
$register = Db::name(‘user‘)->insertGetId($data);
$datalist = Db::name(‘user‘)->where(‘id‘,$register)->field(‘id,name,phone,img‘)->select();
}else{
$time=time();
$register = Db::name(‘user‘)->where(‘open_id‘,$res[‘openid‘])->update([‘login_time‘=>$time]);
$datalist = Db::name(‘user‘)->where(‘open_id‘,$res[‘openid‘])->field(‘id,name,phone,img‘)->select();
}
if ($register) {
return json([‘code‘=>1000,‘msg‘=>‘成功‘,‘data‘=>$datalist]);
}else{
return json([‘code‘=>1001,‘msg‘=>‘失败‘]);
}
}
// 过滤微信昵称
public function removeEmoji($text) {
$clean_text = "";
// Match Emoticons
$regexEmoticons = ‘/[\x{1F600}-\x{1F64F}]/u‘;
$clean_text = preg_replace($regexEmoticons, ‘‘, $text);
// Match Miscellaneous Symbols and Pictographs
$regexSymbols = ‘/[\x{1F300}-\x{1F5FF}]/u‘;
$clean_text = preg_replace($regexSymbols, ‘‘, $clean_text);
// Match Transport And Map Symbols
$regexTransport = ‘/[\x{1F680}-\x{1F6FF}]/u‘;
$clean_text = preg_replace($regexTransport, ‘‘, $clean_text);
// Match Miscellaneous Symbols
$regexMisc = ‘/[\x{2600}-\x{26FF}]/u‘;
$clean_text = preg_replace($regexMisc, ‘‘, $clean_text);
// Match Dingbats
$regexDingbats = ‘/[\x{2700}-\x{27BF}]/u‘;
$clean_text = preg_replace($regexDingbats, ‘‘, $clean_text);
return $clean_text;
}
//获取手机号
public function getPhone(){
$iv = input(‘post.iv‘,‘‘);
$id = input(‘post.id‘,‘‘);
$encryptedData = input(‘post.encryptedData‘,‘‘);
$code = input(‘post.code‘,‘‘);
$wechat_app_id = config(‘wechat.app_id‘);
$wechat_app_secret = config(‘wechat.app_secret‘);
$url = "https://api.weixin.qq.com/sns/jscode2session?"."appid=".$wechat_app_id."&secret=".$wechat_app_secret."&js_code=".$code."&grant_type=authorization_code";
$res = json_decode($this->httpGet($url),true);
$sessionKey = $res[‘session_key‘];
$pc = new WXBizDataCrypt($wechat_app_id, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $data1 );
$data1 = json_decode($data1, true);
//var_dump($data1);
$es = Db::name(‘user‘)->where(‘open_id‘,$res[‘openid‘])->update([‘phone‘=>$data1[‘phoneNumber‘]]);
if ($es) {
return json([‘code‘=>1000,‘msg‘=>‘成功‘,‘data‘=>$data1[‘phoneNumber‘]]);
}else{
return json([‘code‘=>1001,‘msg‘=>‘失败‘]);
}
}
前端传入code 后端通过app_id app_secret 来获取 openid key 在通过demo解析用户信息
特别注意 获取手机号 登录 事件要同步执行
解析demo
<?php
/**
* 对微信小程序用户加密数据的解密示例代码.
*
* @copyright Copyright (c) 1998-2014 Tencent Inc.
*/
namespace wechat;
class WXBizDataCrypt
{
private $appid;
private $sessionKey;
/**
* 构造函数
* @param $sessionKey string 用户在小程序登录后获取的会话密钥
* @param $appid string 小程序的appid
*/
public function __construct( $appid, $sessionKey)
{
$this->sessionKey = $sessionKey;
$this->appid = $appid;
}
/**
* 检验数据的真实性,并且获取解密后的明文.
* @param $encryptedData string 加密的用户数据
* @param $iv string 与用户数据一同返回的初始向量
* @param $data string 解密后的原文
*
* @return int 成功0,失败返回对应的错误码
*/
public function decryptData( $encryptedData, $iv, &$data )
{
if (strlen($this->sessionKey) != 24) {
return -41001;
}
$aesKey=base64_decode($this->sessionKey);
if (strlen($iv) != 24) {
return -41002;
}
$aesIV=base64_decode($iv);
$aesCipher=base64_decode($encryptedData);
$result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
$dataObj=json_decode( $result );
if( $dataObj == NULL )
{
return -41003;
}
if( $dataObj->watermark->appid != $this->appid )
{
return -41003;
}
$data = $result;
return 0;
}
}