diff --git a/app/Http/Controllers/Module/UserController.php b/app/Http/Controllers/Module/UserController.php index 5c614fc..8e4906c 100644 --- a/app/Http/Controllers/Module/UserController.php +++ b/app/Http/Controllers/Module/UserController.php @@ -24,15 +24,15 @@ public function index(Request $request): JsonResponse // 搜索 name, email, balance if ($request->has('name')) { - $users->where('name', 'like', '%'.$request->input('name').'%'); + $users->where('name', 'like', '%' . $request->input('name') . '%'); } if ($request->has('email')) { - $users->where('email', 'like', '%'.$request->input('email').'%'); + $users->where('email', 'like', '%' . $request->input('email') . '%'); } if ($request->has('balance')) { - $users->where('balance', 'like', '%'.$request->input('balance').'%'); + $users->where('balance', 'like', '%' . $request->input('balance') . '%'); } $users = $users->simplePaginate(100); @@ -88,7 +88,7 @@ public function update(Request $request, User $user): JsonResponse return $this->error('用户余额不足。'); } - $user->reduce($balance, $request->description, true); + $trans = $user->reduce($balance, $request->description, true); $module->charge($balance, 'balance', $request->description, [ 'user_id' => $user->id, ]); @@ -101,11 +101,12 @@ public function update(Request $request, User $user): JsonResponse 'user_id' => $user->id, 'payment' => 'module_balance', ]); - - $user->charge($balance, 'module_balance', $request->description); + $trans = $user->charge($balance, 'module_balance', $request->description, [ + 'module_id' => $module->id + ]); } - return $this->updated(); + return $this->success($trans); } public function auth($token): JsonResponse diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 6c116fc..45ed445 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use Jenssegers\Mongodb\Eloquent\Model; +use Symfony\Component\Uid\Ulid; class Transaction extends Model { diff --git a/app/Models/User.php b/app/Models/User.php index 767a37d..a5619f5 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -166,19 +166,20 @@ public function startTransfer(self $to, string $amount, string|null $description /** * 扣除费用 * - * @param string|null $amount - * @param string $description - * @param bool $fail - * @param array $options - * @return string + * @param string|null $amount + * @param string $description + * @param bool $fail + * @param array $options + * + * @return Transaction */ - public function reduce(string|null $amount = '0', string $description = '消费', bool $fail = false, array $options = []): string + public function reduce(string|null $amount = '0', string $description = '消费', bool $fail = false, array $options = []): Transaction { if ($amount === null || $amount === '') { return $this->balance; } - Cache::lock('user_balance_'.$this->id, 10)->block(10, function () use ($amount, $fail, $description, $options) { + return Cache::lock('user_balance_'.$this->id, 10)->block(10, function () use ($amount, $fail, $description, $options) { $this->refresh(); if ($this->balance < $amount) { @@ -202,30 +203,29 @@ public function reduce(string|null $amount = '0', string $description = '消费' $data = array_merge($data, $options); } - (new Transaction)->create($data); - broadcast(new Users($this, 'balances.amount.reduced', $this)); - }); - return $this->balance; + return (new Transaction)->create($data); + }); } /** * 增加余额 * - * @param string|null $amount - * @param string $payment - * @param string $description - * @param array $options - * @return string + * @param string|null $amount + * @param string $payment + * @param string $description + * @param array $options + * + * @return Transaction */ - public function charge(string|null $amount = '0', string $payment = 'console', string $description = '充值', array $options = []): string + public function charge(string|null $amount = '0', string $payment = 'console', string $description = '充值', array $options = []): Transaction { if ($amount === null || $amount === '') { return $this->balance; } - Cache::lock('user_balance_'.$this->id, 10)->block(10, function () use ($amount, $description, $payment, $options) { + return 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(); @@ -242,8 +242,6 @@ public function charge(string|null $amount = '0', string $payment = 'console', s $data = array_merge($data, $options); } - (new Transaction)->create($data); - if (isset($options['add_balances_log']) && $options['add_balances_log'] === true) { (new Balance)->create([ 'user_id' => $this->id, @@ -253,9 +251,9 @@ public function charge(string|null $amount = '0', string $payment = 'console', s 'paid_at' => now(), ]); } - }); - return $this->balance; + return (new Transaction)->create($data); + }); } public function getCostPrice(string $price): string