From 5eb6f8b5cdbc705c7536286189cdd01dc0d24ce2 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Wed, 30 Nov 2022 15:57:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Jobs/CheckHostIfExistsOnModule.php | 3 +-- app/Jobs/Module/Host.php | 8 +++----- app/Jobs/Module/PushHost.php | 3 +-- app/Jobs/Module/PushWorkOrder.php | 3 +-- app/Jobs/Module/WorkOrder/Reply.php | 4 +--- app/Jobs/Module/WorkOrder/WorkOrder.php | 7 +++---- app/Models/Module.php | 26 ++++++++++++------------- 7 files changed, 22 insertions(+), 32 deletions(-) diff --git a/app/Jobs/CheckHostIfExistsOnModule.php b/app/Jobs/CheckHostIfExistsOnModule.php index 430ed3a..aecf34e 100644 --- a/app/Jobs/CheckHostIfExistsOnModule.php +++ b/app/Jobs/CheckHostIfExistsOnModule.php @@ -41,8 +41,7 @@ public function handle() continue; } - $http = Http::module($host->module->api_token, $host->module->url); - $response = $http->get('hosts/' . $host->id); + $response = $host->module->http()->get('hosts/' . $host->id); if ($response->status() === 404) { Log::warning($host->module->name . ' ' . $host->name . ' ' . $host->id . ' 不存在,删除。'); diff --git a/app/Jobs/Module/Host.php b/app/Jobs/Module/Host.php index f67af62..8f006a7 100644 --- a/app/Jobs/Module/Host.php +++ b/app/Jobs/Module/Host.php @@ -42,19 +42,17 @@ public function handle() $host = $this->host; $host->load(['module']); - $http = Http::module($host->module->api_token, $host->module->url); - switch ($this->type) { case 'patch': - $response = $http->patch('hosts/' . $host->id, $host->toArray()); + $response = $host->module->http()->patch('hosts/' . $host->id, $host->toArray()); break; case 'post': - $response = $http->post('hosts', $host->toArray()); + $response = $host->module->http()->post('hosts', $host->toArray()); break; case 'delete': - $response = $http->delete('hosts/' . $host->id); + $response = $host->module->http()->delete('hosts/' . $host->id); // if successful if ($response->successful() || $response->status() === 404) { diff --git a/app/Jobs/Module/PushHost.php b/app/Jobs/Module/PushHost.php index 917aea7..56345d4 100644 --- a/app/Jobs/Module/PushHost.php +++ b/app/Jobs/Module/PushHost.php @@ -35,10 +35,9 @@ public function handle() // Host::whereIn('status', ['pending', 'error'])->with(['module', 'user'])->chunk(100, function ($hosts) { foreach ($hosts as $host) { - $http = Http::module($host->module->api_token, $host->module->url); $host->status = 'running'; - $response = $http->post('hosts', $host->toArray()); + $response = $host->module->http()->post('hosts', $host->toArray()); if (!$response->successful()) { $host->status = 'error'; diff --git a/app/Jobs/Module/PushWorkOrder.php b/app/Jobs/Module/PushWorkOrder.php index e7bcdcf..9eee570 100644 --- a/app/Jobs/Module/PushWorkOrder.php +++ b/app/Jobs/Module/PushWorkOrder.php @@ -53,10 +53,9 @@ public function handle() } } - $http = Http::module($workOrder->module->api_token, $workOrder->module->url); $workOrder->status = 'open'; - $response = $http->post('work-orders', $workOrder->toArray()); + $response = $workOrder->module->http()->post('work-orders', $workOrder->toArray()); if (!$response->successful()) { Log::error('推送工单失败', [ diff --git a/app/Jobs/Module/WorkOrder/Reply.php b/app/Jobs/Module/WorkOrder/Reply.php index 6a2d854..936be09 100644 --- a/app/Jobs/Module/WorkOrder/Reply.php +++ b/app/Jobs/Module/WorkOrder/Reply.php @@ -40,11 +40,9 @@ public function handle() $this->reply->load(['workOrder', 'user']); $this->reply->workOrder->load(['module']); - $http = Http::module($this->reply->workOrder->module->api_token, $this->reply->workOrder->module->url); - $reply = $this->reply->toArray(); - $response = $http->post('work-orders/' . $this->reply->workOrder->id . '/replies', $reply); + $response = $this->reply->workOrder->module->http()->post('work-orders/' . $this->reply->workOrder->id . '/replies', $reply); if ($response->successful()) { $this->reply->update([ diff --git a/app/Jobs/Module/WorkOrder/WorkOrder.php b/app/Jobs/Module/WorkOrder/WorkOrder.php index 30356b8..d91f878 100644 --- a/app/Jobs/Module/WorkOrder/WorkOrder.php +++ b/app/Jobs/Module/WorkOrder/WorkOrder.php @@ -39,17 +39,16 @@ public function handle() { $this->workOrder->load(['module']); - $http = Http::module($this->workOrder->module->api_token, $this->workOrder->module->url); if ($this->type == 'put') { - $response = $http->put('work-orders/' . $this->workOrder->id, $this->workOrder->toArray()); + $response = $this->workOrder->module->http()->put('work-orders/' . $this->workOrder->id, $this->workOrder->toArray()); } else if ($this->type == 'delete') { - $response = $http->delete('work-orders/' . $this->workOrder->id); + $response = $this->workOrder->module->http()->delete('work-orders/' . $this->workOrder->id); if ($response->successful()) { $this->workOrder->delete(); } } else { - $response = $http->post('work-orders', $this->workOrder->toArray()); + $response = $this->workOrder->module->http()->post('work-orders', $this->workOrder->toArray()); } if (!$response->successful()) { diff --git a/app/Models/Module.php b/app/Models/Module.php index 058353b..3bc3e8d 100644 --- a/app/Models/Module.php +++ b/app/Models/Module.php @@ -5,6 +5,7 @@ use GeneaLabs\LaravelModelCaching\Traits\Cachable; use GuzzleHttp\Exception\ConnectException; use Illuminate\Foundation\Auth\User as Authenticatable; +use Illuminate\Http\Client\PendingRequest; use Illuminate\Http\Client\Response; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Http; @@ -94,8 +95,7 @@ protected static function boot() // post, get, patch, delete 等请求 public function remote($func, $requests): array { - $http = Http::module($this->api_token, $this->url); - $response = $http->post('functions/' . $func, $requests); + $response = $this->http()->post('functions/' . $func, $requests); return $this->getResponse($response); } @@ -109,10 +109,8 @@ public function baseRequest($method, $path, $requests): array { $user = auth('sanctum')->user(); - $http = Http::module($this->api_token, $this->url); - if ($user) { - $http->withHeaders([ + $this->http()->withHeaders([ 'X-User-id' => $user->id, ]); $requests['user_id'] = $user->id; @@ -122,7 +120,7 @@ public function baseRequest($method, $path, $requests): array } } - $response = $http->{$method}($path, $requests); + $response = $this->http()->{$method}($path, $requests); return $this->getResponse($response); } @@ -131,16 +129,13 @@ public function moduleRequest($method, $path, $requests): array { $module_id = auth('module')->id(); - $http = Http::module($this->api_token, $this->url) - ->accept('application/json'); - - $http->withHeaders([ + $this->http()->withHeaders([ 'X-Module' => $module_id ]); $requests['module_id'] = $module_id; - $response = $http->{$method}("exports/{$path}", $requests); + $response = $this->http()->{$method}("exports/{$path}", $requests); return $this->getResponse($response); } @@ -165,12 +160,10 @@ public function check($module_id = null): bool $module = $this; } - $http = Http::module($module->api_token, $module->url); - $success = 0; try { - $response = $http->get('remote'); + $response = $this->http()->get('remote'); if ($response->status() == 200) { $success = 1; @@ -182,6 +175,11 @@ public function check($module_id = null): bool return $success; } + public function http(): PendingRequest + { + return Http::module($this->api_token, $this->url)->acceptJson(); + } + #[ArrayShape(['transactions' => "array"])] public function calculate(): array {