作者 Your Name

gx

... ... @@ -13,12 +13,12 @@ use Throwable;
*/
class AsideGlobalException extends Exception
{
public function __construct($code = 0, $message = "", Throwable $previous = null)
{
$this->code = $code;
$this->message = $message;
if (empty($this->message)) {
$this->message = Code::fromValue($code)->description;
}
}
// public function __construct($code = 0, $message = "", Throwable $previous = null)
// {
// $this->code = $code;
// $this->message = $message;
// if (empty($this->message)) {
// $this->message = Code::fromValue($code)->description;
// }
// }
}
... ...
... ... @@ -13,12 +13,12 @@ use Throwable;
*/
class BsideGlobalException extends Exception
{
public function __construct($code = 0, $message = "", Throwable $previous = null)
{
$this->code = $code;
$this->message = $message;
if (empty($this->message)) {
$this->message = Code::fromValue($code)->description;
}
}
// public function __construct($code = 0, $message = "", Throwable $previous = null)
// {
// $this->code = $code;
// $this->message = $message;
// if (empty($this->message)) {
// $this->message = Code::fromValue($code)->description;
// }
// }
}
... ...
... ... @@ -2,17 +2,7 @@
namespace App\Exceptions;
use App\Enums\Common\Code;
use App\Enums\Common\Common;
use App\Services\DingService;
use App\Utils\EncryptUtils;
use App\Utils\LogUtils;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
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
... ... @@ -20,7 +10,7 @@ class Handler extends ExceptionHandler
/**
* A list of the exception types that are not reported.
*
* @var array<int, class-string<Throwable>>
* @var array
*/
protected $dontReport = [
//
... ... @@ -29,26 +19,14 @@ class Handler extends ExceptionHandler
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array<int, string>
* @var array
*/
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
... ... @@ -58,41 +36,9 @@ class Handler extends ExceptionHandler
*/
public function report(Throwable $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);
}
parent::report($exception);
}
/**
* Render an exception into an HTTP response.
*
... ... @@ -104,48 +50,6 @@ class Handler extends ExceptionHandler
*/
public function render($request, Throwable $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);
$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);
return parent::render($request, $exception);
}
}
... ...
... ... @@ -134,7 +134,7 @@ class BaseController extends Controller
];
$this->header['Content-Type'] = $type;
$this->header['token'] = $this->token;
$response = Response::create(json_encode($result),$code,$this->header);
$response = response($result,$result_code,$this->header);;
throw new HttpResponseException($response);
}
/**
... ...
... ... @@ -4,8 +4,6 @@ namespace App\Http\Controllers\Bside;
use App\Enums\Common\Code;
use App\Http\Controllers\Controller;
use App\Models\ProjectMenu;
use App\Models\ProjectRole as ProjectRoleModel;
use \Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Http\Exceptions\HttpResponseException;
... ... @@ -34,7 +32,6 @@ class BaseController extends Controller
$this->token = $this->request->header('token');
$this->get_param();
$this->auth_token();
$this->auth_role();
}
/**
... ... @@ -47,17 +44,6 @@ class BaseController extends Controller
$info = Cache::get($this->token);
$this->user = $info;
$this->uid = $info['id'];
//操作权限设置
$projectRoleModel = new ProjectRoleModel();
$role_info = $projectRoleModel->read(['id'=>$this->user['role_id']]);
//获取当前操作的控制器与方法
$action = $this->request->route()->getAction();
//查询当前用户是否拥有权限操作
$projectMenuModel = new ProjectMenu();
$menu_id = $projectMenuModel->read(['action'=>$action['as']],['id']);
if($menu_id !== false && strpos($role_info['role_menu'], $menu_id['id']) === false){
$this->response('拦截',Code::USER_PERMISSION_ERROE);
}
}
/**
... ... @@ -106,16 +92,16 @@ class BaseController extends Controller
* @author :liyuhang
* @method
*/
public function response($msg,$code = 200,$data = [],$result_code = null,$type = 'application/json'){
$result_code === null && $result_code = $code;
public function response($msg,$code = null,$data = [],$result_code = 200,$type = 'application/json'){
$code === null && $code = $result_code;
$result = [
'msg' =>$msg,
'code'=>$result_code,
'code'=>$code,
'data'=>$data
];
$this->header['Content-Type'] = $type;
$this->header['token'] = $this->token;
$response = Response::create(json_encode($result),$code,$this->header);
$response = response($result,$result_code,$this->header);;
throw new HttpResponseException($response);
}
/**
... ...
... ... @@ -2,9 +2,14 @@
namespace App\Http\Middleware\Bside;
use App\Enums\Common\Code;
use App\Models\ProjectMenu;
use App\Models\ProjectRole as ProjectRoleModel;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;
use Illuminate\Http\Exceptions\HttpResponseException;
class LoginAuthMiddleware
{
/**
... ... @@ -16,6 +21,44 @@ class LoginAuthMiddleware
*/
public function handle(Request $request, Closure $next)
{
$token = $request->header('token');
if(!isset($token) || empty($token)){
$this->response('当前用户未登录',Code::USER_ERROR);
}
$info = Cache::get($request->header('token'));
if(empty($info)){
$this->response('当前用户未登录',Code::USER_ERROR);
}
//操作权限设置
$projectRoleModel = new ProjectRoleModel();
$role_info = $projectRoleModel->read(['id'=>$info['role_id']]);
//获取当前操作的控制器与方法
$action = $request->route()->getAction();
//查询当前用户是否拥有权限操作
$projectMenuModel = new ProjectMenu();
$menu_id = $projectMenuModel->read(['action'=>$action['as']],['id']);
if(strpos($role_info['role_menu'], $menu_id['id']) < 0){
$this->response('当前用户没有权限',Code::USER_ERROR);
}
return $next($request);
}
/**
* @name 统一返回参数
* @return void
* @author :liyuhang
* @method
*/
public function response($msg,$code,$data = [],$result_code = 200,$type = 'application/json'){
$code === null && $code = $result_code;
$result = [
'msg' =>$msg,
'code'=>$code,
'data'=>$data
];
$header['token'] = $type;
$response = response($result,$result_code,$header);
throw new HttpResponseException($response);
}
}
... ...
... ... @@ -8,7 +8,6 @@ namespace App\Services;
use App\Enums\Common\Code;
use App\Exceptions\BsideGlobalException;
use App\Traits\RedisTrait;
class BaseService
{
... ...
... ... @@ -7,6 +7,7 @@ use \Illuminate\Support\Facades\Route;
//必须登录验证的路由组
Route::middleware(['bloginauth'])->group(function () {
Route::any('/user/status', [\App\Http\Controllers\Bside\UserController::class, 'status'])->name('user_status');
});
//无需登录验证的路由组
... ... @@ -17,7 +18,7 @@ Route::group([], function () {
//用户相关路由
Route::any('/user/add', [\App\Http\Controllers\Bside\UserController::class, 'add'])->name('user_add');
Route::any('/user/edit', [\App\Http\Controllers\Bside\UserController::class, 'edit'])->name('user_edit');
Route::any('/user/status', [\App\Http\Controllers\Bside\UserController::class, 'status'])->name('user_status');
Route::any('/user/lists', [\App\Http\Controllers\Bside\UserController::class, 'lists'])->name('user_lists');
Route::any('/user/del', [\App\Http\Controllers\Bside\UserController::class, 'del'])->name('user_del');
//用户角色相关路由
... ...