改进 扣费

This commit is contained in:
iVampireSP.com 2023-02-11 02:18:34 +08:00
parent c7a0484c48
commit c224b2c7b5
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
2 changed files with 17 additions and 14 deletions

View File

@ -94,22 +94,11 @@ public function show(Host $host): JsonResponse
*/ */
public function update(Request $request, Host $host): JsonResponse public function update(Request $request, Host $host): JsonResponse
{ {
//
$this->validate($request, [ $this->validate($request, [
'status' => 'sometimes|in:running,stopped,error,suspended,pending', 'status' => 'sometimes|in:running,stopped,error,suspended,pending',
'managed_price' => 'sometimes|numeric|nullable', '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']); $update = $request->except(['module_id', 'user_id']);
$host->update($update); $host->update($update);
@ -134,4 +123,16 @@ public function destroy($host): JsonResponse
return $this->deleted(); 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();
}
} }

View File

@ -169,7 +169,7 @@ public function safeDelete(): bool
return true; 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'); $this->load('user');
$user = $this->user; $user = $this->user;
@ -209,7 +209,7 @@ public function cost(string $amount = null, $auto = true): bool
} }
if ($real_price == 0) { if ($real_price == 0) {
echo '价格为 0不扣费'; echo '价格为 0不扣费'.PHP_EOL;
return true; return true;
} }
@ -237,7 +237,9 @@ public function cost(string $amount = null, $auto = true): bool
Cache::put($month_cache_key, $hosts_balances, 604800); Cache::put($month_cache_key, $hosts_balances, 604800);
if (! $description) {
$description = '模块发起的扣费。'; $description = '模块发起的扣费。';
}
if ($auto) { if ($auto) {
$description = '自动扣费。'; $description = '自动扣费。';