From a006156bb448db64fd34412e469f4e8bffb5eaa9 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Fri, 19 Aug 2022 18:31:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=B0=83=E7=94=A8=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Remote/CallController.php | 19 ++++++++++++------- app/Models/Module/Module.php | 11 +++++++++++ routes/api/client.php | 3 ++- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Remote/CallController.php b/app/Http/Controllers/Remote/CallController.php index 465d011..4feadf7 100644 --- a/app/Http/Controllers/Remote/CallController.php +++ b/app/Http/Controllers/Remote/CallController.php @@ -2,19 +2,24 @@ namespace App\Http\Controllers\Remote; -use App\Http\Controllers\Controller; -use App\Models\Module\Module; +use App\Models\Host; use Illuminate\Http\Request; +use App\Models\Module\Module; +use App\Http\Controllers\Controller; class CallController extends Controller { // invoke the remote method - public function __invoke(Request $request, Module $module) { - $request->validate([ - 'func' => 'required|string|max:255', - ]); + public function host(Request $request, Host $host, $func) { + $host->load('module'); + $response = $host->module->remoteHost($host->id, $func, $request->all()); - $response = $module->remote($request->func, $request->all()); + return $this->apiResponse($response[0], $response[1]); + } + + public function module(Request $request, Module $module, $func) + { + $response = $module->remote($func, $request->all()); return $this->apiResponse($response[0], $response[1]); } diff --git a/app/Models/Module/Module.php b/app/Models/Module/Module.php index a0d6061..b255212 100644 --- a/app/Models/Module/Module.php +++ b/app/Models/Module/Module.php @@ -26,6 +26,17 @@ class Module extends Authenticatable ]; + public function remoteHost($host_id, $func, $requests) + { + $http = Http::remote($this->api_token, $this->url); + $response = $http->post("hosts/{$host_id}/functions/" . $func, $requests); + + $json = $response->json(); + $status = $response->status(); + + return [$json, $status]; + } + public function remote($func, $requests) { $http = Http::remote($this->api_token, $this->url); diff --git a/routes/api/client.php b/routes/api/client.php index 3286e6f..117911a 100644 --- a/routes/api/client.php +++ b/routes/api/client.php @@ -14,6 +14,7 @@ Route::apiResource('work-orders.replies', User\WorkOrder\ReplyController::class); // 调用远程 API - Route::post('module/{module}', CallController::class)->name('call'); + Route::post('hosts/{host}/func/{func}', [CallController::class, 'host'])->name('host.call'); + Route::post('/modules/{module}/func/{func}', [CallController::class, 'module'])->name('module.call'); });