50 lines
1.8 KiB
PHP
50 lines
1.8 KiB
PHP
<?php
|
|
declare (strict_types=1);
|
|
|
|
namespace app\middleware;
|
|
|
|
use app\config\BusinessCacheKey;
|
|
use app\config\BusinessCode;
|
|
use app\exception\BusinessException;
|
|
use app\service\util\RedisService;
|
|
use app\util\FrontSessionUtil;
|
|
use Firebase\JWT\ExpiredException;
|
|
use Firebase\JWT\SignatureInvalidException;
|
|
|
|
class FrontRequest
|
|
{
|
|
/**
|
|
* 处理请求
|
|
*/
|
|
public function handle(\think\Request $request, \Closure $next): \think\Response
|
|
{
|
|
// $token = FrontSessionUtil::getToken();
|
|
// if (empty($token)) {
|
|
// throw new BusinessException("未登录", BusinessCode::LOGIN_INVALID);
|
|
// }
|
|
// $client = RedisService::getRedisInstance();
|
|
// if (!$client->sismember(BusinessCacheKey::FRONT_TOKEN_LIST['key'], $token)) {
|
|
// throw new BusinessException("token已过期", BusinessCode::LOGIN_INVALID);
|
|
// }
|
|
// try {
|
|
// $data = \Firebase\JWT\JWT::decode($token, new \Firebase\JWT\Key(env('jwt_token_key'), 'HS256'));
|
|
// FrontSessionUtil::setUser($data->data);
|
|
// } catch (ExpiredException $e) {
|
|
// throw new BusinessException('请重新授权登陆', BusinessCode::LOGIN_INVALID);
|
|
// } catch (SignatureInvalidException|\Throwable $e) {
|
|
// throw new BusinessException('签名验证失败', BusinessCode::LOGIN_INVALID);
|
|
// }
|
|
return $next($request);
|
|
}
|
|
|
|
public function end(\think\Response $response): \think\Response
|
|
{
|
|
$response->header([
|
|
'Access-Control-Allow-Origin' => '*',
|
|
'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS',
|
|
'Access-Control-Allow-Headers' => 'X-Requested-With, Content-Type, Accept, Origin, token,Status Code,frontToken'
|
|
]);
|
|
return $response;
|
|
}
|
|
}
|