From 38a4d2d3e021783d1decbbfaa59d696a43a850ef Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Tue, 14 Feb 2023 13:32:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E4=B8=BB=E6=9C=BA=E7=9A=84=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Module/HostController.php | 37 ++++++++++++++++--- app/Models/Host.php | 19 ---------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/app/Http/Controllers/Module/HostController.php b/app/Http/Controllers/Module/HostController.php index 3e587c0..4a90c59 100644 --- a/app/Http/Controllers/Module/HostController.php +++ b/app/Http/Controllers/Module/HostController.php @@ -5,13 +5,13 @@ use App\Http\Controllers\Controller; use App\Models\Host; use App\Models\User; -use function auth; use Illuminate\Contracts\Pagination\Paginator; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Str; use Illuminate\Validation\ValidationException; +use function auth; // use App\Models\User; @@ -28,7 +28,8 @@ public function index(Request $request): Paginator /** * Store a newly created resource in storage. * - * @param Request $request + * @param Request $request + * * @return Response|JsonResponse * * @throws ValidationException @@ -74,7 +75,8 @@ public function store(Request $request): Response|JsonResponse /** * Display the specified resource. * - * @param Host $host + * @param Host $host + * * @return JsonResponse */ public function show(Host $host): JsonResponse @@ -88,8 +90,9 @@ public function show(Host $host): JsonResponse /** * Update the specified resource in storage. * - * @param Request $request - * @param Host $host + * @param Request $request + * @param Host $host + * * @return JsonResponse * * @throws ValidationException @@ -112,15 +115,37 @@ public function update(Request $request, Host $host): JsonResponse * Remove the specified resource from storage. * * @param $host + * * @return JsonResponse */ public function destroy($host): JsonResponse { // if host not instance of HostJob - if (! $host instanceof Host) { + if (!$host instanceof Host) { $host = (new Host)->findOrFail($host); } + if ($host?->isCycle()) { + $days = $host->next_due_at->diffInDays(now()); + + // 算出 1 天的价格 + $price = bcdiv($host->getPrice(), 31, 4); + + // 算出退还的金额 + $amount = bcmul($price, $days, 4); + + $host->user->charge($amount, 'balance', '删除主机退款。', [ + 'module_id' => $this->module_id, + 'host_id' => $this->id, + 'user_id' => $this->user_id, + ]); + + $host->module->reduce($amount, '删除主机退款。', false, [ + 'module_id' => $this->module_id, + 'host_id' => $this->id, + ]); + } + $host?->delete(); return $this->deleted(); diff --git a/app/Models/Host.php b/app/Models/Host.php index 9803170..c8c80b7 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -281,25 +281,6 @@ public function safeDelete(): bool if ($this->next_due_at === null) { $this->next_due_at = now(); } - - $days = $this->next_due_at->diffInDays(now()); - - // 算出 1 天的价格 - $price = bcdiv($this->getPrice(), 31, 4); - - // 算出退还的金额 - $amount = bcmul($price, $days, 4); - - $this->user->charge($amount, 'balance', '删除主机退款。', [ - 'module_id' => $this->module_id, - 'host_id' => $this->id, - 'user_id' => $this->user_id, - ]); - - $this->module->reduce($amount, '删除主机退款。', false, [ - 'module_id' => $this->module_id, - 'host_id' => $this->id, - ]); } // 如果创建时间大于 1 小时