fixed bug

This commit is contained in:
yanwenwu 2020-05-19 08:22:48 +08:00
parent 9da4072b30
commit 64ce1243b4
3 changed files with 23 additions and 2 deletions

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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; // 黑名单
}