From 9bb969577e8cf134262214dee2c443a69a7c0440 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Thu, 19 Jan 2023 03:04:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=9B=9E=E5=BA=94=E7=9A=84=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E5=A4=84=E7=90=86=20=E8=87=AA=E5=8A=A8=E5=B0=86=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E5=88=87=E6=8D=A2=E4=B8=BA=E7=BB=B4=E6=8A=A4=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E5=B9=B6=E9=98=B2=E6=AD=A2=E6=84=8F=E5=A4=96=E5=88=A0?= =?UTF-8?q?=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Jobs/Host/HostJob.php | 4 ++-- app/Jobs/Host/UpdateOrDeleteHostJob.php | 12 ++++-------- app/Models/Module.php | 15 ++++++++++++--- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/app/Jobs/Host/HostJob.php b/app/Jobs/Host/HostJob.php index 8d4e355..1c9801e 100644 --- a/app/Jobs/Host/HostJob.php +++ b/app/Jobs/Host/HostJob.php @@ -50,10 +50,10 @@ public function handle(): void break; case 'delete': - $response = $host->module->http()->delete('hosts/' . $host->id); + $response = $host->module->baseRequest('delete', 'hosts/' . $host->id); // if successful - if ($response->successful() || $response->status() === 404) { + if ($response['status'] === 404) { $host->delete(); } diff --git a/app/Jobs/Host/UpdateOrDeleteHostJob.php b/app/Jobs/Host/UpdateOrDeleteHostJob.php index f5d3778..6bb0a67 100644 --- a/app/Jobs/Host/UpdateOrDeleteHostJob.php +++ b/app/Jobs/Host/UpdateOrDeleteHostJob.php @@ -36,15 +36,11 @@ public function handle(): void { $host = $this->host; - $response = $host->module->http()->get('hosts/' . $host->id); + $response = $host->module->baseRequest('get', 'hosts/' . $host->id); - $status = $response->status(); - - $json = $response->json(); - - if ($status === 200) { - $host->update(Arr::except($json, ['id', 'user_id', 'module_id', 'created_at', 'updated_at'])); - } else if ($status === 404) { + if ($response['status'] === 200) { + $host->update(Arr::except($response['json'], ['id', 'user_id', 'module_id', 'created_at', 'updated_at'])); + } else if ($response['status'] === 404) { Log::warning($host->module->name . ' ' . $host->name . ' ' . $host->id . ' 不存在,删除。'); dispatch(new HostJob($host, 'delete')); } diff --git a/app/Models/Module.php b/app/Models/Module.php index 3fe4412..1957e93 100644 --- a/app/Models/Module.php +++ b/app/Models/Module.php @@ -76,14 +76,23 @@ public function http(): PendingRequest private function getResponse(Response $response): array { - $json = $response->json(); - $status = $response->status(); + $module_token = $response->header('x-module-api-token'); $success = true; + $json = $response->json(); + $status = $response->status(); // if status code is not 20x if ($status < 200 || $status >= 300) { $success = false; + + // 防止误删除 + if ($module_token !== $this->api_token) { + $this->status = 'maintenance'; + $this->save(); + + $status = 401; + } } return [ @@ -99,7 +108,7 @@ public function request($method, $path, $requests): array return $this->baseRequest($method, "functions/$path", $requests); } - public function baseRequest($method, $path, $requests): array + public function baseRequest($method, $path, $requests = []): array { $user = auth('sanctum')->user();