From 5fb265c89ca845c5b5e52395560401937b973786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Wed, 8 Jun 2022 20:05:20 +0800 Subject: [PATCH 1/9] sync set http status. --- app/Exceptions/Exception.php | 5 +++-- app/Exceptions/Handler.php | 14 +++++++++----- app/Exceptions/InvalidRequestException.php | 3 ++- app/Modules/Admin/Services/BaseService.php | 2 +- ...212\237\350\203\275\345\210\227\350\241\250.md" | 3 +++ 5 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 "\345\276\205\345\256\236\347\216\260\345\212\237\350\203\275\345\210\227\350\241\250.md" diff --git a/app/Exceptions/Exception.php b/app/Exceptions/Exception.php index 7bc7360..5607556 100644 --- a/app/Exceptions/Exception.php +++ b/app/Exceptions/Exception.php @@ -11,10 +11,11 @@ class Exception extends \Exception protected $msg; - public function __construct($message = "success", $code = 0, Throwable $previous = null) + public function __construct($message = "success", $http_status = 400, Throwable $previous = null) { - parent::__construct($message, $code, $previous); + parent::__construct($message, $http_status, $previous); $this->msg = $message; + $this->setHttpCode($http_status); } } diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index d48b3ad..082ee04 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -3,6 +3,7 @@ namespace App\Exceptions; use App\Traits\Json; +use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Validation\ValidationException; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; @@ -68,22 +69,24 @@ class Handler extends ExceptionHandler // 控制器不存在 if ($exception instanceof BindingResolutionException){ - return $this->setJsonReturn($exception); + return $this->setJsonReturn($exception, 400); } // 模型不存在 if ($exception instanceof ModelNotFoundException){ - return $this->setJsonReturn($exception); + return $this->setJsonReturn($exception, 400); } // 验证器类的错误监听 if($exception instanceof ValidationException){ + $this->setHttpCode(400); return $this->errorJson($exception->validator->errors()->first()); } // 自定义Exception类的错误监听 if($exception instanceof Exception){ - return $this->setJsonReturn($exception); + $http_code = $exception->getCode(); + return $this->setJsonReturn($exception, $http_code); } // ErrorException类的监听 @@ -95,16 +98,17 @@ class Handler extends ExceptionHandler return parent::render($request, $exception); } - private function setJsonReturn($exception) + private function setJsonReturn($exception, $http_code = false) { $APP_DEBUG = env('APP_DEBUG'); // 设置HTTP的状态码 $http_status = isset($http_status) ? $http_status : (method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : 200); - + // 可设置`status`,但是也要限制 $status = $exception->getCode() != 1 ? 0 : 1; + $this->setHttpCode($http_status); return $this->errorJson($exception->getMessage(), $status, [], $APP_DEBUG ? [ 'file' => $exception->getFile(), 'line' => $exception->getLine(), diff --git a/app/Exceptions/InvalidRequestException.php b/app/Exceptions/InvalidRequestException.php index 954e53a..9c5e723 100644 --- a/app/Exceptions/InvalidRequestException.php +++ b/app/Exceptions/InvalidRequestException.php @@ -6,7 +6,7 @@ use Illuminate\Http\Request; class InvalidRequestException extends Exception { - public function __construct(string $message = "", int $code = 0) + public function __construct(string $message = "", int $code = 400) { parent::__construct($message, $code); } @@ -14,6 +14,7 @@ class InvalidRequestException extends Exception public function render(Request $request) { if ($request->expectsJson()) { + $this->setHttpCode($this->code); return $this->errorJson($this->msg); } } diff --git a/app/Modules/Admin/Services/BaseService.php b/app/Modules/Admin/Services/BaseService.php index a1ea49a..8737960 100644 --- a/app/Modules/Admin/Services/BaseService.php +++ b/app/Modules/Admin/Services/BaseService.php @@ -87,7 +87,7 @@ class BaseService extends Service } $this->detail = $this->model->find($params[$primaryKey]); if (!$this->detail){ - throw new Exception('编辑信息不存在!'); + throw new Exception('编辑信息不存在!', 403); } foreach ($this->model->setFilterFields($params) as $field => $value){ $this->detail->$field = $value ?? ''; diff --git "a/\345\276\205\345\256\236\347\216\260\345\212\237\350\203\275\345\210\227\350\241\250.md" "b/\345\276\205\345\256\236\347\216\260\345\212\237\350\203\275\345\210\227\350\241\250.md" new file mode 100644 index 0000000..7f2ec56 --- /dev/null +++ "b/\345\276\205\345\256\236\347\216\260\345\212\237\350\203\275\345\210\227\350\241\250.md" @@ -0,0 +1,3 @@ +- API的响应状态码设置http状态码 +- 关于页面的按钮权限优化 + - 如果无权限则不展示,那么需要新加接口,效验当前的所有按钮权限 \ No newline at end of file -- Gitee From 9f29916d705a02a6ede7d5ec63319a56fcaa1b2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Wed, 8 Jun 2022 20:07:58 +0800 Subject: [PATCH 2/9] optimize AdminLog middleware --- .../Admin/Http/Middleware/AdminLog.php | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/app/Modules/Admin/Http/Middleware/AdminLog.php b/app/Modules/Admin/Http/Middleware/AdminLog.php index 34fa6f7..23615fa 100644 --- a/app/Modules/Admin/Http/Middleware/AdminLog.php +++ b/app/Modules/Admin/Http/Middleware/AdminLog.php @@ -26,7 +26,8 @@ class AdminLog if ($method != 'GET'){ $ip_agent = get_client_info(); - $admin_log = \App\Modules\Admin\Entities\Log\AdminLog::getInstance()->create([ + + $log_data = [ 'request_data' => json_encode($request->all()), 'admin_id' => !empty(auth($guard)->user()) ? auth($guard)->user()->admin_id : 0, 'created_ip' => $ip_agent['ip'] ?? get_ip(), @@ -38,7 +39,7 @@ class AdminLog 'request_url' => URL::full() ?? get_this_url(), // 默认值 'log_status' => 0, - ]); + ]; $log_status = 0; try{ @@ -49,21 +50,18 @@ class AdminLog // 根据接口响应,存储返回状态与文本提示语 $log_status = $response_body_content->status; - $log_description = empty($adminlog->log_description) ? $response_body_content->msg : $adminlog->log_description; + $log_description = empty($log_data['log_description']) ? $response_body_content->msg : $log_data['log_description']; }catch(\Exception $e){ $log_description = $e->getMessage(); $response = $this->errorJson($log_description); } // 同步更新响应状态与文本,在`handler`层可能会被异常终止 - $admin_log->update( - [ - 'log_duration' => microtime(true) - LARAVEL_START, - // 根据接口响应,存储返回状态与文本提示语 - 'log_status' => $log_status, - 'description' => $log_description, - ] - ); + $log_data['log_duration'] = microtime(true) - LARAVEL_START; + // 根据接口响应,存储返回状态与文本提示语 + $log_data['log_status'] = $log_status; + $log_data['description'] = $log_description; + \App\Modules\Admin\Entities\Log\AdminLog::getInstance()->create($log_data); return $response; } -- Gitee From 6b9bb61b4a418b5d47406b3b57acba12ad62b65b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Wed, 8 Jun 2022 20:10:00 +0800 Subject: [PATCH 3/9] optimize --- app/Traits/Json.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/Traits/Json.php b/app/Traits/Json.php index 99c86b5..fc74363 100644 --- a/app/Traits/Json.php +++ b/app/Traits/Json.php @@ -35,7 +35,16 @@ trait Json { $data['data'] = $data['data'] ?? []; $data['status'] = intval($data['status'] ?? (empty($data['data']) ? 0 : 1)); + switch ($data['status']){ + case -1: + $this->http_code = 401; + break; + case -2: + $this->http_code = 403; + break; + } $data['msg'] = $data['msg'] ?? (empty($data['status']) ? '数据不存在!' : 'success'); + $data['http_code'] = $this->http_code; return response()->json($data, $this->http_code); } -- Gitee From 18c0f34947b66025e8f634f50433c6850a86aa0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Wed, 8 Jun 2022 20:10:58 +0800 Subject: [PATCH 4/9] optimize CheckIpBlacklist --- app/Modules/Admin/Http/Middleware/CheckIpBlacklist.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Modules/Admin/Http/Middleware/CheckIpBlacklist.php b/app/Modules/Admin/Http/Middleware/CheckIpBlacklist.php index bfd47b0..75bc2de 100644 --- a/app/Modules/Admin/Http/Middleware/CheckIpBlacklist.php +++ b/app/Modules/Admin/Http/Middleware/CheckIpBlacklist.php @@ -34,6 +34,10 @@ class CheckIpBlacklist $ip_blacklists_array = array_flip($ip_blacklists_array); if (isset($ip_blacklists_array[$client_ip])){ $msg = '您的IP段在系统黑名单中,禁止访问!'; + if ($request->isJson()){ + $this->setHttpCode(403); + return $this->errorJson($msg); + } abort(403, $msg); } } -- Gitee From 0d19559e84b867d8660683cf452ea120fcafb3fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Wed, 8 Jun 2022 20:22:26 +0800 Subject: [PATCH 5/9] optimize --- app/Exceptions/Admin/AuthException.php | 7 +++---- app/Exceptions/Admin/AuthTokenException.php | 7 +++---- app/Exceptions/Handler.php | 4 ++-- app/Exceptions/InternalException.php | 7 +++---- app/Exceptions/InvalidRequestException.php | 7 +++---- app/Modules/Admin/Http/Middleware/CheckIpBlacklist.php | 2 +- app/Modules/Admin/Services/AuthService.php | 4 +--- 7 files changed, 16 insertions(+), 22 deletions(-) diff --git a/app/Exceptions/Admin/AuthException.php b/app/Exceptions/Admin/AuthException.php index dbac672..a7a0853 100644 --- a/app/Exceptions/Admin/AuthException.php +++ b/app/Exceptions/Admin/AuthException.php @@ -9,9 +9,9 @@ use Illuminate\Http\Request; class AuthException extends Exception { protected $admin_id = 0; - public function __construct(string $message = "", int $code = 0, int $admin_id = 0) + public function __construct(string $message = "", int $http_status = 401, int $admin_id = 0) { - parent::__construct($message, $code); + parent::__construct($message, $http_status); $this->admin_id = $admin_id; } @@ -21,8 +21,7 @@ class AuthException extends Exception // 登录日志 AdminLoginLog::getInstance()->add($this->admin_id, 0, $this->msg); - $this->setHttpCode(401); - return $this->errorJson($this->msg); + return $this->errorJson($this->getMessage(), $this->getCode()); } } } diff --git a/app/Exceptions/Admin/AuthTokenException.php b/app/Exceptions/Admin/AuthTokenException.php index 70d58bd..40f162c 100644 --- a/app/Exceptions/Admin/AuthTokenException.php +++ b/app/Exceptions/Admin/AuthTokenException.php @@ -9,9 +9,9 @@ use Illuminate\Http\Request; class AuthTokenException extends Exception { protected $admin_id = 0; - public function __construct(string $message = "", int $code = 0, int $admin_id = 0) + public function __construct(string $message = "", int $http_status = 401, int $admin_id = 0) { - parent::__construct($message, $code); + parent::__construct($message, $http_status); $this->admin_id = $admin_id; } @@ -21,8 +21,7 @@ class AuthTokenException extends Exception // 登录日志 AdminLoginLog::getInstance()->add($this->admin_id, 0, $this->msg); - $this->setHttpCode(401); - return $this->errorJson($this->msg); + return $this->errorJson($this->getMessage(), $this->getCode()); } } } diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 082ee04..d143c5f 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -98,12 +98,12 @@ class Handler extends ExceptionHandler return parent::render($request, $exception); } - private function setJsonReturn($exception, $http_code = false) + private function setJsonReturn($exception, $http_status = false) { $APP_DEBUG = env('APP_DEBUG'); // 设置HTTP的状态码 - $http_status = isset($http_status) ? $http_status : (method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : 200); + $http_status = $http_status ? $http_status : (method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : (method_exists($exception, 'getCode') ? $exception->getCode() : 200)); // 可设置`status`,但是也要限制 $status = $exception->getCode() != 1 ? 0 : 1; diff --git a/app/Exceptions/InternalException.php b/app/Exceptions/InternalException.php index b9429b2..8b76c69 100644 --- a/app/Exceptions/InternalException.php +++ b/app/Exceptions/InternalException.php @@ -6,16 +6,15 @@ use Illuminate\Http\Request; class InternalException extends Exception { - public function __construct(string $message, string $msg = '系统内部错误', int $code = 500) + public function __construct(string $message = '系统内部错误', int $http_status = 500) { - parent::__construct($message, $code); - $this->msg = $msg; + parent::__construct($message, $http_status); } public function render(Request $request) { if ($request->expectsJson()) { - return response()->json(['msg' => $this->msg], $this->code); + return response()->json(['msg' => $this->getMessage()], $this->getCode()); } } } diff --git a/app/Exceptions/InvalidRequestException.php b/app/Exceptions/InvalidRequestException.php index 9c5e723..a6a1b7c 100644 --- a/app/Exceptions/InvalidRequestException.php +++ b/app/Exceptions/InvalidRequestException.php @@ -6,16 +6,15 @@ use Illuminate\Http\Request; class InvalidRequestException extends Exception { - public function __construct(string $message = "", int $code = 400) + public function __construct(string $message = "", int $http_status = 400) { - parent::__construct($message, $code); + parent::__construct($message, $http_status); } public function render(Request $request) { if ($request->expectsJson()) { - $this->setHttpCode($this->code); - return $this->errorJson($this->msg); + return $this->errorJson($this->getMessage(), $this->getCode()); } } } diff --git a/app/Modules/Admin/Http/Middleware/CheckIpBlacklist.php b/app/Modules/Admin/Http/Middleware/CheckIpBlacklist.php index 75bc2de..bf0834b 100644 --- a/app/Modules/Admin/Http/Middleware/CheckIpBlacklist.php +++ b/app/Modules/Admin/Http/Middleware/CheckIpBlacklist.php @@ -34,7 +34,7 @@ class CheckIpBlacklist $ip_blacklists_array = array_flip($ip_blacklists_array); if (isset($ip_blacklists_array[$client_ip])){ $msg = '您的IP段在系统黑名单中,禁止访问!'; - if ($request->isJson()){ + if ($request->expectsJson()){ $this->setHttpCode(403); return $this->errorJson($msg); } diff --git a/app/Modules/Admin/Services/AuthService.php b/app/Modules/Admin/Services/AuthService.php index 9aebc8a..28634c3 100644 --- a/app/Modules/Admin/Services/AuthService.php +++ b/app/Modules/Admin/Services/AuthService.php @@ -4,7 +4,6 @@ namespace App\Modules\Admin\Services; use App\Exceptions\Admin\AuthException; use App\Exceptions\Admin\AuthTokenException; -use App\Exceptions\InvalidRequestException; use App\Modules\Admin\Entities\Log\AdminLoginLog; use App\Modules\Admin\Entities\Rabc\Admin; use App\Modules\Admin\Entities\Rabc\AdminMenu; @@ -21,7 +20,6 @@ class AuthService extends Service * @param $data * @return array * @throws AuthException - * @throws InvalidRequestException */ public function login($data) { @@ -60,7 +58,7 @@ class AuthService extends Service /** * 获取拥有的权限 - * + * * @throws \App\Exceptions\Admin\AuthTokenException */ public function getRabcList() -- Gitee From 56dff45d52da924c9ee4caafcdcd9fdaf981cd3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Thu, 9 Jun 2022 11:40:31 +0800 Subject: [PATCH 6/9] optimize httpstatus and status. --- app/Exceptions/Admin/AuthException.php | 2 +- app/Exceptions/Admin/AuthTokenException.php | 2 +- app/Exceptions/Handler.php | 5 +--- .../Admin/Http/Middleware/CheckAuth.php | 6 ++--- .../Admin/Http/Middleware/CheckRabc.php | 2 +- app/Traits/Json.php | 26 ++++++++++++------- 6 files changed, 23 insertions(+), 20 deletions(-) diff --git a/app/Exceptions/Admin/AuthException.php b/app/Exceptions/Admin/AuthException.php index a7a0853..0b31538 100644 --- a/app/Exceptions/Admin/AuthException.php +++ b/app/Exceptions/Admin/AuthException.php @@ -19,7 +19,7 @@ class AuthException extends Exception { if ($request->expectsJson()) { // 登录日志 - AdminLoginLog::getInstance()->add($this->admin_id, 0, $this->msg); + AdminLoginLog::getInstance()->add($this->admin_id, 0, $this->getMessage()); return $this->errorJson($this->getMessage(), $this->getCode()); } diff --git a/app/Exceptions/Admin/AuthTokenException.php b/app/Exceptions/Admin/AuthTokenException.php index 40f162c..f26a62c 100644 --- a/app/Exceptions/Admin/AuthTokenException.php +++ b/app/Exceptions/Admin/AuthTokenException.php @@ -19,7 +19,7 @@ class AuthTokenException extends Exception { if ($request->expectsJson()) { // 登录日志 - AdminLoginLog::getInstance()->add($this->admin_id, 0, $this->msg); + AdminLoginLog::getInstance()->add($this->admin_id, 0, $this->getMessage()); return $this->errorJson($this->getMessage(), $this->getCode()); } diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index d143c5f..1b9b6a2 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -105,11 +105,8 @@ class Handler extends ExceptionHandler // 设置HTTP的状态码 $http_status = $http_status ? $http_status : (method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : (method_exists($exception, 'getCode') ? $exception->getCode() : 200)); - // 可设置`status`,但是也要限制 - $status = $exception->getCode() != 1 ? 0 : 1; - $this->setHttpCode($http_status); - return $this->errorJson($exception->getMessage(), $status, [], $APP_DEBUG ? [ + return $this->errorJson($exception->getMessage(), $http_status, [], $APP_DEBUG ? [ 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'code' => $exception->getCode(), diff --git a/app/Modules/Admin/Http/Middleware/CheckAuth.php b/app/Modules/Admin/Http/Middleware/CheckAuth.php index 26fd33d..f872676 100644 --- a/app/Modules/Admin/Http/Middleware/CheckAuth.php +++ b/app/Modules/Admin/Http/Middleware/CheckAuth.php @@ -29,12 +29,12 @@ class CheckAuth $auth = Auth()->guard($this->guard); try { if ( !$auth->check() ) { //未登录踢回,给予错误返回提示 - return $this->errorJson('认证失败,请重新登录!', -1); + return $this->errorJson('认证失败,请重新登录!', 401); } } catch (TokenExpiredException $e) { - return $this->errorJson($e->getMessage(), -1); + return $this->errorJson($e->getMessage(), 401); } catch (TokenInvalidException $e) { - return $this->errorJson($e->getMessage(), -1); + return $this->errorJson($e->getMessage(), 401); } catch (JWTException $e) { return $this->errorJson($e->getMessage()); } diff --git a/app/Modules/Admin/Http/Middleware/CheckRabc.php b/app/Modules/Admin/Http/Middleware/CheckRabc.php index dec3e91..15bf004 100644 --- a/app/Modules/Admin/Http/Middleware/CheckRabc.php +++ b/app/Modules/Admin/Http/Middleware/CheckRabc.php @@ -29,7 +29,7 @@ class CheckRabc $this->guard = 'admin'; // 开始验证路由权限 if (!$this->checkRabc($request, Auth()->guard($this->guard)->user()->getAuthIdentifier(), $load_error)){ - return $this->errorJson('无权限' . (empty($load_error) ? '!' : ',' . $load_error), -2); + return $this->errorJson('无权限' . (empty($load_error) ? '!' : ',' . $load_error), 403); } return $next($request); diff --git a/app/Traits/Json.php b/app/Traits/Json.php index fc74363..f81de52 100644 --- a/app/Traits/Json.php +++ b/app/Traits/Json.php @@ -10,12 +10,12 @@ trait Json public function successJson($data = [], $msg = 'success', $other = []) { - return $this->myAjaxReturn(array_merge(['data' => $data, 'msg' => $msg, 'status' => 1], $other)); + return $this->myAjaxReturn(array_merge(['data' => $data, 'msg' => $msg, 'http_code' => 200], $other)); } - public function errorJson($msg = 'error', $status = 0, $data = [], $other = []) + public function errorJson($msg = 'error', $http_code = 0, $data = [], $other = []) { - return $this->myAjaxReturn(array_merge(['msg' => $msg, 'status' => $status, 'data' => $data], $other)); + return $this->myAjaxReturn(array_merge(['msg' => $msg, 'http_code' => $http_code, 'data' => $data], $other)); } public function setHttpCode(int $http_code): void @@ -34,17 +34,23 @@ trait Json public function myAjaxReturn($data) { $data['data'] = $data['data'] ?? []; - $data['status'] = intval($data['status'] ?? (empty($data['data']) ? 0 : 1)); - switch ($data['status']){ - case -1: - $this->http_code = 401; + // $data['status'] = intval($data['status'] ?? (empty($data['data']) ? 0 : 1)); + if(!isset($data['http_code'])) $data['http_code'] = $this->http_code; + switch ($data['http_code']){ + case 200: + $data['status'] = 1; break; - case -2: - $this->http_code = 403; + case 400: + $data['status'] = 0; + break; + case 401: + $data['status'] = -1; + break; + case 403: + $data['status'] = -2; break; } $data['msg'] = $data['msg'] ?? (empty($data['status']) ? '数据不存在!' : 'success'); - $data['http_code'] = $this->http_code; return response()->json($data, $this->http_code); } -- Gitee From 91b2b9a20ee3ae057afc280d64dbdcecadd642d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Thu, 9 Jun 2022 17:50:51 +0800 Subject: [PATCH 7/9] vue request http status --- .../Admin/Resources/vue-element-admin/utils/request.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/Modules/Admin/Resources/vue-element-admin/utils/request.js b/app/Modules/Admin/Resources/vue-element-admin/utils/request.js index b3df91f..c8d1284 100644 --- a/app/Modules/Admin/Resources/vue-element-admin/utils/request.js +++ b/app/Modules/Admin/Resources/vue-element-admin/utils/request.js @@ -69,7 +69,6 @@ service.interceptors.response.use( */ response => { const res = response.data; - // if the custom code is not 20000, it is judged as an error. if (res.status !== 1) { Message({ @@ -104,13 +103,16 @@ service.interceptors.response.use( msg = '超时 ' + timeout + ' ms,请刷新!'; }else{ switch (error.response.status) { - case 404: + case 400: msg = error.response.statusText; break; case 401: // 认证失败 msg = error.response.data.msg; break; - case 500: // 认证失败 + case 404: + msg = error.response.statusText; + break; + case 500: msg = error.response.statusText; break; } -- Gitee From 246f8125cef12bb1e3a5e3b51061ab81e8450fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Fri, 10 Jun 2022 12:08:20 +0800 Subject: [PATCH 8/9] optimize vue.request http_status. --- .../vue-element-admin/utils/request.js | 30 ++++++++++++++++++- app/Traits/Json.php | 2 +- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/Modules/Admin/Resources/vue-element-admin/utils/request.js b/app/Modules/Admin/Resources/vue-element-admin/utils/request.js index c8d1284..16ae215 100644 --- a/app/Modules/Admin/Resources/vue-element-admin/utils/request.js +++ b/app/Modules/Admin/Resources/vue-element-admin/utils/request.js @@ -97,31 +97,59 @@ service.interceptors.response.use( } }, error => { + let callback = null; console.log('err' + error); // for debug let msg = error.msg; if (error.response == undefined){ msg = '超时 ' + timeout + ' ms,请刷新!'; }else{ + console.log(error.response); switch (error.response.status) { case 400: msg = error.response.statusText; break; case 401: // 认证失败 - msg = error.response.data.msg; + msg = error.response.data.msg ? error.response.data.msg : error.response.statusText; + callback = function(){ + // to re-login + MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', + 'Confirm logout', { + confirmButtonText: 'Re-Login', + cancelButtonText: 'Cancel', + type: 'warning' + }).then(() => { + store.dispatch('user/resetToken').then(() => { + location.reload(); + }) + }) + }; break; case 404: msg = error.response.statusText; break; + case 403: + msg = error.response.data.msg ? error.response.data.msg : error.response.statusText; + break; case 500: msg = error.response.statusText; break; + default: + msg = error.response.data.msg ? error.response.data.msg : error.response.statusText; + break; } } + Message({ message: msg, type: 'error', duration: 5 * 1000 }); + + // 执行闭包 + if (callback){ + callback(); + } + return Promise.reject(error); } ) diff --git a/app/Traits/Json.php b/app/Traits/Json.php index f81de52..575eec8 100644 --- a/app/Traits/Json.php +++ b/app/Traits/Json.php @@ -52,6 +52,6 @@ trait Json } $data['msg'] = $data['msg'] ?? (empty($data['status']) ? '数据不存在!' : 'success'); - return response()->json($data, $this->http_code); + return response()->json($data, $data['http_code']); } } -- Gitee From e9cba476c336333fc0c2e25b0728ba2f70b3701d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E4=B8=91=E8=B7=AF=E4=BA=BA?= <2278757482@qq.com> Date: Fri, 10 Jun 2022 13:59:58 +0800 Subject: [PATCH 9/9] optimize --- .../Admin/Resources/vue-element-admin/utils/request.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/app/Modules/Admin/Resources/vue-element-admin/utils/request.js b/app/Modules/Admin/Resources/vue-element-admin/utils/request.js index 16ae215..84d48b3 100644 --- a/app/Modules/Admin/Resources/vue-element-admin/utils/request.js +++ b/app/Modules/Admin/Resources/vue-element-admin/utils/request.js @@ -8,13 +8,10 @@ import { getToken } from '@/utils/auth'; -console.log(process); -// console.log(process.env); -// console.log(process.env.VUE_APP_BASE_API); +// console.log(process); // process失效了,默认为当前URL为请求地址 process.env.VUE_APP_BASE_API = window.location.origin + window.location.pathname; -// console.log(process.env.VUE_APP_BASE_API); let timeout = 15000; @@ -103,7 +100,6 @@ service.interceptors.response.use( if (error.response == undefined){ msg = '超时 ' + timeout + ' ms,请刷新!'; }else{ - console.log(error.response); switch (error.response.status) { case 400: msg = error.response.statusText; @@ -138,7 +134,6 @@ service.interceptors.response.use( break; } } - Message({ message: msg, type: 'error', -- Gitee