改进 Remote 调用

This commit is contained in:
iVampireSP.com 2022-09-19 21:39:12 +08:00
parent aab01d912b
commit bc11f5605d
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
3 changed files with 21 additions and 7 deletions

View File

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

View File

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

View File

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