作者 liyuhang

gx

@@ -2,10 +2,19 @@ @@ -2,10 +2,19 @@
2 2
3 namespace App\Exceptions; 3 namespace App\Exceptions;
4 4
  5 +use App\Enums\Common\Code;
  6 +use App\Enums\Common\Common;
  7 +use App\Services\DingService;
  8 +use App\Traits\RedisTrait;
  9 +use App\Utils\EncryptUtils;
  10 +use App\Utils\LogUtils;
  11 +use Illuminate\Database\Eloquent\ModelNotFoundException;
5 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; 12 use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
6 -  
7 use Illuminate\Support\Arr; 13 use Illuminate\Support\Arr;
8 - 14 +use Illuminate\Support\Facades\Route;
  15 +use Illuminate\Validation\ValidationException;
  16 +use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  17 +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9 use Throwable; 18 use Throwable;
10 19
11 class Handler extends ExceptionHandler 20 class Handler extends ExceptionHandler
@@ -13,7 +22,7 @@ class Handler extends ExceptionHandler @@ -13,7 +22,7 @@ class Handler extends ExceptionHandler
13 /** 22 /**
14 * A list of the exception types that are not reported. 23 * A list of the exception types that are not reported.
15 * 24 *
16 - * @var array 25 + * @var array<int, class-string<Throwable>>
17 */ 26 */
18 protected $dontReport = [ 27 protected $dontReport = [
19 // 28 //
@@ -22,14 +31,26 @@ class Handler extends ExceptionHandler @@ -22,14 +31,26 @@ class Handler extends ExceptionHandler
22 /** 31 /**
23 * A list of the inputs that are never flashed for validation exceptions. 32 * A list of the inputs that are never flashed for validation exceptions.
24 * 33 *
25 - * @var array 34 + * @var array<int, string>
26 */ 35 */
27 protected $dontFlash = [ 36 protected $dontFlash = [
  37 + 'current_password',
28 'password', 38 'password',
29 'password_confirmation', 39 'password_confirmation',
30 ]; 40 ];
31 41
32 /** 42 /**
  43 + * Register the exception handling callbacks for the application.
  44 + *
  45 + * @return void
  46 + */
  47 + public function register()
  48 + {
  49 + $this->reportable(function (Throwable $e) {
  50 + //
  51 + });
  52 + }
  53 + /**
33 * Report or log an exception. 54 * Report or log an exception.
34 * 55 *
35 * @param \Throwable $exception 56 * @param \Throwable $exception
@@ -39,9 +60,41 @@ class Handler extends ExceptionHandler @@ -39,9 +60,41 @@ class Handler extends ExceptionHandler
39 */ 60 */
40 public function report(Throwable $exception) 61 public function report(Throwable $exception)
41 { 62 {
42 - parent::report($exception);  
43 - }  
44 63
  64 + //日志记录
  65 + $exceptionMessage = "错误CODE:" . $exception->getCode() .
  66 + "-----错误message:" . $exception->getMessage() .
  67 + '------错误文件:' . $exception->getFile() .
  68 + '-------错误行数:' . $exception->getLine();
  69 + //A端错误
  70 + if ($exception instanceof AsideGlobalException) {
  71 + LogUtils::error("AsideGlobalException", [], $exceptionMessage);
  72 + }
  73 + //B端错误
  74 + elseif($exception instanceof BsideGlobalException) {
  75 + LogUtils::error("BsideGlobalException", [], $exceptionMessage);
  76 + }
  77 + //验证错误(非手动抛出)
  78 + elseif ($exception instanceof ValidationException) {
  79 + LogUtils::error("参数验证失败", [], $exceptionMessage);
  80 + }
  81 + //Redis错误(非手动抛出)
  82 + elseif ($exception instanceof \RedisException) {
  83 + LogUtils::error("Redis服务异常", [], $exceptionMessage);
  84 + }
  85 + //Mysql错误(非手动抛出)
  86 + elseif ($exception instanceof \PDOException) {
  87 + LogUtils::error("数据库服务异常", [], $exceptionMessage);
  88 + }
  89 + //模型未找到错误(非手动抛出)
  90 + elseif ($exception instanceof ModelNotFoundException) {
  91 + LogUtils::error("模型资源未找到", [], $exceptionMessage);
  92 + }
  93 + //其他
  94 + else {
  95 + LogUtils::error("全局系统异常", [], $exceptionMessage);
  96 + }
  97 + }
45 /** 98 /**
46 * Render an exception into an HTTP response. 99 * Render an exception into an HTTP response.
47 * 100 *
@@ -53,6 +106,53 @@ class Handler extends ExceptionHandler @@ -53,6 +106,53 @@ class Handler extends ExceptionHandler
53 */ 106 */
54 public function render($request, Throwable $exception) 107 public function render($request, Throwable $exception)
55 { 108 {
56 - return parent::render($request, $exception); 109 + $message = $exception->getMessage();
  110 +
  111 + if ($exception instanceof AsideGlobalException) {
  112 + $code = $exception->getCode();
  113 + }elseif ($exception instanceof BsideGlobalException) {
  114 + $code = $exception->getCode();
  115 + } elseif ($exception instanceof ValidationException) {
  116 + $code = Code::USER_PARAMS_ERROE();
  117 + } elseif ($exception instanceof \RedisException) {
  118 + $code = Code::SERVER_REDIS_ERROR();
  119 + } elseif ($exception instanceof \PDOException) {
  120 + $code = Code::SERVER_MYSQL_ERROR();
  121 + } elseif ($exception instanceof ModelNotFoundException) {
  122 + $code = Code::USER_MODEL_NOTFOUND_ERROE();
  123 + } elseif ($exception instanceof NotFoundHttpException || $exception instanceof MethodNotAllowedHttpException) {
  124 + return response('404 Not Found', 404);
  125 + } else {
  126 + $code = Code::SYSTEM_ERROR();
  127 + }
  128 + //钉钉通知错误信息
  129 + if (in_array(config('app.env'), ['test', 'production']) && (in_array(substr($code, 0, 1), ['B', 'C']))) {
  130 + $exceptionMessage = "路由:" . Route::current()->uri .
  131 + "-----错误CODE:" . $exception->getCode() .
  132 + "-----错误message:" . $exception->getMessage() .
  133 + '------错误文件:' . $exception->getFile() . '-------错误行数:' .
  134 + $exception->getLine();
  135 + (new DingService())->handle(['keyword' => "ERROR", 'msg' => config('app.env') . '环境报错:' . $exceptionMessage, 'isAtAll' => false]);
  136 + }
  137 + //开启debug 错误原样输出
  138 + $debub = config('app.debug');
  139 + $message = $debub ? $message : ($code->description ?? $message);
  140 +
  141 + //请求参数错误 输出具体错误信息
  142 + if($code == Code::USER_PARAMS_ERROE()){
  143 + $message = Arr::first(Arr::first($exception->errors()));
  144 + }
  145 + $response = [
  146 + 'code' => $code,
  147 + 'message' => $message
  148 + ];
  149 + //加密返回
  150 + if (config('app.params_encrypt')) {
  151 + $k = config('app.params_encrypt_key');
  152 + $i = config('app.params_encrypt_iv');
  153 + $response = [
  154 + 'p' => (new EncryptUtils())->openssl_en($response, $k, $i)];
  155 + }
  156 + return response($response);
57 } 157 }
58 } 158 }
1 -<?php  
2 -  
3 -namespace App\Http\Controllers\Aside;  
4 -  
5 -use App\Enums\Common\Code;  
6 -use App\Models\ProjectMenu as ProjectMenuModel;  
7 -use Illuminate\Support\Facades\Validator;  
8 -  
9 -class ProjectMenuController extends BaseController  
10 -{  
11 - /**  
12 - * @name :用户组菜单列表(带分页)  
13 - * @return void  
14 - * @author :liyuhang  
15 - * @method  
16 - */  
17 - public function lists(){  
18 - //根据角色获取菜单列表  
19 - $projectMenuModel = new ProjectMenuModel();  
20 - $lists = $projectMenuModel->lists($this->map,$this->p,$this->row,$this->order);  
21 - $this->allCount = $projectMenuModel->allCount;  
22 - $this->result($lists);  
23 - }  
24 -  
25 - /**  
26 - * @name :添加用户组菜单  
27 - * @return void  
28 - * @author :liyuhang  
29 - * @method  
30 - */  
31 - public function add(){  
32 - //参数验证  
33 - $rules = [  
34 - 'name'=>'required|max:11',  
35 - 'rules'=>'required',  
36 - ];  
37 - //验证的提示信息  
38 - $message = [  
39 - 'name.required'=>'名称必须填写',  
40 - 'name.max' => '名称不大于16字符.',  
41 - 'rules.required'=>'路由必须填写',  
42 - ];  
43 - $validate = Validator::make($this->param, $rules, $message);  
44 - if($validate->fails()){  
45 - return $this->response($validate->errors()->first(),Code::USER_PARAMS_ERROE,$this->param);  
46 - }  
47 - $projectMenuModel = new ProjectMenuModel();  
48 - $rs = $projectMenuModel->add($this->param);  
49 - if($rs === false){  
50 - $this->response('请求失败',Code::USER_ERROR,[]);  
51 - }  
52 - $this->response('success',Code::SUCCESS);  
53 - }  
54 -  
55 - /**  
56 - * @name :编辑用户组菜单  
57 - * @return void  
58 - * @author :liyuhang  
59 - * @method  
60 - */  
61 - public function edit(){  
62 - //参数验证  
63 - $rules = [  
64 - 'id'=>'required',  
65 - 'name'=>'required|max:11',  
66 - 'rules'=>'required',  
67 - ];  
68 - //验证的提示信息  
69 - $message = [  
70 - 'id.required'=>'服务器id错误',  
71 - 'name.required'=>'名称必须填写',  
72 - 'name.max' => '名称不大于16字符.',  
73 - 'rules.required'=>'路由必须填写',  
74 - ];  
75 - $validate = Validator::make($this->param, $rules, $message);  
76 - if($validate->fails()){  
77 - return $this->response($validate->errors()->first(),Code::USER_PARAMS_ERROE,$this->param);  
78 - }  
79 - $projectMenuModel = new ProjectMenuModel();  
80 - $rs = $projectMenuModel->edit($this->param,['id'=>$this->param['id']]);  
81 - if($rs === false){  
82 - $this->response('请求失败',Code::USER_ERROR);  
83 - }  
84 - $this->response('success',Code::SUCCESS);  
85 - }  
86 -  
87 - /**  
88 - * @name :编辑状态  
89 - * @return void  
90 - * @author :liyuhang  
91 - * @method  
92 - */  
93 - public function status(){  
94 - //参数验证  
95 - $rules = [  
96 - 'id'=>'required',  
97 - 'status'=>'required',  
98 - ];  
99 - //验证的提示信息  
100 - $message = [  
101 - 'id.required'=>'主键必须填写',  
102 - 'status.required'=>'状态必须填写',  
103 - ];  
104 - $validate = Validator::make($this->param, $rules, $message);  
105 - if($validate->fails()){  
106 - return $this->response($validate->errors()->first(),Code::USER_PARAMS_ERROE,$this->param);  
107 - }  
108 - $projectMenuModel = new ProjectMenuModel();  
109 - $rs = $projectMenuModel->edit(['status'=>$this->param['status']],['id'=>$this->param['id']]);  
110 - if($rs === false){  
111 - $this->response('编辑失败',Code::USER_PARAMS_ERROE);  
112 - }  
113 - $this->response($this->param['status'] == 0 ? '启用成功' : '禁用成功',Code::SUCCESS);  
114 - }  
115 -  
116 -}  
@@ -22,9 +22,8 @@ class CategoryController extends BaseController @@ -22,9 +22,8 @@ class CategoryController extends BaseController
22 if(!empty($this->param['title'])){ 22 if(!empty($this->param['title'])){
23 $map[] = ['title', 'like', "%{$this->param['title']}%"]; 23 $map[] = ['title', 'like', "%{$this->param['title']}%"];
24 } 24 }
25 - $sort = ['id' => 'desc', 'pid' => "asc"];  
26 25
27 - $data = $logic->getList($map, $sort); 26 + $data = $logic->getList($map);
28 return $this->success($data); 27 return $this->success($data);
29 } 28 }
30 29