改进 远程调用方式
This commit is contained in:
parent
9b0f050828
commit
3b1e919d4b
@ -2,10 +2,11 @@
|
||||
|
||||
namespace App\Http\Controllers\Remote\Host;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Host;
|
||||
use Cache;
|
||||
use App\Models\Host;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
class HostController extends Controller
|
||||
{
|
||||
@ -29,7 +30,29 @@ public function index()
|
||||
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([
|
||||
'status' => 'sometimes|in:stopped,running,suspended,error',
|
||||
'managed_price' => 'sometimes|numeric|nullable',
|
||||
'status' => 'sometimes|in:running,stopped,error,suspended,pending',
|
||||
// 'managed_price' => 'sometimes|numeric|nullable',
|
||||
|
||||
// 如果是立即扣费
|
||||
'cost_once' => 'sometimes|boolean|nullable',
|
||||
|
@ -27,8 +27,22 @@ public function call(Request $request, Module $module)
|
||||
$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];
|
||||
}
|
||||
|
||||
|
||||
// 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 = [])
|
||||
{
|
||||
$http = Http::remote($this->api_token, $this->url);
|
||||
|
@ -30,6 +30,7 @@ public function boot()
|
||||
// 关闭证书验证
|
||||
return Http::withoutVerifying()->withHeaders([
|
||||
'X-Remote-Api-Token' => $api_token,
|
||||
'Content-Type' => 'application/json'
|
||||
])->baseUrl($url);
|
||||
});
|
||||
|
||||
|
@ -19,7 +19,6 @@
|
||||
Route::apiResource('work-orders.replies', ReplyController::class);
|
||||
|
||||
// 调用远程 API
|
||||
// Route::post('hosts/{host}/func/{func}', [Remote\CallController::class, 'host'])->name('host.call');
|
||||
Route::post('/modules/{module}', [ModuleController::class, 'call'])->name('module.call');
|
||||
Route::any('/modules/{module}', [ModuleController::class, 'call'])->name('module.call');
|
||||
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user