2022-08-19 15:27:57 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-06 11:28:22 +00:00
|
|
|
namespace App\Http\Controllers\Api;
|
2022-08-19 15:27:57 +00:00
|
|
|
|
2022-11-06 11:28:22 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
2022-11-25 08:14:42 +00:00
|
|
|
use App\Http\Requests\User\HostRequest;
|
2023-01-13 14:13:46 +00:00
|
|
|
use App\Jobs\Host\HostJob;
|
2022-08-19 15:27:57 +00:00
|
|
|
use App\Models\Host;
|
2022-11-06 11:28:22 +00:00
|
|
|
use function auth;
|
|
|
|
use function dispatch;
|
2023-01-30 16:14:07 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
2022-11-06 11:28:22 +00:00
|
|
|
use function now;
|
2022-08-19 15:27:57 +00:00
|
|
|
|
|
|
|
class HostController extends Controller
|
|
|
|
{
|
2022-11-06 11:28:22 +00:00
|
|
|
public function index(): JsonResponse
|
2022-08-19 15:27:57 +00:00
|
|
|
{
|
2023-01-10 13:42:27 +00:00
|
|
|
$hosts = (new Host)->where('user_id', auth()->id())->with('module', function ($query) {
|
2022-11-06 11:28:22 +00:00
|
|
|
$query->select(['id', 'name']);
|
|
|
|
})->get();
|
2022-08-19 15:27:57 +00:00
|
|
|
|
|
|
|
return $this->success($hosts);
|
|
|
|
}
|
|
|
|
|
2022-11-23 09:05:54 +00:00
|
|
|
//
|
2022-11-25 08:14:42 +00:00
|
|
|
public function update(HostRequest $request, Host $host): JsonResponse
|
2022-09-05 05:36:46 +00:00
|
|
|
{
|
2022-11-23 09:05:54 +00:00
|
|
|
$request->validate([
|
|
|
|
'status' => 'required|in:running,stopped',
|
|
|
|
]);
|
|
|
|
|
2023-01-20 08:05:12 +00:00
|
|
|
if ($host->status === 'locked' || $host->status === 'unavailable') {
|
|
|
|
return $this->error('当前主机状态不允许操作');
|
|
|
|
}
|
|
|
|
|
2022-09-05 05:36:46 +00:00
|
|
|
$user = $request->user();
|
2022-11-23 09:06:50 +00:00
|
|
|
|
2022-12-18 03:16:27 +00:00
|
|
|
if ($user->balance < 0.5) {
|
2023-01-30 16:14:07 +00:00
|
|
|
return $this->error('余额不足,无法开启计费项目。请确保您的余额至少为 0.5 元,您当前有 '.$user->balance.' 元。');
|
2022-11-23 09:06:50 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 08:14:42 +00:00
|
|
|
$host->update([
|
2023-01-10 13:42:27 +00:00
|
|
|
'status' => $request->input('status'),
|
2022-11-25 08:14:42 +00:00
|
|
|
]);
|
2022-09-05 05:36:46 +00:00
|
|
|
|
2022-11-25 08:14:42 +00:00
|
|
|
return $this->updated($host);
|
2022-09-05 05:36:46 +00:00
|
|
|
}
|
|
|
|
|
2022-11-27 13:04:20 +00:00
|
|
|
public function destroy(HostRequest $request, Host $host): JsonResponse
|
2022-09-03 06:01:51 +00:00
|
|
|
{
|
2022-11-27 13:04:20 +00:00
|
|
|
unset($request);
|
|
|
|
|
2022-11-25 08:14:42 +00:00
|
|
|
if ($host->status == 'pending') {
|
|
|
|
// 如果上次更新时间大于 5min
|
2022-12-04 06:17:12 +00:00
|
|
|
if ($host->updated_at->diffInMinutes(now()) > 5) {
|
2022-11-25 08:14:42 +00:00
|
|
|
$host->delete();
|
|
|
|
} else {
|
|
|
|
return $this->error('请等待 5 分钟后再试');
|
2022-09-09 09:20:47 +00:00
|
|
|
}
|
2022-09-03 06:01:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:17:12 +00:00
|
|
|
// 如果时间大于 5 分钟,不满 1 小时
|
|
|
|
if (now()->diffInMinutes($host->updated_at) > 5 && now()->diffInMinutes($host->updated_at) < 60) {
|
|
|
|
$host->cost();
|
|
|
|
}
|
|
|
|
|
2022-12-28 13:19:40 +00:00
|
|
|
dispatch(new HostJob($host, 'delete'));
|
2022-11-25 08:14:42 +00:00
|
|
|
|
2022-09-03 06:01:51 +00:00
|
|
|
return $this->deleted($host);
|
|
|
|
}
|
|
|
|
|
2022-11-06 11:28:22 +00:00
|
|
|
public function usages(): JsonResponse
|
2022-09-09 13:42:12 +00:00
|
|
|
{
|
|
|
|
$month = now()->month;
|
|
|
|
|
2023-01-30 16:14:07 +00:00
|
|
|
$month_cache_key = 'user_'.auth()->id().'_month_'.$month.'_hosts_balances';
|
2022-10-09 06:53:25 +00:00
|
|
|
$hosts_balances = Cache::get($month_cache_key, []);
|
|
|
|
|
|
|
|
return $this->success([
|
2023-01-30 16:14:07 +00:00
|
|
|
'balances' => $hosts_balances,
|
2022-10-09 06:53:25 +00:00
|
|
|
]);
|
2022-09-09 13:42:12 +00:00
|
|
|
}
|
2022-08-19 15:27:57 +00:00
|
|
|
}
|