正在显示
8 个修改的文件
包含
72 行增加
和
139 行删除
| @@ -13,12 +13,12 @@ use Throwable; | @@ -13,12 +13,12 @@ use Throwable; | ||
| 13 | */ | 13 | */ |
| 14 | class AsideGlobalException extends Exception | 14 | class AsideGlobalException extends Exception |
| 15 | { | 15 | { |
| 16 | - public function __construct($code = 0, $message = "", Throwable $previous = null) | ||
| 17 | - { | ||
| 18 | - $this->code = $code; | ||
| 19 | - $this->message = $message; | ||
| 20 | - if (empty($this->message)) { | ||
| 21 | - $this->message = Code::fromValue($code)->description; | ||
| 22 | - } | ||
| 23 | - } | 16 | +// public function __construct($code = 0, $message = "", Throwable $previous = null) |
| 17 | +// { | ||
| 18 | +// $this->code = $code; | ||
| 19 | +// $this->message = $message; | ||
| 20 | +// if (empty($this->message)) { | ||
| 21 | +// $this->message = Code::fromValue($code)->description; | ||
| 22 | +// } | ||
| 23 | +// } | ||
| 24 | } | 24 | } |
| @@ -13,12 +13,12 @@ use Throwable; | @@ -13,12 +13,12 @@ use Throwable; | ||
| 13 | */ | 13 | */ |
| 14 | class BsideGlobalException extends Exception | 14 | class BsideGlobalException extends Exception |
| 15 | { | 15 | { |
| 16 | - public function __construct($code = 0, $message = "", Throwable $previous = null) | ||
| 17 | - { | ||
| 18 | - $this->code = $code; | ||
| 19 | - $this->message = $message; | ||
| 20 | - if (empty($this->message)) { | ||
| 21 | - $this->message = Code::fromValue($code)->description; | ||
| 22 | - } | ||
| 23 | - } | 16 | +// public function __construct($code = 0, $message = "", Throwable $previous = null) |
| 17 | +// { | ||
| 18 | +// $this->code = $code; | ||
| 19 | +// $this->message = $message; | ||
| 20 | +// if (empty($this->message)) { | ||
| 21 | +// $this->message = Code::fromValue($code)->description; | ||
| 22 | +// } | ||
| 23 | +// } | ||
| 24 | } | 24 | } |
| @@ -2,17 +2,7 @@ | @@ -2,17 +2,7 @@ | ||
| 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\Utils\EncryptUtils; | ||
| 9 | -use App\Utils\LogUtils; | ||
| 10 | -use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
| 11 | use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; | 5 | use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
| 12 | -use Illuminate\Support\Facades\Route; | ||
| 13 | -use Illuminate\Validation\ValidationException; | ||
| 14 | -use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; | ||
| 15 | -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
| 16 | use Throwable; | 6 | use Throwable; |
| 17 | 7 | ||
| 18 | class Handler extends ExceptionHandler | 8 | class Handler extends ExceptionHandler |
| @@ -20,7 +10,7 @@ class Handler extends ExceptionHandler | @@ -20,7 +10,7 @@ class Handler extends ExceptionHandler | ||
| 20 | /** | 10 | /** |
| 21 | * A list of the exception types that are not reported. | 11 | * A list of the exception types that are not reported. |
| 22 | * | 12 | * |
| 23 | - * @var array<int, class-string<Throwable>> | 13 | + * @var array |
| 24 | */ | 14 | */ |
| 25 | protected $dontReport = [ | 15 | protected $dontReport = [ |
| 26 | // | 16 | // |
| @@ -29,26 +19,14 @@ class Handler extends ExceptionHandler | @@ -29,26 +19,14 @@ class Handler extends ExceptionHandler | ||
| 29 | /** | 19 | /** |
| 30 | * A list of the inputs that are never flashed for validation exceptions. | 20 | * A list of the inputs that are never flashed for validation exceptions. |
| 31 | * | 21 | * |
| 32 | - * @var array<int, string> | 22 | + * @var array |
| 33 | */ | 23 | */ |
| 34 | protected $dontFlash = [ | 24 | protected $dontFlash = [ |
| 35 | - 'current_password', | ||
| 36 | 'password', | 25 | 'password', |
| 37 | 'password_confirmation', | 26 | 'password_confirmation', |
| 38 | ]; | 27 | ]; |
| 39 | 28 | ||
| 40 | /** | 29 | /** |
| 41 | - * Register the exception handling callbacks for the application. | ||
| 42 | - * | ||
| 43 | - * @return void | ||
| 44 | - */ | ||
| 45 | - public function register() | ||
| 46 | - { | ||
| 47 | - $this->reportable(function (Throwable $e) { | ||
| 48 | - // | ||
| 49 | - }); | ||
| 50 | - } | ||
| 51 | - /** | ||
| 52 | * Report or log an exception. | 30 | * Report or log an exception. |
| 53 | * | 31 | * |
| 54 | * @param \Throwable $exception | 32 | * @param \Throwable $exception |
| @@ -58,41 +36,9 @@ class Handler extends ExceptionHandler | @@ -58,41 +36,9 @@ class Handler extends ExceptionHandler | ||
| 58 | */ | 36 | */ |
| 59 | public function report(Throwable $exception) | 37 | public function report(Throwable $exception) |
| 60 | { | 38 | { |
| 61 | - | ||
| 62 | - //日志记录 | ||
| 63 | - $exceptionMessage = "错误CODE:" . $exception->getCode() . | ||
| 64 | - "-----错误message:" . $exception->getMessage() . | ||
| 65 | - '------错误文件:' . $exception->getFile() . | ||
| 66 | - '-------错误行数:' . $exception->getLine(); | ||
| 67 | - //A端错误 | ||
| 68 | - if ($exception instanceof AsideGlobalException) { | ||
| 69 | - LogUtils::error("AsideGlobalException", [], $exceptionMessage); | ||
| 70 | - } | ||
| 71 | - //B端错误 | ||
| 72 | - elseif($exception instanceof BsideGlobalException) { | ||
| 73 | - LogUtils::error("BsideGlobalException", [], $exceptionMessage); | ||
| 74 | - } | ||
| 75 | - //验证错误(非手动抛出) | ||
| 76 | - elseif ($exception instanceof ValidationException) { | ||
| 77 | - LogUtils::error("参数验证失败", [], $exceptionMessage); | ||
| 78 | - } | ||
| 79 | - //Redis错误(非手动抛出) | ||
| 80 | - elseif ($exception instanceof \RedisException) { | ||
| 81 | - LogUtils::error("Redis服务异常", [], $exceptionMessage); | ||
| 82 | - } | ||
| 83 | - //Mysql错误(非手动抛出) | ||
| 84 | - elseif ($exception instanceof \PDOException) { | ||
| 85 | - LogUtils::error("数据库服务异常", [], $exceptionMessage); | ||
| 86 | - } | ||
| 87 | - //模型未找到错误(非手动抛出) | ||
| 88 | - elseif ($exception instanceof ModelNotFoundException) { | ||
| 89 | - LogUtils::error("模型资源未找到", [], $exceptionMessage); | ||
| 90 | - } | ||
| 91 | - //其他 | ||
| 92 | - else { | ||
| 93 | - LogUtils::error("全局系统异常", [], $exceptionMessage); | ||
| 94 | - } | 39 | + parent::report($exception); |
| 95 | } | 40 | } |
| 41 | + | ||
| 96 | /** | 42 | /** |
| 97 | * Render an exception into an HTTP response. | 43 | * Render an exception into an HTTP response. |
| 98 | * | 44 | * |
| @@ -104,48 +50,6 @@ class Handler extends ExceptionHandler | @@ -104,48 +50,6 @@ class Handler extends ExceptionHandler | ||
| 104 | */ | 50 | */ |
| 105 | public function render($request, Throwable $exception) | 51 | public function render($request, Throwable $exception) |
| 106 | { | 52 | { |
| 107 | - $message = $exception->getMessage(); | ||
| 108 | - | ||
| 109 | - if ($exception instanceof AsideGlobalException) { | ||
| 110 | - $code = $exception->getCode(); | ||
| 111 | - }elseif ($exception instanceof BsideGlobalException) { | ||
| 112 | - $code = $exception->getCode(); | ||
| 113 | - } elseif ($exception instanceof ValidationException) { | ||
| 114 | - $code = Code::USER_PARAMS_ERROE(); | ||
| 115 | - } elseif ($exception instanceof \RedisException) { | ||
| 116 | - $code = Code::SERVER_REDIS_ERROR(); | ||
| 117 | - } elseif ($exception instanceof \PDOException) { | ||
| 118 | - $code = Code::SERVER_MYSQL_ERROR(); | ||
| 119 | - } elseif ($exception instanceof ModelNotFoundException) { | ||
| 120 | - $code = Code::USER_MODEL_NOTFOUND_ERROE(); | ||
| 121 | - } elseif ($exception instanceof NotFoundHttpException || $exception instanceof MethodNotAllowedHttpException) { | ||
| 122 | - return response('404 Not Found', 404); | ||
| 123 | - } else { | ||
| 124 | - $code = Code::SYSTEM_ERROR(); | ||
| 125 | - } | ||
| 126 | - //钉钉通知错误信息 | ||
| 127 | - if (in_array(config('app.env'), ['test', 'production']) && (in_array(substr($code, 0, 1), ['B', 'C']))) { | ||
| 128 | - $exceptionMessage = "路由:" . Route::current()->uri . | ||
| 129 | - "-----错误CODE:" . $exception->getCode() . | ||
| 130 | - "-----错误message:" . $exception->getMessage() . | ||
| 131 | - '------错误文件:' . $exception->getFile() . '-------错误行数:' . | ||
| 132 | - $exception->getLine(); | ||
| 133 | - (new DingService())->handle(['keyword' => "ERROR", 'msg' => config('app.env') . '环境报错:' . $exceptionMessage, 'isAtAll' => false]); | ||
| 134 | - } | ||
| 135 | - //开启debug 错误原样输出 | ||
| 136 | - $debub = config('app.debug'); | ||
| 137 | - $message = $debub ? $message : ($code->description ?? $message); | ||
| 138 | - $response = [ | ||
| 139 | - 'code' => $code, | ||
| 140 | - 'message' => $message | ||
| 141 | - ]; | ||
| 142 | - //加密返回 | ||
| 143 | - if (config('app.params_encrypt')) { | ||
| 144 | - $k = config('app.params_encrypt_key'); | ||
| 145 | - $i = config('app.params_encrypt_iv'); | ||
| 146 | - $response = [ | ||
| 147 | - 'p' => (new EncryptUtils())->openssl_en($response, $k, $i)]; | ||
| 148 | - } | ||
| 149 | - return response($response); | 53 | + return parent::render($request, $exception); |
| 150 | } | 54 | } |
| 151 | } | 55 | } |
| @@ -134,7 +134,7 @@ class BaseController extends Controller | @@ -134,7 +134,7 @@ class BaseController extends Controller | ||
| 134 | ]; | 134 | ]; |
| 135 | $this->header['Content-Type'] = $type; | 135 | $this->header['Content-Type'] = $type; |
| 136 | $this->header['token'] = $this->token; | 136 | $this->header['token'] = $this->token; |
| 137 | - $response = Response::create(json_encode($result),$code,$this->header); | 137 | + $response = response($result,$result_code,$this->header);; |
| 138 | throw new HttpResponseException($response); | 138 | throw new HttpResponseException($response); |
| 139 | } | 139 | } |
| 140 | /** | 140 | /** |
| @@ -4,8 +4,6 @@ namespace App\Http\Controllers\Bside; | @@ -4,8 +4,6 @@ namespace App\Http\Controllers\Bside; | ||
| 4 | 4 | ||
| 5 | use App\Enums\Common\Code; | 5 | use App\Enums\Common\Code; |
| 6 | use App\Http\Controllers\Controller; | 6 | use App\Http\Controllers\Controller; |
| 7 | -use App\Models\ProjectMenu; | ||
| 8 | -use App\Models\ProjectRole as ProjectRoleModel; | ||
| 9 | use \Illuminate\Http\Request; | 7 | use \Illuminate\Http\Request; |
| 10 | use Illuminate\Http\Response; | 8 | use Illuminate\Http\Response; |
| 11 | use Illuminate\Http\Exceptions\HttpResponseException; | 9 | use Illuminate\Http\Exceptions\HttpResponseException; |
| @@ -34,7 +32,6 @@ class BaseController extends Controller | @@ -34,7 +32,6 @@ class BaseController extends Controller | ||
| 34 | $this->token = $this->request->header('token'); | 32 | $this->token = $this->request->header('token'); |
| 35 | $this->get_param(); | 33 | $this->get_param(); |
| 36 | $this->auth_token(); | 34 | $this->auth_token(); |
| 37 | - $this->auth_role(); | ||
| 38 | } | 35 | } |
| 39 | 36 | ||
| 40 | /** | 37 | /** |
| @@ -47,17 +44,6 @@ class BaseController extends Controller | @@ -47,17 +44,6 @@ class BaseController extends Controller | ||
| 47 | $info = Cache::get($this->token); | 44 | $info = Cache::get($this->token); |
| 48 | $this->user = $info; | 45 | $this->user = $info; |
| 49 | $this->uid = $info['id']; | 46 | $this->uid = $info['id']; |
| 50 | - //操作权限设置 | ||
| 51 | - $projectRoleModel = new ProjectRoleModel(); | ||
| 52 | - $role_info = $projectRoleModel->read(['id'=>$this->user['role_id']]); | ||
| 53 | - //获取当前操作的控制器与方法 | ||
| 54 | - $action = $this->request->route()->getAction(); | ||
| 55 | - //查询当前用户是否拥有权限操作 | ||
| 56 | - $projectMenuModel = new ProjectMenu(); | ||
| 57 | - $menu_id = $projectMenuModel->read(['action'=>$action['as']],['id']); | ||
| 58 | - if($menu_id !== false && strpos($role_info['role_menu'], $menu_id['id']) === false){ | ||
| 59 | - $this->response('拦截',Code::USER_PERMISSION_ERROE); | ||
| 60 | - } | ||
| 61 | } | 47 | } |
| 62 | 48 | ||
| 63 | /** | 49 | /** |
| @@ -106,16 +92,16 @@ class BaseController extends Controller | @@ -106,16 +92,16 @@ class BaseController extends Controller | ||
| 106 | * @author :liyuhang | 92 | * @author :liyuhang |
| 107 | * @method | 93 | * @method |
| 108 | */ | 94 | */ |
| 109 | - public function response($msg,$code = 200,$data = [],$result_code = null,$type = 'application/json'){ | ||
| 110 | - $result_code === null && $result_code = $code; | 95 | + public function response($msg,$code = null,$data = [],$result_code = 200,$type = 'application/json'){ |
| 96 | + $code === null && $code = $result_code; | ||
| 111 | $result = [ | 97 | $result = [ |
| 112 | 'msg' =>$msg, | 98 | 'msg' =>$msg, |
| 113 | - 'code'=>$result_code, | 99 | + 'code'=>$code, |
| 114 | 'data'=>$data | 100 | 'data'=>$data |
| 115 | ]; | 101 | ]; |
| 116 | $this->header['Content-Type'] = $type; | 102 | $this->header['Content-Type'] = $type; |
| 117 | $this->header['token'] = $this->token; | 103 | $this->header['token'] = $this->token; |
| 118 | - $response = Response::create(json_encode($result),$code,$this->header); | 104 | + $response = response($result,$result_code,$this->header);; |
| 119 | throw new HttpResponseException($response); | 105 | throw new HttpResponseException($response); |
| 120 | } | 106 | } |
| 121 | /** | 107 | /** |
| @@ -2,9 +2,14 @@ | @@ -2,9 +2,14 @@ | ||
| 2 | 2 | ||
| 3 | namespace App\Http\Middleware\Bside; | 3 | namespace App\Http\Middleware\Bside; |
| 4 | 4 | ||
| 5 | +use App\Enums\Common\Code; | ||
| 6 | +use App\Models\ProjectMenu; | ||
| 7 | +use App\Models\ProjectRole as ProjectRoleModel; | ||
| 5 | use Closure; | 8 | use Closure; |
| 6 | use Illuminate\Http\Request; | 9 | use Illuminate\Http\Request; |
| 7 | - | 10 | +use Illuminate\Http\Response; |
| 11 | +use Illuminate\Support\Facades\Cache; | ||
| 12 | +use Illuminate\Http\Exceptions\HttpResponseException; | ||
| 8 | class LoginAuthMiddleware | 13 | class LoginAuthMiddleware |
| 9 | { | 14 | { |
| 10 | /** | 15 | /** |
| @@ -16,6 +21,44 @@ class LoginAuthMiddleware | @@ -16,6 +21,44 @@ class LoginAuthMiddleware | ||
| 16 | */ | 21 | */ |
| 17 | public function handle(Request $request, Closure $next) | 22 | public function handle(Request $request, Closure $next) |
| 18 | { | 23 | { |
| 24 | + $token = $request->header('token'); | ||
| 25 | + if(!isset($token) || empty($token)){ | ||
| 26 | + $this->response('当前用户未登录',Code::USER_ERROR); | ||
| 27 | + } | ||
| 28 | + $info = Cache::get($request->header('token')); | ||
| 29 | + if(empty($info)){ | ||
| 30 | + $this->response('当前用户未登录',Code::USER_ERROR); | ||
| 31 | + } | ||
| 32 | + //操作权限设置 | ||
| 33 | + $projectRoleModel = new ProjectRoleModel(); | ||
| 34 | + $role_info = $projectRoleModel->read(['id'=>$info['role_id']]); | ||
| 35 | + //获取当前操作的控制器与方法 | ||
| 36 | + $action = $request->route()->getAction(); | ||
| 37 | + //查询当前用户是否拥有权限操作 | ||
| 38 | + $projectMenuModel = new ProjectMenu(); | ||
| 39 | + $menu_id = $projectMenuModel->read(['action'=>$action['as']],['id']); | ||
| 40 | + | ||
| 41 | + if(strpos($role_info['role_menu'], $menu_id['id']) < 0){ | ||
| 42 | + $this->response('当前用户没有权限',Code::USER_ERROR); | ||
| 43 | + } | ||
| 19 | return $next($request); | 44 | return $next($request); |
| 20 | } | 45 | } |
| 46 | + /** | ||
| 47 | + * @name 统一返回参数 | ||
| 48 | + * @return void | ||
| 49 | + * @author :liyuhang | ||
| 50 | + * @method | ||
| 51 | + */ | ||
| 52 | + public function response($msg,$code,$data = [],$result_code = 200,$type = 'application/json'){ | ||
| 53 | + $code === null && $code = $result_code; | ||
| 54 | + $result = [ | ||
| 55 | + 'msg' =>$msg, | ||
| 56 | + 'code'=>$code, | ||
| 57 | + 'data'=>$data | ||
| 58 | + ]; | ||
| 59 | + $header['token'] = $type; | ||
| 60 | + $response = response($result,$result_code,$header); | ||
| 61 | + throw new HttpResponseException($response); | ||
| 62 | + } | ||
| 63 | + | ||
| 21 | } | 64 | } |
| @@ -8,7 +8,6 @@ namespace App\Services; | @@ -8,7 +8,6 @@ namespace App\Services; | ||
| 8 | 8 | ||
| 9 | use App\Enums\Common\Code; | 9 | use App\Enums\Common\Code; |
| 10 | use App\Exceptions\BsideGlobalException; | 10 | use App\Exceptions\BsideGlobalException; |
| 11 | -use App\Traits\RedisTrait; | ||
| 12 | 11 | ||
| 13 | class BaseService | 12 | class BaseService |
| 14 | { | 13 | { |
| @@ -7,6 +7,7 @@ use \Illuminate\Support\Facades\Route; | @@ -7,6 +7,7 @@ use \Illuminate\Support\Facades\Route; | ||
| 7 | //必须登录验证的路由组 | 7 | //必须登录验证的路由组 |
| 8 | Route::middleware(['bloginauth'])->group(function () { | 8 | Route::middleware(['bloginauth'])->group(function () { |
| 9 | 9 | ||
| 10 | + Route::any('/user/status', [\App\Http\Controllers\Bside\UserController::class, 'status'])->name('user_status'); | ||
| 10 | }); | 11 | }); |
| 11 | 12 | ||
| 12 | //无需登录验证的路由组 | 13 | //无需登录验证的路由组 |
| @@ -17,7 +18,7 @@ Route::group([], function () { | @@ -17,7 +18,7 @@ Route::group([], function () { | ||
| 17 | //用户相关路由 | 18 | //用户相关路由 |
| 18 | Route::any('/user/add', [\App\Http\Controllers\Bside\UserController::class, 'add'])->name('user_add'); | 19 | Route::any('/user/add', [\App\Http\Controllers\Bside\UserController::class, 'add'])->name('user_add'); |
| 19 | Route::any('/user/edit', [\App\Http\Controllers\Bside\UserController::class, 'edit'])->name('user_edit'); | 20 | Route::any('/user/edit', [\App\Http\Controllers\Bside\UserController::class, 'edit'])->name('user_edit'); |
| 20 | - Route::any('/user/status', [\App\Http\Controllers\Bside\UserController::class, 'status'])->name('user_status'); | 21 | + |
| 21 | Route::any('/user/lists', [\App\Http\Controllers\Bside\UserController::class, 'lists'])->name('user_lists'); | 22 | Route::any('/user/lists', [\App\Http\Controllers\Bside\UserController::class, 'lists'])->name('user_lists'); |
| 22 | Route::any('/user/del', [\App\Http\Controllers\Bside\UserController::class, 'del'])->name('user_del'); | 23 | Route::any('/user/del', [\App\Http\Controllers\Bside\UserController::class, 'del'])->name('user_del'); |
| 23 | //用户角色相关路由 | 24 | //用户角色相关路由 |
-
请 注册 或 登录 后发表评论