2022-08-19 15:27:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\User;
|
|
|
|
|
|
|
|
use App\Models\Host;
|
|
|
|
use Illuminate\Http\Request;
|
2022-09-28 05:13:24 +00:00
|
|
|
// use App\Models\Module\Module;
|
2022-08-19 15:27:57 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
2022-09-09 13:42:12 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2022-08-19 15:27:57 +00:00
|
|
|
|
|
|
|
class HostController extends Controller
|
|
|
|
{
|
2022-09-19 13:24:14 +00:00
|
|
|
public function index()
|
2022-08-19 15:27:57 +00:00
|
|
|
{
|
|
|
|
//
|
2022-09-19 13:24:14 +00:00
|
|
|
$hosts = (new Host())->getUserHosts(auth()->id());
|
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') {
|
2022-09-09 09:28:43 +00:00
|
|
|
|
|
|
|
// 如果上次更新时间大于 5min
|
|
|
|
if (time() - strtotime($host->updated_at) > 300) {
|
|
|
|
$host->delete();
|
|
|
|
} else {
|
|
|
|
return $this->error('请等待 5 分钟后再试');
|
|
|
|
}
|
2022-09-09 09:20:47 +00:00
|
|
|
}
|
|
|
|
|
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-09 13:42:12 +00:00
|
|
|
public function usages()
|
|
|
|
{
|
|
|
|
$month = now()->month;
|
|
|
|
|
2022-09-09 17:32:52 +00:00
|
|
|
$month_cache_key = 'user_' . auth()->id() . '_month_' . $month . '_hosts_drops';
|
2022-09-09 13:42:12 +00:00
|
|
|
$hosts_drops = Cache::get($month_cache_key, []);
|
|
|
|
|
|
|
|
return $this->success($hosts_drops);
|
|
|
|
}
|
2022-08-19 15:27:57 +00:00
|
|
|
}
|