From 821dcc90423e7a62e40d4a62ec3750ee8e17d02e Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Wed, 16 Nov 2022 21:02:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E9=87=91=E9=A2=9D=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/Transaction.php | 42 ++++++++++++----------- resources/views/transfer/search.blade.php | 2 +- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index dd0da7c..ee6b413 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -47,6 +47,7 @@ class Transaction extends Model // 可用余额 'balances', + 'balance', // 可用 Drops 'drops', @@ -110,8 +111,9 @@ private function addLog($user_id, $data) { $user = User::find($user_id); + $current = [ - 'balance' => $user->balance, + 'balance' => (float) $user->balance, 'drops' => $this->getDrops($user_id), 'user_id' => intval($user_id), ]; @@ -122,7 +124,6 @@ private function addLog($user_id, $data) // add expired at $data['expired_at'] = now()->addSeconds(7); - return $this->create($data); } @@ -160,11 +161,11 @@ public function reduceDrops($user_id, $host_id, $module_id, $auto = 1, $amount = Cache::forever($cache_key, $current_drops); - if ($auto) { - $description = '平台按时间自动扣费。'; - } else { - $description = '集成模块发起的扣费。'; - } + // if ($auto) { + // $description = '平台按时间自动扣费。'; + // } else { + // $description = '集成模块发起的扣费。'; + // } // $this->addPayoutDrops($user_id, $amount, $description, $host_id, $module_id); } @@ -183,11 +184,11 @@ public function reduceAmount($user_id, $amount = 0, $description = '扣除费用 $user->save(); $this->addPayoutBalance($user_id, $amount, $description); - - return $user->balance; } finally { optional($lock)->release(); } + + return $user->balance; } public function addPayoutBalance($user_id, $amount, $description, $module_id = null) @@ -229,11 +230,11 @@ public function reduceAmountModuleFail($user_id, $module_id, $amount = 0, $descr $user->save(); $this->addPayoutBalance($user_id, $amount, $description, $module_id); - - return $user->balance; } finally { optional($lock)->release(); } + + return $user->balance; } public function reduceHostAmount($user_id, $host_id, $module_id, $amount = 0, $description = '扣除费用请求。') @@ -250,11 +251,11 @@ public function reduceHostAmount($user_id, $host_id, $module_id, $amount = 0, $d $user->save(); $this->addHostPayoutBalance($user_id, $host_id, $module_id, $amount, $description); - - return $user->balance; } finally { optional($lock)->release(); } + + return $user->balance; } public function addHostPayoutBalance($user_id, $host_id, $module_id, $amount, $description) @@ -297,14 +298,14 @@ public function addAmount($user_id, $payment = 'console', $amount = 0, $descript } $this->addIncomeBalance($user_id, $payment, $amount, $description); - - return $left_balance; } catch (LockTimeoutException $e) { Log::error($e); throw new ChargeException('充值失败,请稍后再试。'); } finally { optional($lock)->release(); } + + return $left_balance; } public function addIncomeBalance($user_id, $payment, $amount, $description) @@ -322,7 +323,7 @@ public function addIncomeBalance($user_id, $payment, $amount, $description) return $this->addLog($user_id, $data); } - public function transfer(User $user, User $to, float $amount, string $description): float + public function transfer(User $user, User $to, float $amount, string|null $description): float { $lock = Cache::lock("user_balance_lock_" . $user->id, 10); $lock_to = Cache::lock("user_balance_lock_" . $to->id, 10); @@ -337,6 +338,10 @@ public function transfer(User $user, User $to, float $amount, string $descriptio $to->balance += $amount; $to->save(); + if (!$description) { + $description = '完成。'; + } + $description_new = "转账给 {$to->name}({$to->email}) {$amount} 元,{$description}"; $this->addPayoutBalance($user->id, $amount, $description_new); @@ -344,13 +349,12 @@ public function transfer(User $user, User $to, float $amount, string $descriptio $description_new = "收到来自 {$user->name}($user->email) 转来的 {$amount} 元, $description"; $this->addIncomeBalance($to->id, 'transfer', $amount, $description_new); - - return $user->balance; } finally { optional($lock)->release(); optional($lock_to)->release(); } + return $user->balance; } public function transferDrops(User $user, User $to, float $amount, string|null $description = null): bool @@ -373,8 +377,6 @@ public function transferDrops(User $user, User $to, float $amount, string|null $ $this->increaseDrops($to->id, $amount, $description_new, 'transfer'); return true; - - } public function reduceDropsWithoutHost($user_id, $amount = 0, $description = null) diff --git a/resources/views/transfer/search.blade.php b/resources/views/transfer/search.blade.php index fb2df56..3640dd9 100644 --- a/resources/views/transfer/search.blade.php +++ b/resources/views/transfer/search.blade.php @@ -39,7 +39,7 @@