2021-07-28 10:04:47 +08:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* @copyright Copyright (c) 2021 勾股工作室
|
2021-11-24 17:17:29 +08:00
|
|
|
|
* @license https://opensource.org/licenses/Apache-2.0
|
2021-07-28 10:04:47 +08:00
|
|
|
|
* @link https://www.gougucms.com
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace app\api\middleware;
|
|
|
|
|
|
2022-07-25 10:50:07 +08:00
|
|
|
|
use Firebase\JWT\JWT;
|
|
|
|
|
use Firebase\JWT\Key;
|
2023-01-30 16:33:01 +08:00
|
|
|
|
use think\Config;
|
2023-01-18 17:10:33 +08:00
|
|
|
|
use think\facade\Db;
|
2021-07-28 10:04:47 +08:00
|
|
|
|
use think\facade\Request;
|
|
|
|
|
use think\Response;
|
|
|
|
|
|
|
|
|
|
class Auth
|
|
|
|
|
{
|
2023-01-30 16:33:01 +08:00
|
|
|
|
public function handle($request, \Closure $next, ? array $header = [])
|
2021-07-28 10:04:47 +08:00
|
|
|
|
{
|
2023-01-30 16:33:01 +08:00
|
|
|
|
|
2023-01-18 17:10:33 +08:00
|
|
|
|
$token = Request::header('x-Token');
|
2021-07-28 10:04:47 +08:00
|
|
|
|
if ($token) {
|
2023-01-18 17:10:33 +08:00
|
|
|
|
if (strpos($token, 'Bearer') === 0){
|
|
|
|
|
$token = trim(substr($token, 6));
|
|
|
|
|
}
|
2021-07-28 10:04:47 +08:00
|
|
|
|
if (count(explode('.', $token)) != 3) {
|
2022-07-25 10:50:07 +08:00
|
|
|
|
return json(['code'=>404,'msg'=>'非法请求']);
|
2021-07-28 10:04:47 +08:00
|
|
|
|
}
|
2022-07-25 10:50:07 +08:00
|
|
|
|
try {
|
|
|
|
|
JWT::$leeway = 60;//当前时间减去60,把时间留点余地
|
2023-01-18 17:10:33 +08:00
|
|
|
|
$decoded = JWT::decode($token, new Key('ae47e94a7dcd1fdfacb499b60e361a8d', 'HS256')); //HS256方式,这里要和签发的时候对应
|
2023-02-02 11:56:19 +08:00
|
|
|
|
$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);
|
2022-07-25 10:50:07 +08:00
|
|
|
|
} 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'=>'非法请求']);
|
|
|
|
|
}
|
2021-07-28 10:04:47 +08:00
|
|
|
|
} else {
|
2022-07-25 10:50:07 +08:00
|
|
|
|
return json(['code'=>404,'msg'=>'token不能为空']);
|
2021-07-28 10:04:47 +08:00
|
|
|
|
}
|
2023-02-02 11:56:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()]);
|
|
|
|
|
}
|
2021-07-28 10:04:47 +08:00
|
|
|
|
}
|
2022-07-25 10:50:07 +08:00
|
|
|
|
}
|