where(['user_id' => $user_id])->first(); if (!empty($field)) { return $wallet[$field] ?? ''; } return $wallet; } /** * 钱包操作 * @param int $user_id * @param float $money * @param string $remark * @param int $type * @return bool */ public function change(int $user_id, float $money, string $remark = '', int $type = 3) { if (!in_array($type, [3, 4])) { return $this->error('余额类型错误'); } $wallet = (new static())->getOne($user_id); if ($type == 3) { // 消费 $data = [ 'money' => bcadd($wallet['money'], (string)(-$money), 2), 'disburse_money' => bcadd($wallet['disburse_money'], (string)$money, 2) ]; if ($data['money'] < 0) { return $this->error('余额不足'); } $log_money = -$money; } else { // 退还 $data = [ 'money' => bcadd($wallet['money'], (string)$money, 2), 'disburse_money' => bcadd($wallet['disburse_money'], (string)(-$money), 2) ]; $log_money = $money; } if (!$this->query()->where(['user_id' => $user_id, 'money' => $wallet['money']])->update($data)) { $this->error('操作失败'); } if (!UserMoneyLogModel::query()->insert(['user_id' => $user_id, 'type' => $type, 'money' => $log_money, 'before' => $wallet['money'], 'after' => $data['money'], 'remark' => $remark, 'create_time' => time()])) { $this->error('记录操作失败'); } return $this->success('操作成功'); } }