改进 计费周期

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 小时
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();
}

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;
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') {

View File

@ -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));
}
}
}

View File

@ -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();

View File

@ -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);
}
}