2022-08-19 15:27:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\User;
|
|
|
|
|
|
|
|
use App\Models\Host;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Models\Module\Module;
|
|
|
|
use App\Http\Controllers\Controller;
|
2022-08-26 14:37:20 +00:00
|
|
|
use Illuminate\Support\Facades\Http;
|
2022-08-19 15:27:57 +00:00
|
|
|
|
|
|
|
class HostController extends Controller
|
|
|
|
{
|
2022-09-03 06:01:51 +00:00
|
|
|
public function index(Module $module)
|
2022-08-19 15:27:57 +00:00
|
|
|
{
|
|
|
|
//
|
2022-09-02 16:35:46 +00:00
|
|
|
$hosts = Host::thisUser($module->id)->with('module', function ($query) {
|
|
|
|
$query->select(['id', 'name']);
|
|
|
|
})->get();
|
2022-08-19 15:27:57 +00:00
|
|
|
|
|
|
|
return $this->success($hosts);
|
|
|
|
}
|
|
|
|
|
2022-09-05 05:36:46 +00:00
|
|
|
public function update(Request $request, Host $host)
|
|
|
|
{
|
|
|
|
$user = $request->user();
|
|
|
|
if ($host->user_id == $user->id) {
|
|
|
|
|
2022-09-05 06:26:36 +00:00
|
|
|
if ($user->balance < 1) {
|
2022-09-05 05:36:46 +00:00
|
|
|
return $this->error('余额不足');
|
|
|
|
}
|
|
|
|
|
2022-09-05 05:59:30 +00:00
|
|
|
$host->update([
|
|
|
|
'status' => 'running'
|
|
|
|
]);
|
2022-09-05 05:36:46 +00:00
|
|
|
|
|
|
|
return $this->updated($host);
|
|
|
|
} else {
|
|
|
|
return $this->error('无权操作');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->deleted($host);
|
|
|
|
}
|
|
|
|
|
2022-09-03 06:01:51 +00:00
|
|
|
public function destroy(Host $host)
|
|
|
|
{
|
|
|
|
if ($host->user_id == auth()->id()) {
|
2022-09-09 09:20:47 +00:00
|
|
|
|
|
|
|
if ($host->status == 'pending') {
|
|
|
|
return $this->error('主机正在创建中,无法删除');
|
|
|
|
}
|
|
|
|
|
2022-09-03 06:01:51 +00:00
|
|
|
dispatch(new \App\Jobs\Remote\Host($host, 'delete'));
|
|
|
|
} else {
|
|
|
|
return $this->error('无权操作');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->deleted($host);
|
|
|
|
}
|
|
|
|
|
2022-09-02 16:35:46 +00:00
|
|
|
// /**
|
|
|
|
// * Store a newly created resource in storage.
|
|
|
|
// *
|
|
|
|
// * @param \Illuminate\Http\Request $request
|
|
|
|
// * @return \Illuminate\Http\Response
|
|
|
|
// */
|
|
|
|
// public function store(Request $request, Module $module)
|
|
|
|
// {
|
|
|
|
// // User create host
|
2022-09-08 16:12:02 +00:00
|
|
|
// $this->validate($request, [
|
2022-09-02 16:35:46 +00:00
|
|
|
// 'name' => 'required|max:255',
|
|
|
|
// 'configuration' => 'required|json',
|
|
|
|
// ]);
|
|
|
|
|
|
|
|
// $data = [
|
|
|
|
// 'name' => $request->name,
|
|
|
|
// 'module_id' => $module->id,
|
|
|
|
// 'configuration' => $request->configuration ?? [],
|
|
|
|
// ];
|
|
|
|
|
|
|
|
|
|
|
|
// // if (!$data['confirm']) {
|
|
|
|
// // $data['confirm'] = false;
|
|
|
|
|
|
|
|
// // }
|
|
|
|
|
|
|
|
// // $calc = $module->remotePost('/hosts', ['data' => $data]);
|
|
|
|
// // $data['price'] = $calc[0]['data']['price'];
|
|
|
|
|
|
|
|
// $host = Host::create($data);
|
|
|
|
// return $this->created($host);
|
|
|
|
|
|
|
|
// // if ($request->confirm) {
|
|
|
|
// // $host = Host::create($data);
|
|
|
|
// // return $this->created($host);
|
|
|
|
// // } else {
|
|
|
|
// // // return $this->apiResponse($calc[0]['data'], $calc[1]);
|
|
|
|
// // }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // // post to module
|
|
|
|
// // $host = $module->hosts()->create([
|
|
|
|
// // 'name' => $request->name,
|
|
|
|
// // 'configuration' => $request->configuration,
|
|
|
|
// // ]);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * Display the specified resource.
|
|
|
|
// *
|
|
|
|
// * @param int $id
|
|
|
|
// * @return \Illuminate\Http\Response
|
|
|
|
// */
|
|
|
|
// public function show()
|
|
|
|
// {
|
|
|
|
// }
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * Update the specified resource in storage.
|
|
|
|
// *
|
|
|
|
// * @param \Illuminate\Http\Request $request
|
|
|
|
// * @param int $id
|
|
|
|
// * @return \Illuminate\Http\Response
|
|
|
|
// */
|
|
|
|
// public function update(Request $request, $id)
|
|
|
|
// {
|
|
|
|
// //
|
|
|
|
// }
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * Remove the specified resource from storage.
|
|
|
|
// *
|
|
|
|
// * @param int $id
|
|
|
|
// * @return \Illuminate\Http\Response
|
|
|
|
// */
|
|
|
|
// public function destroy($id)
|
|
|
|
// {
|
|
|
|
// //
|
|
|
|
// }
|
2022-08-19 15:27:57 +00:00
|
|
|
}
|