2022-08-16 10:44:16 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Remote\Host;
|
|
|
|
|
|
2022-08-29 09:31:08 +00:00
|
|
|
|
use App\Models\Host;
|
|
|
|
|
use Illuminate\Support\Str;
|
2022-08-16 10:44:16 +00:00
|
|
|
|
use Illuminate\Http\Request;
|
2022-08-29 09:31:08 +00:00
|
|
|
|
use App\Http\Controllers\Controller;
|
2022-08-16 10:44:16 +00:00
|
|
|
|
|
|
|
|
|
class HostController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Display a listing of the resource.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
|
|
|
|
//
|
2022-08-28 17:17:57 +00:00
|
|
|
|
// Host::all();
|
2022-08-16 10:44:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
|
|
|
|
public function store(Request $request)
|
|
|
|
|
{
|
2022-08-28 17:17:57 +00:00
|
|
|
|
// 存储计费项目
|
2022-09-08 16:12:02 +00:00
|
|
|
|
$this->validate($request, [
|
2022-08-29 09:31:08 +00:00
|
|
|
|
'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);
|
2022-08-16 10:44:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Display the specified resource.
|
|
|
|
|
*
|
2022-08-23 09:36:10 +00:00
|
|
|
|
* @param Host $host
|
2022-08-16 10:44:16 +00:00
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2022-08-23 09:36:10 +00:00
|
|
|
|
public function show(Host $host)
|
2022-08-16 10:44:16 +00:00
|
|
|
|
{
|
2022-08-23 09:36:10 +00:00
|
|
|
|
|
|
|
|
|
return $this->success($host);
|
2022-08-16 10:44:16 +00:00
|
|
|
|
//
|
2022-08-23 09:36:10 +00:00
|
|
|
|
|
|
|
|
|
// dd($host->cost());
|
2022-08-16 10:44:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
2022-08-23 09:36:10 +00:00
|
|
|
|
* @param Host $host
|
2022-08-16 10:44:16 +00:00
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2022-08-23 09:36:10 +00:00
|
|
|
|
public function update(Request $request, Host $host)
|
2022-08-16 10:44:16 +00:00
|
|
|
|
{
|
|
|
|
|
//
|
2022-09-08 16:12:02 +00:00
|
|
|
|
$this->validate($request, [
|
2022-08-29 09:31:08 +00:00
|
|
|
|
'status' => 'sometimes|in:running,stopped,error,suspended,pending',
|
|
|
|
|
// 'managed_price' => 'sometimes|numeric|nullable',
|
2022-08-23 09:36:10 +00:00
|
|
|
|
|
|
|
|
|
// 如果是立即扣费
|
2022-08-29 10:59:32 +00:00
|
|
|
|
'cost_once' => 'sometimes|numeric|nullable',
|
2022-10-07 05:15:08 +00:00
|
|
|
|
|
|
|
|
|
'cost_balance' => 'sometimes|numeric|nullable',
|
2022-08-23 09:36:10 +00:00
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// if has cost_once
|
|
|
|
|
if ($request->has('cost_once')) {
|
2022-10-07 05:15:08 +00:00
|
|
|
|
$host->cost($request->cost_once ?? 0, false);
|
|
|
|
|
|
|
|
|
|
return $this->updated();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($request->has('cost_balance')) {
|
|
|
|
|
$host->costBalance($request->cost_balance ?? 0);
|
2022-08-23 09:36:10 +00:00
|
|
|
|
|
2022-10-07 05:15:08 +00:00
|
|
|
|
return $this->updated();
|
2022-08-23 09:36:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-07 05:15:08 +00:00
|
|
|
|
|
2022-10-08 04:24:04 +00:00
|
|
|
|
$update = $request->except(['module_id', 'user_id']);
|
2022-08-29 10:59:32 +00:00
|
|
|
|
|
|
|
|
|
$host->update($update);
|
2022-08-23 09:36:10 +00:00
|
|
|
|
|
|
|
|
|
return $this->updated($host);
|
2022-08-16 10:44:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
|
*
|
2022-10-16 09:25:04 +00:00
|
|
|
|
* @param $host
|
2022-08-16 10:44:16 +00:00
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
|
*/
|
2022-10-16 09:25:04 +00:00
|
|
|
|
public function destroy($host)
|
2022-08-16 10:44:16 +00:00
|
|
|
|
{
|
2022-10-16 09:25:04 +00:00
|
|
|
|
|
|
|
|
|
$host = Host::where('id', $host)->first($host);
|
|
|
|
|
|
|
|
|
|
if ($host) {
|
|
|
|
|
$host->delete();
|
|
|
|
|
}
|
2022-08-23 09:36:10 +00:00
|
|
|
|
|
|
|
|
|
return $this->deleted($host);
|
2022-08-16 10:44:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|