From bc11f5605d8560c925039f62e3422bda59678a5f Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Mon, 19 Sep 2022 21:39:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20Remote=20=E8=B0=83?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Remote/UserController.php | 4 ++-- app/Models/Module/Module.php | 18 ++++++++++++++---- routes/remote.php | 6 +++++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Remote/UserController.php b/app/Http/Controllers/Remote/UserController.php index 9056ed9..bc05409 100644 --- a/app/Http/Controllers/Remote/UserController.php +++ b/app/Http/Controllers/Remote/UserController.php @@ -23,9 +23,9 @@ public function show(User $user) return $this->success($user); } - public function hosts(Request $request, User $user) + public function hosts(User $user) { - $hosts = (new Host())->getUserHosts($request->module_id ?? null); + $hosts = (new Host())->getUserHosts($user->id); return $this->success($hosts); } diff --git a/app/Models/Module/Module.php b/app/Models/Module/Module.php index f8ddb63..42b3d11 100644 --- a/app/Models/Module/Module.php +++ b/app/Models/Module/Module.php @@ -60,14 +60,20 @@ public function remote($func, $requests) // post, get, patch, delete 等请求 public function remoteRequest($method, $path, $requests) { + $user = auth('api')->user(); + $http = Http::remote($this->api_token, $this->url) ->accept('application/json'); + // add Headers + $http->withHeaders([ + 'X-User' => $user->id + ]); + unset($requests['func']); - $requests['user_id'] = auth('api')->id(); + $requests['user_id'] = $user->id; - $user = auth('api')->user(); if ($method == 'post') { // add user to requests @@ -91,12 +97,16 @@ public function remoteRequest($method, $path, $requests) public function moduleRequest($method, $path, $requests) { + $module_id = auth('remote')->id(); + $http = Http::remote($this->api_token, $this->url) ->accept('application/json'); - unset($requests['func']); + $http->withHeaders([ + 'X-Module' => $module_id + ]); - $requests['module_id'] = auth('module')->id(); + $requests['module_id'] = $module_id; $response = $http->{$method}("exports/{$path}", $requests); diff --git a/routes/remote.php b/routes/remote.php index 07f7e66..6964944 100644 --- a/routes/remote.php +++ b/routes/remote.php @@ -78,7 +78,7 @@ // 模块间调用 $router->group(['prefix' => 'modules/{module}'], function () use ($router) { - $controller = 'Remote\ModuleController@exportCall'; + $controller = 'ModuleController@exportCall'; $router->get('/{route:.*}/', $controller); $router->post('/{route:.*}/', $controller); $router->put('/{route:.*}/', $controller); @@ -91,3 +91,7 @@ $router->get('users/{user}', [ 'uses' => '\App\Http\Controllers\Remote\UserController@show' ]); + +$router->get('users/{user}/hosts', [ + 'uses' => '\App\Http\Controllers\Remote\UserController@hosts' +]);