增加调用函数

This commit is contained in:
iVampireSP.com 2022-08-19 18:31:24 +08:00
parent 3a3fac7c55
commit a006156bb4
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
3 changed files with 25 additions and 8 deletions

View File

@ -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]);
}

View File

@ -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);

View File

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