增加基础

This commit is contained in:
wuyanwen
2019-12-12 18:52:11 +08:00
parent ca4272d7a6
commit 5f6a7cf24e
16 changed files with 102 additions and 32 deletions

View File

@@ -2,6 +2,9 @@
namespace app;
use catcher\CatchResponse;
use catcher\exceptions\FailedException;
use catcher\exceptions\LoginFailedException;
use catcher\exceptions\PermissionForbiddenException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\Handle;
@@ -28,6 +31,12 @@ class ExceptionHandle extends Handle
ValidateException::class,
];
protected $catchExceptions = [
FailedException::class,
LoginFailedException::class,
ValidateException::class,
PermissionForbiddenException::class,
];
/**
* 记录异常信息(包括日志或者其它方式记录)
*
@@ -45,16 +54,17 @@ class ExceptionHandle extends Handle
* Render an exception into an HTTP response.
*
* @access public
* @param \think\Request $request
* @param \think\Request $request
* @param Throwable $e
* @return Response
* @throws \Exception
*/
public function render($request, Throwable $e): Response
{
// 添加自定义异常处理机制
if ($request->isAjax()) {
if (in_array(get_class($e), $this->catchExceptions)) {
return CatchResponse::fail($e->getMessage(), $e->getCode());
}
// 其他错误交给系统处理
return parent::render($request, $e);
}

View File

@@ -3,7 +3,12 @@ namespace app;
// 应用请求对象类
use catchAdmin\user\Auth;
class Request extends \think\Request
{
public function user()
{
return Auth::user();
}
}