From 3872b5a9bbb8f5098bf611f5d0b549e10a5339dd Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Mon, 13 Feb 2023 02:22:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=BB=84=E4=BB=B7=E6=A0=BC=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/User.php | 36 ++++++++++++++++++------------------ app/Models/UserGroup.php | 2 +- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 92d3129..9261882 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -2,6 +2,7 @@ namespace App\Models; +use App\Events\Users; use App\Exceptions\User\BalanceNotEnoughException; use GeneaLabs\LaravelModelCaching\CachedBuilder; use GeneaLabs\LaravelModelCaching\Traits\Cachable; @@ -87,11 +88,11 @@ public function getBirthdayFromIdCard(string|null $id_card = null): Carbon } $bir = substr($id_card, 6, 8); - $year = (int)substr($bir, 0, 4); - $month = (int)substr($bir, 4, 2); - $day = (int)substr($bir, 6, 2); + $year = (int) substr($bir, 0, 4); + $month = (int) substr($bir, 4, 2); + $day = (int) substr($bir, 6, 2); - return Carbon::parse($year . '-' . $month . '-' . $day); + return Carbon::parse($year.'-'.$month.'-'.$day); } public function hasBalance(string $amount = '0.01'): bool @@ -166,22 +167,20 @@ public function getCostPrice(string $price): string { $this->load('user_group'); - if (!$this->user_group) { + if (! $this->user_group) { return $price; } return $this->user_group->getCostPrice($price); } - /** * 扣除费用 * - * @param string|null $amount - * @param string $description - * @param bool $fail - * @param array $options - * + * @param string|null $amount + * @param string $description + * @param bool $fail + * @param array $options * @return string */ public function reduce(string|null $amount = '0', string $description = '消费', bool $fail = false, array $options = []): string @@ -190,7 +189,7 @@ public function reduce(string|null $amount = '0', string $description = '消费' return $this->balance; } - Cache::lock('user_balance_' . $this->id, 10)->block(10, function () use ($amount, $fail, $description, $options) { + Cache::lock('user_balance_'.$this->id, 10)->block(10, function () use ($amount, $fail, $description, $options) { $this->refresh(); if ($this->balance < $amount) { @@ -215,6 +214,8 @@ public function reduce(string|null $amount = '0', string $description = '消费' } (new Transaction)->create($data); + + broadcast(new Users($this, 'balances.amount.reduced', $this)); }); return $this->balance; @@ -223,11 +224,10 @@ public function reduce(string|null $amount = '0', string $description = '消费' /** * 增加余额 * - * @param string|null $amount - * @param string $payment - * @param string $description - * @param array $options - * + * @param string|null $amount + * @param string $payment + * @param string $description + * @param array $options * @return string */ public function charge(string|null $amount = '0', string $payment = 'console', string $description = '充值', array $options = []): string @@ -236,7 +236,7 @@ public function charge(string|null $amount = '0', string $payment = 'console', s return $this->balance; } - Cache::lock('user_balance_' . $this->id, 10)->block(10, function () use ($amount, $description, $payment, $options) { + Cache::lock('user_balance_'.$this->id, 10)->block(10, function () use ($amount, $description, $payment, $options) { $this->refresh(); $this->balance = bcadd($this->balance, $amount, 4); $this->save(); diff --git a/app/Models/UserGroup.php b/app/Models/UserGroup.php index e4c3597..45fd41d 100644 --- a/app/Models/UserGroup.php +++ b/app/Models/UserGroup.php @@ -65,7 +65,7 @@ public function getCostPrice(string $price): string { if ($this->exempt) { return '0'; - } else if ($this->discount !== 100 && $this->discount !== null) { + } elseif ($this->discount !== 100 && $this->discount !== null) { $price = bcmul($price, bcdiv($this->discount, '100', 4), 4); }