feat : jwt
Some checks are pending
Build Docker / build (push) Waiting to run

This commit is contained in:
2025-09-12 18:21:19 +08:00
parent ff3e0105ec
commit ea6abe3043
2 changed files with 15 additions and 5 deletions

View File

@@ -3,6 +3,7 @@
namespace App\Exception\Handler;
use App\Constants\ResultCode;
use App\Exception\ErrException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
@@ -30,17 +31,24 @@ class JwtExceptionHandler extends BaseErrExceptionHandler
return $response;
}
protected function modifyException(Throwable $e): Throwable
protected function modifyException(JWTException $e): Throwable
{
// 根据不同的异常类型设置不同的code和message
switch ($e->getMessage()) {
case 'The token is expired':
$e->code = ResultCode::JWT_EXPIRED;
$e->message = 'token已过期';
$code = ResultCode::JWT_EXPIRED;
$message = 'token已过期';
break;
default:
$e->code = ResultCode::JWT_ERROR;
$e->message = 'token错误';
$code = ResultCode::JWT_ERROR;
$message = 'token错误';
}
if (method_exists($e, 'setCustomCode')) {
$e->setCustomCode($code);
}
if (method_exists($e, 'setCustomMessage')) {
$e->setCustomMessage($message);
}
return $e;

View File

@@ -9,6 +9,7 @@ declare(strict_types=1);
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
return [
'handler' => [
'http' => [
@@ -16,6 +17,7 @@ return [
App\Exception\Handler\ErrExceptionHandler::class,
App\Exception\Handler\ValidationExceptionHandler::class,
App\Exception\Handler\AppExceptionHandler::class,
App\Exception\Handler\JwtExceptionHandler::class,
],
],
];