改进 计费周期

This commit is contained in:
iVampireSP.com 2023-02-13 02:27:59 +08:00
parent 3872b5a9bb
commit 04c2a75883
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
6 changed files with 56 additions and 12 deletions

View File

@ -61,7 +61,7 @@ public function destroy(HostRequest $request, Host $host): JsonResponse
} }
// 如果时间大于 5 分钟,不满 1 小时 // 如果时间大于 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(); $host->cost();
} }

View File

@ -0,0 +1,45 @@
<?php
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use App\Models\Host;
use Illuminate\Http\RedirectResponse;
use Illuminate\View\View;
class HostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return View
*/
public function index(): View
{
$hosts = Host::thisUser()->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', '已添加到销毁队列。');
}
}

View File

@ -13,6 +13,7 @@ class DispatchHostCostQueueJob implements ShouldQueue
use InteractsWithQueue, Queueable, SerializesModels; use InteractsWithQueue, Queueable, SerializesModels;
protected int $minute; protected int $minute;
protected ?Host $host; protected ?Host $host;
/** /**

View File

@ -27,7 +27,6 @@ public function __construct(Module $module = null)
$this->module = $module; $this->module = $module;
} }
/** /**
* Execute the job. * Execute the job.
* *
@ -39,7 +38,6 @@ public function handle(): void
(new Module)->whereNotNull('url')->chunk(100, function ($modules) { (new Module)->whereNotNull('url')->chunk(100, function ($modules) {
foreach ($modules as $module) { foreach ($modules as $module) {
dispatch(new self($module)); dispatch(new self($module));
} }
}); });
} }

View File

@ -180,7 +180,7 @@ public function isCycle(): bool
public function safeDelete(): bool public function safeDelete(): bool
{ {
// 如果创建时间大于大于 1 小时 // 如果创建时间大于大于 1 小时
if ($this->created_at->diffInHours(now()) > 1) { if (! $this->isCycle() && $this->created_at->diffInHours(now()) > 1) {
// 如果当前时间比扣费时间小,则说明没有扣费。执行扣费。 // 如果当前时间比扣费时间小,则说明没有扣费。执行扣费。
if (now()->minute < $this->minute_at) { if (now()->minute < $this->minute_at) {
$this->cost(); $this->cost();