diff --git a/app/Http/Controllers/Api/HostController.php b/app/Http/Controllers/Api/HostController.php index 2b968e0..4376651 100644 --- a/app/Http/Controllers/Api/HostController.php +++ b/app/Http/Controllers/Api/HostController.php @@ -61,7 +61,7 @@ public function destroy(HostRequest $request, Host $host): JsonResponse } // 如果时间大于 5 分钟,不满 1 小时 - if (now()->diffInMinutes($host->updated_at) > 5 && now()->diffInMinutes($host->updated_at) < 60) { + if (! $host->isCycle() && now()->diffInMinutes($host->updated_at) > 5 && now()->diffInMinutes($host->updated_at) < 60) { $host->cost(); } diff --git a/app/Http/Controllers/Web/HostController.php b/app/Http/Controllers/Web/HostController.php new file mode 100644 index 0000000..d116418 --- /dev/null +++ b/app/Http/Controllers/Web/HostController.php @@ -0,0 +1,45 @@ +with(['user', 'module'])->paginate(20); + + return view('hosts.index', compact('hosts')); + } + + public function renew(Host $host) + { + if ($host->renew()) { + return back()->with('success', '续费成功,新的到期时间为:'.$host->next_due_at); + } + + return back()->with('error', '续费失败,请检查是否有足够的余额。'); + } + + /** + * Remove the specified resource from storage. + * + * @param Host $host + * @return RedirectResponse + */ + public function destroy(Host $host): RedirectResponse + { + $host->safeDelete(); + + return back()->with('success', '已添加到销毁队列。'); + } +} diff --git a/app/Jobs/Host/DispatchHostCostQueueJob.php b/app/Jobs/Host/DispatchHostCostQueueJob.php index b0858c4..04bfc63 100644 --- a/app/Jobs/Host/DispatchHostCostQueueJob.php +++ b/app/Jobs/Host/DispatchHostCostQueueJob.php @@ -13,6 +13,7 @@ class DispatchHostCostQueueJob implements ShouldQueue use InteractsWithQueue, Queueable, SerializesModels; protected int $minute; + protected ?Host $host; /** @@ -35,7 +36,7 @@ public function __construct($minute, Host $host = null) */ public function handle(): void { - if (!$this->host) { + if (! $this->host) { $host = new Host(); if (app()->environment() != 'local') { diff --git a/app/Jobs/Module/DispatchFetchModuleJob.php b/app/Jobs/Module/DispatchFetchModuleJob.php index d6b71c0..e30df9c 100644 --- a/app/Jobs/Module/DispatchFetchModuleJob.php +++ b/app/Jobs/Module/DispatchFetchModuleJob.php @@ -27,7 +27,6 @@ public function __construct(Module $module = null) $this->module = $module; } - /** * Execute the job. * @@ -35,11 +34,10 @@ public function __construct(Module $module = null) */ public function handle(): void { - if (!$this->module) { + if (! $this->module) { (new Module)->whereNotNull('url')->chunk(100, function ($modules) { foreach ($modules as $module) { dispatch(new self($module)); - } }); } @@ -52,7 +50,7 @@ public function handle(): void try { $response = $module->http()->get('remote'); } catch (Exception $e) { - Log::debug('无法连接到模块 - down: ' . $e->getMessage()); + Log::debug('无法连接到模块 - down: '.$e->getMessage()); // 如果模块状态不为 down,则更新为 down if ($module->status !== 'down') { @@ -67,7 +65,7 @@ public function handle(): void // 如果模块状态不为 up,则更新为 up if ($module->status !== 'up') { $module->status = 'up'; - Log::debug('模块状态更新为 up: ' . $module->name); + Log::debug('模块状态更新为 up: '.$module->name); } $json = $response->json(); @@ -99,16 +97,16 @@ public function handle(): void $module->status = 'down'; } - Log::debug('模块状态更新为 ' . $module->status . ': ' . $module->name); + Log::debug('模块状态更新为 '.$module->status.': '.$module->name); } $module->save(); // if local if (config('app.env') === 'local') { - Cache::forever('module:' . $module->id . ':servers', $servers); + Cache::forever('module:'.$module->id.':servers', $servers); } else { - Cache::put('module:' . $module->id . ':servers', $servers, now()->addMinutes(10)); + Cache::put('module:'.$module->id.':servers', $servers, now()->addMinutes(10)); } } } diff --git a/app/Models/Host.php b/app/Models/Host.php index 7ce5add..60c2687 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -180,7 +180,7 @@ public function isCycle(): bool public function safeDelete(): bool { // 如果创建时间大于大于 1 小时 - if ($this->created_at->diffInHours(now()) > 1) { + if (! $this->isCycle() && $this->created_at->diffInHours(now()) > 1) { // 如果当前时间比扣费时间小,则说明没有扣费。执行扣费。 if (now()->minute < $this->minute_at) { $this->cost(); diff --git a/app/View/Components/BillingCycle.php b/app/View/Components/BillingCycle.php index 1a566d7..9aa0ea5 100644 --- a/app/View/Components/BillingCycle.php +++ b/app/View/Components/BillingCycle.php @@ -25,6 +25,6 @@ public function __construct(null|string $cycle = 'dynamic') */ public function render(): string { - return trans('hosts.' . $this->cycle); + return trans('hosts.'.$this->cycle); } }