作者 赵彬吉

Merge branch 'dev' of http://47.244.231.31:8099/zhl/globalso-v6 into dev

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