切换中间件,防止串应用

This commit is contained in:
wuyanwen
2020-02-18 14:44:16 +08:00
parent fd1798875f
commit ebe5f5757a
3 changed files with 65 additions and 66 deletions

View File

@@ -99,9 +99,7 @@ class CatchAdminService extends Service
*/ */
protected function registerMiddleWares(): void protected function registerMiddleWares(): void
{ {
$this->app->middleware->import([ // todo
'catch_check_permission' => PermissionsMiddleware::class,
], 'route');
} }
/** /**

View File

@@ -1,61 +1,61 @@
<?php <?php
namespace catcher; namespace catcher;
use app\ExceptionHandle; use app\ExceptionHandle;
use catcher\exceptions\CatchException; use catcher\exceptions\CatchException;
use catcher\exceptions\FailedException; use catcher\exceptions\FailedException;
use think\db\exception\DataNotFoundException; use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException; use think\db\exception\ModelNotFoundException;
use think\exception\Handle; use think\exception\Handle;
use think\exception\HttpException; use think\exception\HttpException;
use think\exception\HttpResponseException; use think\exception\HttpResponseException;
use think\exception\ValidateException; use think\exception\ValidateException;
use think\Response; use think\Response;
use Throwable; use Throwable;
class CatchExceptionHandle extends Handle class CatchExceptionHandle extends Handle
{ {
/** /**
* 不需要记录信息(日志)的异常类列表 * 不需要记录信息(日志)的异常类列表
* @var array * @var array
*/ */
protected $ignoreReport = [ protected $ignoreReport = [
HttpException::class, HttpException::class,
HttpResponseException::class, HttpResponseException::class,
ModelNotFoundException::class, ModelNotFoundException::class,
DataNotFoundException::class, DataNotFoundException::class,
ValidateException::class, ValidateException::class,
]; ];
/** /**
* 记录异常信息(包括日志或者其它方式记录) * 记录异常信息(包括日志或者其它方式记录)
* *
* @access public * @access public
* @param Throwable $exception * @param Throwable $exception
* @return void * @return void
*/ */
public function report(Throwable $exception): void public function report(Throwable $exception): void
{ {
// 使用内置的方式记录异常日志 // 使用内置的方式记录异常日志
parent::report($exception); parent::report($exception);
} }
/** /**
* Render an exception into an HTTP response. * Render an exception into an HTTP response.
* *
* @access public * @access public
* @param \think\Request $request * @param \think\Request $request
* @param Throwable $e * @param Throwable $e
* @return Response * @return Response
* @throws \Exception * @throws \Exception
*/ */
public function render($request, Throwable $e): Response public function render($request, Throwable $e): Response
{ {
// 其他错误交给系统处理 // 其他错误交给系统处理
if (!$e instanceof CatchException) { if (!$e instanceof CatchException) {
$e = new FailedException($e->getMessage()); $e = new FailedException($e->getMessage());
} }
return parent::render($request, $e); return parent::render($request, $e);
} }
} }

View File

@@ -3,6 +3,7 @@ declare (strict_types = 1);
namespace catcher\event; namespace catcher\event;
use catchAdmin\permissions\PermissionsMiddleware;
use catchAdmin\user\AuthTokenMiddleware; use catchAdmin\user\AuthTokenMiddleware;
use catcher\CatchAdmin; use catcher\CatchAdmin;
use think\Route; use think\Route;
@@ -28,13 +29,13 @@ class LoadModuleRoutes
foreach ($routes as $route) { foreach ($routes as $route) {
include $route; include $route;
} }
})->middleware([AuthTokenMiddleware::class]); })->middleware([AuthTokenMiddleware::class, PermissionsMiddleware::class]);
} else { } else {
$router->group(function () use ($router, $routes) { $router->group(function () use ($router, $routes) {
foreach ($routes as $route) { foreach ($routes as $route) {
include $route; include $route;
} }
})->middleware([AuthTokenMiddleware::class]); })->middleware([AuthTokenMiddleware::class, PermissionsMiddleware::class]);
} }
// 单独加载登录 // 单独加载登录