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'); });