新增解析中台系统token

This commit is contained in:
yaooo 2023-11-24 10:23:08 +08:00
parent 3f791f3134
commit 9d422a2d45
2 changed files with 70 additions and 2 deletions

View File

@ -34,7 +34,7 @@ use app\common\service\FileService;
class LoginController extends BaseApiController
{
public array $notNeedLogin = ['register', 'account', 'logout', 'codeUrl', 'oaLogin', 'mnpLogin', 'getScanCode', 'scanLogin', 'shop_account','checkToken'];
public array $notNeedLogin = ['register', 'account', 'logout', 'codeUrl', 'oaLogin', 'mnpLogin', 'getScanCode', 'scanLogin', 'shop_account','checkToken', 'parseToken'];
/**
@ -274,6 +274,19 @@ class LoginController extends BaseApiController
$userInfo['company_name'] = $company['company_name'];
$userInfo['company_type'] = $company['company_type'];
}
return $this->success('请求成功',$userInfo);
return $this->success('请求成功', $userInfo);
}
public function parseToken()
{
$token = $this->request->post('token');
if(empty($token)){
return $this->fail('token参数不能为空');
}
$result = LoginLogic::parseToken($token);
if (false === $result) {
return $this->fail(LoginLogic::getError() ?? '登录失败');
}
return $this->success('请求成功', $result);
}
}

View File

@ -31,6 +31,9 @@ use app\common\model\user\{User, UserAuth};
use think\facade\{Db, Config};
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Firebase\JWT\SignatureInvalidException;
use Firebase\JWT\BeforeValidException;
use Firebase\JWT\ExpiredException;
/**
* 登录逻辑
* Class LoginLogic
@ -210,6 +213,58 @@ class LoginLogic extends BaseLogic
self::setError('无登录信息');
return false;
}
public static function parseToken($token){
$app_key = 'ae47e94a7dcd1fdfacb499b60e361a8d';
try {
JWT::$leeway = 10; //当前时间减去10秒时间留点余地
$decoded = JWT::decode($token, new Key($app_key, 'HS256'));
$decodedArray = json_decode(json_encode($decoded), true);
$jwtData = $decodedArray['data'] ?? [];
if (empty($jwtData['uid']) || empty($jwtData['phone'])) {
self::setError('解析数据缺少必要参数');
return false;
}
$where = ['account|mobile' => $jwtData['phone']];
$user = User::where($where)->findOrEmpty();
if ($user->isEmpty()) {
self::setError('用户不存在,请联系管理员开通供销系统账户');
return false;
}
//更新登录信息
$user->login_time = time();
$user->login_ip = request()->ip();
$user->save();
//设置token
$userInfo = UserTokenService::setToken($user->id, 1);
//返回登录信息
$avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
$avatar = FileService::getFileUrl($avatar);
$user_id = $jwtData['uid'];
Db::name('user_other')->insert(['user_id'=>$user->id,'other_user_id'=>$user_id,'type'=>'middle']);
return [
'nickname' => $userInfo['nickname'],
'sn' => $userInfo['sn'],
'mobile' => $userInfo['mobile'],
'avatar' => $avatar,
'token' => $userInfo['token'],
];
} catch(\Firebase\JWT\SignatureInvalidException $e) {
self::setError('签名错误');
return false;
} catch(\Firebase\JWT\BeforeValidException $e) {
self::setError('token无效');
return false;
} catch(\Firebase\JWT\ExpiredException $e) {
self::setError('token已过期');
return false;
} catch(\Exception $e) {
self::setError('非法请求');
return false;
}
return false;
}
/**
* @notes 退出登录
* @param $userInfo