改进 远程调用方式
This commit is contained in:
parent
9b0f050828
commit
3b1e919d4b
@ -2,10 +2,11 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Remote\Host;
|
namespace App\Http\Controllers\Remote\Host;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller;
|
|
||||||
use App\Models\Host;
|
|
||||||
use Cache;
|
use Cache;
|
||||||
|
use App\Models\Host;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
|
||||||
class HostController extends Controller
|
class HostController extends Controller
|
||||||
{
|
{
|
||||||
@ -29,6 +30,28 @@ public function index()
|
|||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
// 存储计费项目
|
// 存储计费项目
|
||||||
|
$request->validate([
|
||||||
|
'status' => 'required|in:running,stopped,error,suspended,pending',
|
||||||
|
'price' => 'required|numeric',
|
||||||
|
'user_id' => 'required|integer|exists:users,id',
|
||||||
|
]);
|
||||||
|
|
||||||
|
// 如果没有 name,则随机
|
||||||
|
$name = $request->input('name', Str::random(10));
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'name' => $name,
|
||||||
|
'status' => $request->status,
|
||||||
|
'price' => $request->price,
|
||||||
|
'user_id' => $request->user_id,
|
||||||
|
'module_id' => auth('remote')->id()
|
||||||
|
];
|
||||||
|
|
||||||
|
$host = Host::create($data);
|
||||||
|
|
||||||
|
$host['host_id'] = $host->id;
|
||||||
|
|
||||||
|
return $this->created($host);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,8 +81,8 @@ public function update(Request $request, Host $host)
|
|||||||
{
|
{
|
||||||
//
|
//
|
||||||
$request->validate([
|
$request->validate([
|
||||||
'status' => 'sometimes|in:stopped,running,suspended,error',
|
'status' => 'sometimes|in:running,stopped,error,suspended,pending',
|
||||||
'managed_price' => 'sometimes|numeric|nullable',
|
// 'managed_price' => 'sometimes|numeric|nullable',
|
||||||
|
|
||||||
// 如果是立即扣费
|
// 如果是立即扣费
|
||||||
'cost_once' => 'sometimes|boolean|nullable',
|
'cost_once' => 'sometimes|boolean|nullable',
|
||||||
|
@ -27,8 +27,22 @@ public function call(Request $request, Module $module)
|
|||||||
$func = substr($func, 1);
|
$func = substr($func, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = $module->remote($func, $request->all());
|
// 过滤除了 "/" 以外的特殊字符
|
||||||
|
$func = preg_replace('/[^a-zA-Z0-9\/]/', '', $func);
|
||||||
|
|
||||||
return $this->apiResponse($response[0], $response[1]);
|
|
||||||
|
|
||||||
|
// dd($func);
|
||||||
|
|
||||||
|
$method = Str::lower($request->method());
|
||||||
|
|
||||||
|
|
||||||
|
$response = $module->remoteRequest($method, $func, $request->all());
|
||||||
|
|
||||||
|
if ($response['json'] === null && $response['body'] !== null) {
|
||||||
|
return response($response['body'], $response['status']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->apiResponse($response['json'], $response['status']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,34 @@ public function remote($func, $requests)
|
|||||||
return [$json, $status];
|
return [$json, $status];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// post, get, patch, delete 等请求
|
||||||
|
public function remoteRequest($method, $func, $requests)
|
||||||
|
{
|
||||||
|
$http = Http::remote($this->api_token, $this->url)
|
||||||
|
->accept('application/json')
|
||||||
|
->withHeaders(['X-Func' => $func]);
|
||||||
|
|
||||||
|
|
||||||
|
unset($requests['func']);
|
||||||
|
|
||||||
|
$requests['user_id'] = auth('sanctum')->id();
|
||||||
|
|
||||||
|
$response = $http->{$method}("functions/{$func}", $requests);
|
||||||
|
|
||||||
|
$json = $response->json();
|
||||||
|
|
||||||
|
$status = $response->status();
|
||||||
|
|
||||||
|
return [
|
||||||
|
'body' => $response->body(),
|
||||||
|
'json' => $json,
|
||||||
|
'status' => $status
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function remotePost($path = '', $data = [])
|
public function remotePost($path = '', $data = [])
|
||||||
{
|
{
|
||||||
$http = Http::remote($this->api_token, $this->url);
|
$http = Http::remote($this->api_token, $this->url);
|
||||||
|
@ -30,6 +30,7 @@ public function boot()
|
|||||||
// 关闭证书验证
|
// 关闭证书验证
|
||||||
return Http::withoutVerifying()->withHeaders([
|
return Http::withoutVerifying()->withHeaders([
|
||||||
'X-Remote-Api-Token' => $api_token,
|
'X-Remote-Api-Token' => $api_token,
|
||||||
|
'Content-Type' => 'application/json'
|
||||||
])->baseUrl($url);
|
])->baseUrl($url);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -19,7 +19,6 @@
|
|||||||
Route::apiResource('work-orders.replies', ReplyController::class);
|
Route::apiResource('work-orders.replies', ReplyController::class);
|
||||||
|
|
||||||
// 调用远程 API
|
// 调用远程 API
|
||||||
// Route::post('hosts/{host}/func/{func}', [Remote\CallController::class, 'host'])->name('host.call');
|
Route::any('/modules/{module}', [ModuleController::class, 'call'])->name('module.call');
|
||||||
Route::post('/modules/{module}', [ModuleController::class, 'call'])->name('module.call');
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user