12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <?php
- namespace app\sub\library;
- use app\UserException;
- use Exception;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\ModelNotFoundException;
- use think\exception\Handle;
- class ExceptionHandle extends Handle
- {
- protected $ignoreReport = [
- '\\think\\exception\\HttpException',
- UserException::class,
- ];
- public function render(Exception $e)
- {
-
-
- if(request()->isAjax() || strpos(request()->header('Accept'),'/json')!==false) {
- $statuscode = $code = 0;
-
- $msg = $e->getMessage();
-
- if ($e instanceof \think\exception\ValidateException) {
- $code = 0;
- $statuscode = 200;
- $msg = $e->getError();
- }
-
- if ($e instanceof \think\exception\HttpException) {
- $statuscode = $code = $e->getStatusCode();
- }
- if ($e instanceof UserException) {
- $code = 0;
- $msg = $e->getMessage();
- $statuscode = 200;
- }
- if ($e instanceof ModelNotFoundException) {
- $msg = '查找的数据不存在';
- }
- return json(['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => null], $statuscode);
- }
-
-
- return parent::render($e);
- }
- }
|