diff --git a/app/Request.php b/app/Request.php index 7e3a236..edcc841 100644 --- a/app/Request.php +++ b/app/Request.php @@ -4,6 +4,11 @@ namespace app; // 应用请求对象类 use catcher\CatchAuth; +use catcher\Code; +use catcher\exceptions\FailedException; +use thans\jwt\exception\TokenBlacklistException; +use thans\jwt\exception\TokenExpiredException; +use thans\jwt\exception\TokenInvalidException; class Request extends \think\Request { @@ -21,6 +26,21 @@ class Request extends \think\Request $this->auth = new CatchAuth; } - return $this->auth->user(); + try { + $user = $this->auth->user(); + } catch (\Exception $e) { + if ($e instanceof TokenExpiredException) { + throw new FailedException('token 过期', Code::LOGIN_EXPIRED); + } + if ($e instanceof TokenBlacklistException) { + throw new FailedException('token 被加入黑名单', Code::LOGIN_BLACKLIST); + } + if ($e instanceof TokenInvalidException) { + throw new FailedException('token 不合法', Code::LOST_LOGIN); + } + throw new FailedException('auth failed', Code::LOST_LOGIN); + } + + return $user; } } diff --git a/catch/permissions/AuthTokenMiddleware.php b/catch/permissions/AuthTokenMiddleware.php index 1f7960d..8e0bbee 100644 --- a/catch/permissions/AuthTokenMiddleware.php +++ b/catch/permissions/AuthTokenMiddleware.php @@ -20,7 +20,7 @@ class AuthTokenMiddleware extends Middleware throw new FailedException('token 过期', Code::LOGIN_EXPIRED); } if ($e instanceof TokenBlacklistException) { - throw new FailedException('token 被加入黑名单', Code::LOST_LOGIN); + throw new FailedException('token 被加入黑名单', Code::LOGIN_BLACKLIST); } if ($e instanceof TokenInvalidException) { throw new FailedException('token 不合法', Code::LOST_LOGIN); diff --git a/extend/catcher/Code.php b/extend/catcher/Code.php index 2c41233..d570a39 100644 --- a/extend/catcher/Code.php +++ b/extend/catcher/Code.php @@ -10,4 +10,5 @@ class Code public const LOGIN_FAILED = 10004; // 登录失败 public const FAILED = 10005; // 操作失败 public const LOGIN_EXPIRED = 10006; // 登录失效 + public const LOGIN_BLACKLIST = 10007; // 黑名单 }