diff --git a/app/Http/Controllers/User/HostController.php b/app/Http/Controllers/User/HostController.php index 6f8acba..3b53343 100644 --- a/app/Http/Controllers/User/HostController.php +++ b/app/Http/Controllers/User/HostController.php @@ -68,6 +68,12 @@ public function usages() $month_cache_key = 'user_' . auth()->id() . '_month_' . $month . '_hosts_drops'; $hosts_drops = Cache::get($month_cache_key, []); - return $this->success($hosts_drops); + $month_cache_key = 'user_' . auth()->id() . '_month_' . $month . '_hosts_balances'; + $hosts_balances = Cache::get($month_cache_key, []); + + return $this->success([ + 'drops' => $hosts_drops, + 'balances' => $hosts_balances + ]); } } diff --git a/app/Models/Host.php b/app/Models/Host.php index 78574ce..ae148e5 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -157,6 +157,20 @@ public function costBalance($amount = 1) { $transaction = new Transaction(); + $month = now()->month; + + $month_cache_key = 'user_' . $this->user_id . '_month_' . $month . '_hosts_balances'; + $hosts_drops = Cache::get($month_cache_key, []); + + // 统计 Host 消耗的 Drops + if (isset($hosts_drops[$this->id])) { + $hosts_drops[$this->id] += $amount; + } else { + $hosts_drops[$this->id] = $amount; + } + + Cache::put($month_cache_key, $hosts_drops, 604800); + $left = $transaction->reduceAmount($this->user_id, $amount); broadcast(new UserEvent($this->user_id, 'balances.amount.reduced', $this->user));