diff --git a/app/Models/Host.php b/app/Models/Host.php index 30ccbe9..f332012 100644 --- a/app/Models/Host.php +++ b/app/Models/Host.php @@ -54,6 +54,11 @@ protected static function boot() }); static::created(function (self $model) { + $model->load('module'); + + // model price 使用 bcmul 保留两位小数 + $model->price = bcmul($model->price, 1, 2); + $model->user->notify(new WebNotification($model, 'hosts.created')); }); @@ -97,13 +102,13 @@ protected static function boot() // }); static::deleting(function ($model) { - Cache::forget('user_tasks_'.$model->user_id); + Cache::forget('user_tasks_' . $model->user_id); }); static::deleted(function ($model) { broadcast(new Users($model->user_id, 'hosts.deleted', $model)); - Cache::forget('user_tasks_'.$model->user_id); - Cache::forget('user_hosts_'.$model->user_id); + Cache::forget('user_tasks_' . $model->user_id); + Cache::forget('user_hosts_' . $model->user_id); }); } @@ -179,7 +184,7 @@ public function cost(string $amount = null, $auto = true): bool $real_price = $amount ?? $this->price; - if (! $amount) { + if (!$amount) { if ($this->managed_price) { $real_price = $this->managed_price; } @@ -190,7 +195,7 @@ public function cost(string $amount = null, $auto = true): bool if ($user_group->discount !== 100 && $user_group->discount !== null) { $real_price = bcmul($real_price, bcdiv($user_group->discount, '100', 4), 4); - $append_description = ' (折扣 '.$user_group->discount.'%)'; + $append_description = ' (折扣 ' . $user_group->discount . '%)'; } } @@ -218,7 +223,7 @@ public function cost(string $amount = null, $auto = true): bool $month = now()->month; - $month_cache_key = 'user_'.$this->user_id.'_month_'.$month.'_hosts_balances'; + $month_cache_key = 'user_' . $this->user_id . '_month_' . $month . '_hosts_balances'; $hosts_balances = Cache::get($month_cache_key, []); // 统计 Host 消耗的 Balance @@ -272,7 +277,7 @@ public function addLog(string $amount = '0'): bool $current_month = now()->month; $current_year = now()->year; - $cache_key = 'module_earning_'.$this->module_id; + $cache_key = 'module_earning_' . $this->module_id; // 应支付的提成 $commission = config('billing.commission'); @@ -287,7 +292,7 @@ public function addLog(string $amount = '0'): bool $earnings = Cache::get($cache_key, []); - if (! isset($earnings[$current_year])) { + if (!isset($earnings[$current_year])) { $earnings[$current_year] = []; }