作者 liyuhang

gx

... ... @@ -2,10 +2,19 @@
namespace App\Exceptions;
use App\Enums\Common\Code;
use App\Enums\Common\Common;
use App\Services\DingService;
use App\Traits\RedisTrait;
use App\Utils\EncryptUtils;
use App\Utils\LogUtils;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Route;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;
class Handler extends ExceptionHandler
... ... @@ -13,7 +22,7 @@ class Handler extends ExceptionHandler
/**
* A list of the exception types that are not reported.
*
* @var array
* @var array<int, class-string<Throwable>>
*/
protected $dontReport = [
//
... ... @@ -22,14 +31,26 @@ class Handler extends ExceptionHandler
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
* @var array<int, string>
*/
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->reportable(function (Throwable $e) {
//
});
}
/**
* Report or log an exception.
*
* @param \Throwable $exception
... ... @@ -39,9 +60,41 @@ class Handler extends ExceptionHandler
*/
public function report(Throwable $exception)
{
parent::report($exception);
}
//日志记录
$exceptionMessage = "错误CODE:" . $exception->getCode() .
"-----错误message:" . $exception->getMessage() .
'------错误文件:' . $exception->getFile() .
'-------错误行数:' . $exception->getLine();
//A端错误
if ($exception instanceof AsideGlobalException) {
LogUtils::error("AsideGlobalException", [], $exceptionMessage);
}
//B端错误
elseif($exception instanceof BsideGlobalException) {
LogUtils::error("BsideGlobalException", [], $exceptionMessage);
}
//验证错误(非手动抛出)
elseif ($exception instanceof ValidationException) {
LogUtils::error("参数验证失败", [], $exceptionMessage);
}
//Redis错误(非手动抛出)
elseif ($exception instanceof \RedisException) {
LogUtils::error("Redis服务异常", [], $exceptionMessage);
}
//Mysql错误(非手动抛出)
elseif ($exception instanceof \PDOException) {
LogUtils::error("数据库服务异常", [], $exceptionMessage);
}
//模型未找到错误(非手动抛出)
elseif ($exception instanceof ModelNotFoundException) {
LogUtils::error("模型资源未找到", [], $exceptionMessage);
}
//其他
else {
LogUtils::error("全局系统异常", [], $exceptionMessage);
}
}
/**
* Render an exception into an HTTP response.
*
... ... @@ -53,6 +106,53 @@ class Handler extends ExceptionHandler
*/
public function render($request, Throwable $exception)
{
return parent::render($request, $exception);
$message = $exception->getMessage();
if ($exception instanceof AsideGlobalException) {
$code = $exception->getCode();
}elseif ($exception instanceof BsideGlobalException) {
$code = $exception->getCode();
} elseif ($exception instanceof ValidationException) {
$code = Code::USER_PARAMS_ERROE();
} elseif ($exception instanceof \RedisException) {
$code = Code::SERVER_REDIS_ERROR();
} elseif ($exception instanceof \PDOException) {
$code = Code::SERVER_MYSQL_ERROR();
} elseif ($exception instanceof ModelNotFoundException) {
$code = Code::USER_MODEL_NOTFOUND_ERROE();
} elseif ($exception instanceof NotFoundHttpException || $exception instanceof MethodNotAllowedHttpException) {
return response('404 Not Found', 404);
} else {
$code = Code::SYSTEM_ERROR();
}
//钉钉通知错误信息
if (in_array(config('app.env'), ['test', 'production']) && (in_array(substr($code, 0, 1), ['B', 'C']))) {
$exceptionMessage = "路由:" . Route::current()->uri .
"-----错误CODE:" . $exception->getCode() .
"-----错误message:" . $exception->getMessage() .
'------错误文件:' . $exception->getFile() . '-------错误行数:' .
$exception->getLine();
(new DingService())->handle(['keyword' => "ERROR", 'msg' => config('app.env') . '环境报错:' . $exceptionMessage, 'isAtAll' => false]);
}
//开启debug 错误原样输出
$debub = config('app.debug');
$message = $debub ? $message : ($code->description ?? $message);
//请求参数错误 输出具体错误信息
if($code == Code::USER_PARAMS_ERROE()){
$message = Arr::first(Arr::first($exception->errors()));
}
$response = [
'code' => $code,
'message' => $message
];
//加密返回
if (config('app.params_encrypt')) {
$k = config('app.params_encrypt_key');
$i = config('app.params_encrypt_iv');
$response = [
'p' => (new EncryptUtils())->openssl_en($response, $k, $i)];
}
return response($response);
}
}
... ...
<?php
namespace App\Http\Controllers\Aside;
use App\Enums\Common\Code;
use App\Models\ProjectMenu as ProjectMenuModel;
use Illuminate\Support\Facades\Validator;
class ProjectMenuController extends BaseController
{
/**
* @name :用户组菜单列表(带分页)
* @return void
* @author :liyuhang
* @method
*/
public function lists(){
//根据角色获取菜单列表
$projectMenuModel = new ProjectMenuModel();
$lists = $projectMenuModel->lists($this->map,$this->p,$this->row,$this->order);
$this->allCount = $projectMenuModel->allCount;
$this->result($lists);
}
/**
* @name :添加用户组菜单
* @return void
* @author :liyuhang
* @method
*/
public function add(){
//参数验证
$rules = [
'name'=>'required|max:11',
'rules'=>'required',
];
//验证的提示信息
$message = [
'name.required'=>'名称必须填写',
'name.max' => '名称不大于16字符.',
'rules.required'=>'路由必须填写',
];
$validate = Validator::make($this->param, $rules, $message);
if($validate->fails()){
return $this->response($validate->errors()->first(),Code::USER_PARAMS_ERROE,$this->param);
}
$projectMenuModel = new ProjectMenuModel();
$rs = $projectMenuModel->add($this->param);
if($rs === false){
$this->response('请求失败',Code::USER_ERROR,[]);
}
$this->response('success',Code::SUCCESS);
}
/**
* @name :编辑用户组菜单
* @return void
* @author :liyuhang
* @method
*/
public function edit(){
//参数验证
$rules = [
'id'=>'required',
'name'=>'required|max:11',
'rules'=>'required',
];
//验证的提示信息
$message = [
'id.required'=>'服务器id错误',
'name.required'=>'名称必须填写',
'name.max' => '名称不大于16字符.',
'rules.required'=>'路由必须填写',
];
$validate = Validator::make($this->param, $rules, $message);
if($validate->fails()){
return $this->response($validate->errors()->first(),Code::USER_PARAMS_ERROE,$this->param);
}
$projectMenuModel = new ProjectMenuModel();
$rs = $projectMenuModel->edit($this->param,['id'=>$this->param['id']]);
if($rs === false){
$this->response('请求失败',Code::USER_ERROR);
}
$this->response('success',Code::SUCCESS);
}
/**
* @name :编辑状态
* @return void
* @author :liyuhang
* @method
*/
public function status(){
//参数验证
$rules = [
'id'=>'required',
'status'=>'required',
];
//验证的提示信息
$message = [
'id.required'=>'主键必须填写',
'status.required'=>'状态必须填写',
];
$validate = Validator::make($this->param, $rules, $message);
if($validate->fails()){
return $this->response($validate->errors()->first(),Code::USER_PARAMS_ERROE,$this->param);
}
$projectMenuModel = new ProjectMenuModel();
$rs = $projectMenuModel->edit(['status'=>$this->param['status']],['id'=>$this->param['id']]);
if($rs === false){
$this->response('编辑失败',Code::USER_PARAMS_ERROE);
}
$this->response($this->param['status'] == 0 ? '启用成功' : '禁用成功',Code::SUCCESS);
}
}
... ... @@ -22,9 +22,8 @@ class CategoryController extends BaseController
if(!empty($this->param['title'])){
$map[] = ['title', 'like', "%{$this->param['title']}%"];
}
$sort = ['id' => 'desc', 'pid' => "asc"];
$data = $logic->getList($map, $sort);
$data = $logic->getList($map);
return $this->success($data);
}
... ...