catchAdmin/app/Request.php

47 lines
1.1 KiB
PHP
Raw Normal View History

2019-12-02 23:04:43 +08:00
<?php
namespace app;
// 应用请求对象类
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;
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-01-17 11:30:39 +08:00
/**
* login user
*
* @time 2020年01月09日
* @return mixed
*/
public function user()
{
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 {
$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;
2020-01-17 11:30:39 +08:00
}
2019-12-02 23:04:43 +08:00
}