统一规范状态码

This commit is contained in:
2026-03-04 16:07:07 +08:00
parent 5d0e2a82ff
commit a6858adf14
5 changed files with 50 additions and 24 deletions

View File

@@ -10,6 +10,7 @@ use Webman\MiddlewareInterface;
use Tinywan\Jwt\JwtToken;
use Tinywan\Jwt\Exception\JwtTokenException;
use Tinywan\Jwt\Exception\JwtTokenExpiredException;
use app\api\util\ReturnCode;
use plugin\saiadmin\exception\ApiException;
/**
@@ -38,7 +39,7 @@ class CheckApiAuthMiddleware implements MiddlewareInterface
}
}
if (empty($token)) {
throw new ApiException('缺少 auth-token,请先调用 /api/authToken 获取', 401);
throw new ApiException('请携带 auth-token', ReturnCode::MISSING_TOKEN);
}
try {
@@ -46,17 +47,17 @@ class CheckApiAuthMiddleware implements MiddlewareInterface
$decoded = JwtToken::verify(1, $token);
$extend = $decoded['extend'] ?? [];
if (($extend['plat'] ?? '') !== 'api') {
throw new ApiException('auth-token 无效', 401);
throw new ApiException('auth-token 无效', ReturnCode::TOKEN_TIMEOUT);
}
} catch (JwtTokenExpiredException $e) {
Log::error('code=401, auth-token 已过期,请重新获取, 报错信息'. $e);
throw new ApiException('auth-token 已过期,请重新获取', 401);
Log::error('auth-token 已过期, 报错信息'. $e);
throw new ApiException('auth-token 已过期', ReturnCode::TOKEN_TIMEOUT);
} catch (JwtTokenException $e) {
Log::error('code=401, message=auth-token 无效, 报错信息'. $e);
throw new ApiException($e->getMessage() ?: 'auth-token 无效', 401);
Log::error('auth-token 无效, 报错信息'. $e);
throw new ApiException($e->getMessage() ?: 'auth-token 无效', ReturnCode::TOKEN_TIMEOUT);
} catch (\Throwable $e) {
Log::error('code=401, message=auth-token 校验失败, 报错信息'. $e);
throw new ApiException('auth-token 校验失败', 401);
Log::error('auth-token 校验失败, 报错信息'. $e);
throw new ApiException('auth-token 校验失败', ReturnCode::TOKEN_TIMEOUT);
}
return $handler($request);