146 lines
3.4 KiB
PHP
146 lines
3.4 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Remote\Host;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Models\Host;
|
||
use App\Models\User;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Str;
|
||
|
||
// use Illuminate\Support\Facades\Log;
|
||
|
||
class HostController extends Controller
|
||
{
|
||
/**
|
||
* Display a listing of the resource.
|
||
*
|
||
* @return \Illuminate\Http\Response
|
||
*/
|
||
public function index()
|
||
{
|
||
//
|
||
// Host::all();
|
||
}
|
||
|
||
/**
|
||
* Store a newly created resource in storage.
|
||
*
|
||
* @param \Illuminate\Http\Request $request
|
||
* @return \Illuminate\Http\Response
|
||
*/
|
||
public function store(Request $request)
|
||
{
|
||
// 存储计费项目
|
||
$this->validate($request, [
|
||
'status' => 'required|in:running,stopped,error,suspended,pending',
|
||
'price' => 'required|numeric',
|
||
'user_id' => 'required|integer|exists:users,id',
|
||
]);
|
||
|
||
//
|
||
$user = User::findOrFail($request->user_id);
|
||
|
||
if ($user->balance < 1) {
|
||
return $this->error('此用户余额不足,无法开设计费项目。');
|
||
}
|
||
|
||
// 如果没有 name,则随机
|
||
$name = $request->input('name', Str::random(10));
|
||
|
||
$data = [
|
||
'name' => $name,
|
||
'status' => $request->status,
|
||
'price' => $request->price,
|
||
'managed_price' => $request->managed_price ?? 0,
|
||
'user_id' => $user->id,
|
||
'module_id' => auth('module')->id()
|
||
];
|
||
|
||
$host = Host::create($data);
|
||
|
||
$host['host_id'] = $host->id;
|
||
|
||
return $this->created($host);
|
||
}
|
||
|
||
/**
|
||
* Display the specified resource.
|
||
*
|
||
* @param Host $host
|
||
* @return \Illuminate\Http\Response
|
||
*/
|
||
public function show(Host $host)
|
||
{
|
||
|
||
return $this->success($host);
|
||
//
|
||
|
||
// dd($host->cost());
|
||
}
|
||
|
||
/**
|
||
* Update the specified resource in storage.
|
||
*
|
||
* @param \Illuminate\Http\Request $request
|
||
* @param Host $host
|
||
* @return \Illuminate\Http\Response
|
||
*/
|
||
public function update(Request $request, Host $host)
|
||
{
|
||
//
|
||
$this->validate($request, [
|
||
'status' => 'sometimes|in:running,stopped,error,suspended,pending',
|
||
// 'managed_price' => 'sometimes|numeric|nullable',
|
||
|
||
// 如果是立即扣费
|
||
'cost_once' => 'sometimes|numeric|nullable',
|
||
|
||
'cost_balance' => 'sometimes|numeric|nullable',
|
||
]);
|
||
|
||
// if has cost_once
|
||
if ($request->has('cost_once')) {
|
||
$host->cost($request->cost_once ?? 0, false);
|
||
|
||
return $this->updated();
|
||
}
|
||
|
||
if ($request->has('cost_balance')) {
|
||
$host->costBalance($request->cost_balance ?? 0);
|
||
|
||
return $this->updated();
|
||
}
|
||
|
||
|
||
$update = $request->except(['module_id', 'user_id']);
|
||
|
||
$host->update($update);
|
||
|
||
return $this->updated($host);
|
||
}
|
||
|
||
/**
|
||
* Remove the specified resource from storage.
|
||
*
|
||
* @param $host
|
||
* @return \Illuminate\Http\Response
|
||
*/
|
||
public function destroy($host)
|
||
{
|
||
if ($host instanceof Host) {
|
||
$host->delete();
|
||
} else {
|
||
$host = Host::where('id', $host)->first($host);
|
||
|
||
if ($host) {
|
||
$host->delete();
|
||
}
|
||
}
|
||
|
||
|
||
|
||
return $this->deleted($host);
|
||
}
|
||
}
|