From 70889deb7368d758dbc838c66b37f005f9cdd3b4 Mon Sep 17 00:00:00 2001 From: yanwnewu <82664165@qq.com> Date: Fri, 30 Nov 2018 18:04:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/request/FormRequest.php | 16 ++------- application/exceptions/Handle.php | 44 +++++++++++++++++++++++ config/app.php | 2 +- 3 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 application/exceptions/Handle.php diff --git a/application/admin/request/FormRequest.php b/application/admin/request/FormRequest.php index 44bedf0..3a839d1 100644 --- a/application/admin/request/FormRequest.php +++ b/application/admin/request/FormRequest.php @@ -6,6 +6,7 @@ */ namespace app\admin\request; +use think\exception\HttpException; use think\Request; abstract class FormRequest extends Request @@ -19,20 +20,7 @@ abstract class FormRequest extends Request parent::__construct(); if ($this->withServer($_SERVER)->isAjax(true) && $err = $this->validate()) { - exit($this->error($err)); + throw new HttpException(0, $err); } } - - protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = []) - { - - return json([ - 'code' => 0, - 'msg' => $msg, - 'data' => $data, - 'url' => $url, - 'wait' => $wait, - ])->send(); - } - } \ No newline at end of file diff --git a/application/exceptions/Handle.php b/application/exceptions/Handle.php new file mode 100644 index 0000000..25aadd5 --- /dev/null +++ b/application/exceptions/Handle.php @@ -0,0 +1,44 @@ +error($e->getMessage()); + break; + default: + return $this->convertExceptionToResponse($e); + break; + } + } + + protected function error($msg = '', $url = null, $data = '', $wait = 3, array $header = []) + { + return json([ + 'code' => 0, + 'msg' => $msg, + 'data' => $data, + 'url' => $url, + 'wait' => $wait, + ]); + } +} \ No newline at end of file diff --git a/config/app.php b/config/app.php index cdd7da1..4a26e13 100644 --- a/config/app.php +++ b/config/app.php @@ -141,6 +141,6 @@ return [ // 显示错误信息 'show_error_msg' => false, // 异常处理handle类 留空使用 \think\exception\Handle - 'exception_handle' => '', + 'exception_handle' => app\exceptions\Handle::class, ];