catchAdmin/app/Request.php

54 lines
1.4 KiB
PHP
Raw Normal View History

2019-12-02 23:04:43 +08:00
<?php
namespace app;
// 应用请求对象类
2020-08-20 21:02:16 +08:00
use catchAdmin\permissions\model\Users;
2020-01-08 22:43:40 +08:00
use catcher\CatchAuth;
2020-05-19 08:22:48 +08:00
use catcher\Code;
use catcher\exceptions\FailedException;
2020-08-20 21:02:16 +08:00
use catcher\exceptions\LoginFailedException;
2020-05-19 08:22:48 +08:00
use thans\jwt\exception\TokenBlacklistException;
use thans\jwt\exception\TokenExpiredException;
use thans\jwt\exception\TokenInvalidException;
2019-12-12 18:52:11 +08:00
2019-12-02 23:04:43 +08:00
class Request extends \think\Request
{
2020-01-08 22:43:40 +08:00
protected $auth;
2020-07-13 17:07:18 +08:00
/**
* login user
*
* @time 2020年01月09日
* @param null $guard
* @return mixed
*/
public function user($guard = null)
2020-01-17 11:30:39 +08:00
{
if (!$this->auth) {
$this->auth = new CatchAuth;
2019-12-12 18:52:11 +08:00
}
2020-01-17 11:30:39 +08:00
2020-05-19 08:22:48 +08:00
try {
2020-07-13 17:07:18 +08:00
$user = $this->auth->guard($guard ? : config('catch.auth.default.guard'))->user();
2020-08-20 21:02:16 +08:00
if ($user->status == Users::DISABLE) {
throw new LoginFailedException('该用户已被禁用');
}
2020-05-19 08:22:48 +08:00
} 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);
}
2020-06-30 17:42:07 +08:00
throw new FailedException('认证失败: '. $e->getMessage(), Code::LOST_LOGIN);
2020-05-19 08:22:48 +08:00
}
return $user;
2020-01-17 11:30:39 +08:00
}
2019-12-02 23:04:43 +08:00
}