From c224b2c7b5b82010776061e4763d827371016a4d Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Sat, 11 Feb 2023 02:18:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20=E6=89=A3=E8=B4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Module/HostController.php | 23 ++++++++++--------- app/Models/Host.php | 8 ++++--- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/Module/HostController.php b/app/Http/Controllers/Module/HostController.php index ce13d79..c0f6a74 100644 --- a/app/Http/Controllers/Module/HostController.php +++ b/app/Http/Controllers/Module/HostController.php @@ -94,22 +94,11 @@ public function show(Host $host): JsonResponse */ public function update(Request $request, Host $host): JsonResponse { - // $this->validate($request, [ 'status' => 'sometimes|in:running,stopped,error,suspended,pending', 'managed_price' => 'sometimes|numeric|nullable', - - // 如果是立即扣费 - 'cost_once' => 'sometimes|numeric|nullable', ]); - // if it has cost_once - if ($request->has('cost_once')) { - $host->cost($request->cost_once ?? 0, false); - - return $this->updated(); - } - $update = $request->except(['module_id', 'user_id']); $host->update($update); @@ -134,4 +123,16 @@ public function destroy($host): JsonResponse return $this->deleted(); } + + public function cost(Request $request, Host $host): JsonResponse + { + $this->validate($request, [ + 'amount' => 'required|numeric|min:0.01', + 'description' => 'nullable|string:max:255', + ]); + + $host->cost($request->input('amount'), false); + + return $this->noContent(); + } } diff --git a/app/Models/Host.php b/app/Models/Host.php index cced7d2..e34934e 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -169,7 +169,7 @@ public function safeDelete(): bool return true; } - public function cost(string $amount = null, $auto = true): bool + public function cost(string $amount = null, $auto = true, $description = null): bool { $this->load('user'); $user = $this->user; @@ -209,7 +209,7 @@ public function cost(string $amount = null, $auto = true): bool } if ($real_price == 0) { - echo '价格为 0,不扣费'; + echo '价格为 0,不扣费'.PHP_EOL; return true; } @@ -237,7 +237,9 @@ public function cost(string $amount = null, $auto = true): bool Cache::put($month_cache_key, $hosts_balances, 604800); - $description = '模块发起的扣费。'; + if (! $description) { + $description = '模块发起的扣费。'; + } if ($auto) { $description = '自动扣费。';