107 lines
4.0 KiB
PHP
107 lines
4.0 KiB
PHP
<?php
|
||
/**
|
||
* @copyright Copyright (c) 2021 勾股工作室
|
||
* @license https://opensource.org/licenses/Apache-2.0
|
||
* @link https://www.gougucms.com
|
||
*/
|
||
|
||
namespace app\api\middleware;
|
||
|
||
use Firebase\JWT\JWT;
|
||
use Firebase\JWT\Key;
|
||
use think\Config;
|
||
use think\facade\Db;
|
||
use think\facade\Request;
|
||
use think\Response;
|
||
|
||
class Auth
|
||
{
|
||
public function handle($request, \Closure $next, ? array $header = [])
|
||
{
|
||
|
||
$token = Request::header('x-Token');
|
||
if ($token) {
|
||
if (strpos($token, 'Bearer') === 0){
|
||
$token = trim(substr($token, 6));
|
||
}
|
||
if (count(explode('.', $token)) != 3) {
|
||
return json(['code'=>404,'msg'=>'非法请求']);
|
||
}
|
||
try {
|
||
JWT::$leeway = 60;//当前时间减去60,把时间留点余地
|
||
$decoded = JWT::decode($token, new Key('ae47e94a7dcd1fdfacb499b60e361a8d', 'HS256')); //HS256方式,这里要和签发的时候对应
|
||
$user=Db::connect('shop')->name('nk_user')->where('user_id',$decoded->jti[0])->find();
|
||
if ($user &&$user['n_user_id']!=0){
|
||
$request->uid=$user['n_user_id'];
|
||
}else{
|
||
$request->uid=$this->addUser($decoded->jti[0],$user);
|
||
}
|
||
return $next($request);
|
||
} catch(\Firebase\JWT\SignatureInvalidException $e) { //签名不正确
|
||
return json(['code'=>403,'msg'=>'签名错误']);
|
||
}catch(\Firebase\JWT\BeforeValidException $e) { // 签名在某个时间点之后才能用
|
||
return json(['code'=>401,'msg'=>'token失效']);
|
||
}catch(\Firebase\JWT\ExpiredException $e) { // token过期
|
||
return json(['code'=>401,'msg'=>'token已过期']);
|
||
}catch(Exception $e) { //其他错误
|
||
return json(['code'=>404,'msg'=>'非法请求']);
|
||
}catch(\UnexpectedValueException $e) { //其他错误
|
||
return json(['code'=>404,'msg'=>'非法请求']);
|
||
} catch(\DomainException $e) { //其他错误
|
||
return json(['code'=>404,'msg'=>'非法请求']);
|
||
}
|
||
} else {
|
||
return json(['code'=>404,'msg'=>'token不能为空']);
|
||
}
|
||
}
|
||
|
||
public function addUser($uid,$nk_user){
|
||
$user=Db::connect('shop')->name('user')->where('uid',$uid)->find();
|
||
|
||
$msg=Db::table('fa_szxc_information_usermsg')->where('phone',$user['account'])->find();
|
||
Db::startTrans();
|
||
try {
|
||
if ($msg){
|
||
$users=Db::table('fa_user')->where('id',$msg['user_id'])->find();
|
||
if (!$nk_user){
|
||
$datas=[
|
||
'user_id'=>$user['uid'],
|
||
'n_user_id'=>$msg['user_id'],
|
||
'group_id'=>$users['group_id']
|
||
];
|
||
Db::connect('shop')->name('nk_user')->insert($datas);
|
||
}else{
|
||
Db::connect('shop')->name('nk_user')->where('id',$nk_user['id'])->update(['user_id'=>$user['uid'],'group_id'=>$users['group_id']]);
|
||
}
|
||
}else{
|
||
$time=time();
|
||
$user_data=[
|
||
'openid'=>'wx'.$time,
|
||
'group_id'=>1,
|
||
'username'=>'wx'.$time,
|
||
'nickname'=>'微信用户'.$time,
|
||
'avatar'=>'https://lihai001.oss-cn-chengdu.aliyuncs.com/uploads/20230104/32a639be4ee32349705e947fbbd1e114.png',
|
||
'level'=>1,
|
||
'prevtime'=>$time,
|
||
'jointime'=>$time,
|
||
'createtime'=>$time,
|
||
'updatetime'=>$time,
|
||
'status'=>'normal'
|
||
];
|
||
$n_user_id= Db::table('fa_user')->insertGetId($user_data);
|
||
$datas=[
|
||
'user_id'=>$user['uid'],
|
||
'n_user_id'=>$n_user_id,
|
||
'group_id'=>1
|
||
];
|
||
Db::connect('shop')->name('nk_user')->insert($datas);
|
||
return $n_user_id;
|
||
}
|
||
Db::commit();
|
||
} catch (\Exception $e) {
|
||
// 回滚事务
|
||
Db::rollback();
|
||
return json(['code'=>400,'msg'=>$e->getMessage()]);
|
||
}
|
||
}
|
||
} |