$type; $after = bcadd((string)$user->$type, (string)$amount, 2); // 只有后台扣除用户余额、扣除用户积分、佣金退回,钱包才可以是负值 if ($after < 0 && !in_array($event, ['admin_recharge', 'reward_back'])) { $walletTypeText = WalletLog::TYPE_MAP[$type]; error_stop("可用{$walletTypeText}不足"); } try { // 更新会员余额信息 $user->setInc($type, $amount); // 获取操作人 $oper = Operator::get(); // 写入日志 $walletLog = WalletLog::create([ 'user_id' => $user->id, 'amount' => $amount, 'type' => $type, 'before' => $before, 'after' => $after, 'event' => $event, 'memo' => $memo, 'ext' => $ext, 'oper_type' => $oper['type'], 'oper_id' => $oper['id'] ]); // 钱包和积分记录存到 fastadmin 钱包积分记录表 if (in_array($type, ['money', 'score'])) { $eventMap = (new WalletLog)->getEventMap(); $memo = $memo ?: $eventMap[$type][$event] ?? ''; if ($type === 'money') { MoneyLog::create(['user_id' => $user->id, 'money' => $amount, 'before' => $before, 'after' => $after, 'memo' => $memo]); } else if ($type === 'score') { ScoreLog::create(['user_id' => $user->id, 'score' => $amount, 'before' => $before, 'after' => $after, 'memo' => $memo]); } } // 账户变动事件 $data = ['walletLog' => $walletLog, 'type' => $type]; \think\Hook::listen('user_wallet_change', $data); } catch (HttpResponseException $e) { $data = $e->getResponse()->getData(); $message = $data ? ($data['msg'] ?? '') : $e->getMessage(); error_stop($message); } catch (\Exception $e) { error_stop('您提交的数据不正确'); } return true; } }