*/ public static function create(int $count, array $options = []): array { SimulationSettings::assertEnabled(); $settings = SimulationSettings::all(); if ($count < 1 || $count > (int) $settings['simulation_max_members_per_task']) { throw new Exception('一次最多创建 ' . $settings['simulation_max_members_per_task'] . ' 个模拟会员', HttpStatus::CUSTOM_ERROR); } $existing = DB::table('users')->where('user_type', MemberAudience::TYPE_SIMULATED)->count(); if ($existing + $count > (int) $settings['simulation_max_members_total']) { throw new Exception('模拟会员总数将超过上限 ' . $settings['simulation_max_members_total'], HttpStatus::CUSTOM_ERROR); } $domain = trim((string) ($options['domain'] ?? '')) ?: $settings['simulation_default_domain']; $initial = $options['initial_balance'] ?? $settings['simulation_initial_balance']; $created = []; for ($i = 0; $i < $count; $i++) { $user = DB::transaction(function () use ($domain) { $sn = self::serial(); $user = (new User())->forceFill([ 'member_id' => $sn, 'user_id' => $sn, 'account' => self::account(), 'first_name' => self::nickname(), 'username' => '', 'from' => '2', 'user_type' => MemberAudience::TYPE_SIMULATED, 'register_ip' => self::ip(), 'register_domain' => $domain, 'last_active_time' => time(), ]); $user->save(); (new Wallet())->forceFill(['user_id' => $user->id, 'member_id' => $sn, 'coin' => 'USDT', 'net' => 'TRC20', 'available_balance' => 0])->save(); return $user; }); MemberAudience::remember($user->member_id); if (is_numeric($initial) && $initial > 0) SimulationFundService::adjust($user->member_id, $initial, 'credit', '初始余额'); $created[] = ['id' => (int) $user->id, 'member_id' => (string) $user->member_id, 'account' => (string) $user->account]; } return $created; } private const SURNAMES = ['wang', 'li', 'zhang', 'liu', 'chen', 'yang', 'huang', 'zhao', 'wu', 'zhou', 'xu', 'sun', 'ma', 'zhu', 'hu', 'guo', 'he', 'lin', 'gao', 'luo', 'zheng', 'liang', 'xie', 'song', 'tang', 'han', 'feng', 'deng', 'cao', 'peng', 'zeng', 'xiao', 'tian', 'dong', 'pan', 'yuan', 'cai', 'jiang', 'yu', 'du']; private const GIVEN = ['wei', 'fang', 'na', 'min', 'jing', 'lei', 'qiang', 'jun', 'yang', 'yong', 'jie', 'juan', 'tao', 'ming', 'chao', 'xia', 'ping', 'gang', 'hui', 'yan', 'ling', 'bo', 'hao', 'yu', 'xin', 'peng', 'fei', 'kai', 'long', 'bin', 'ting', 'xue', 'dan', 'rui', 'chen', 'hong', 'lan', 'feng', 'shuai', 'kun']; private const WORDS = ['lucky', 'happy', 'sunny', 'tiger', 'dragon', 'star', 'moon', 'king', 'boss', 'cool', 'super', 'win', 'gold', 'ace', 'fly', 'sky', 'blue', 'fox', 'wolf', 'bear']; private const SURNAMES_CN = ['王', '李', '张', '刘', '陈', '杨', '黄', '赵', '吴', '周', '徐', '孙', '马', '朱', '胡', '郭', '何', '林', '高', '罗', '郑', '梁', '谢', '宋', '唐', '韩', '冯', '邓', '曹', '彭']; private const GIVEN_CN = ['伟', '芳', '娜', '敏', '静', '磊', '强', '军', '洋', '勇', '杰', '娟', '涛', '明', '超', '霞', '平', '刚', '辉', '燕', '玲', '波', '浩', '宇', '欣', '鹏', '飞', '凯', '龙', '斌', '婷', '雪', '丹', '瑞', '晨', '红', '兰', '峰', '帅', '坤', '子轩', '浩然', '雨涵', '梓涵', '一鸣', '思远', '佳怡', '俊杰', '志强', '晓东']; private const NICKNAMES = ['风一样的男子', '小幸运', '夜空中最亮的星', '快乐每一天', '往事随风', '平安是福', '一路向北', '海阔天空', '知足常乐', '顺其自然', '好运连连', '财源滚滚', '阳光明媚', '淡定', '大圣归来', '追风少年', '南方姑娘', '老司机', '独行侠', '清风徐来', '人间烟火', '星辰大海', '江湖再见', '稳稳的幸福', '发发发', '明天会更好']; private static function pick(array $list): string { return $list[random_int(0, count($list) - 1)]; } /** 像真实会员自己起的登录账号:拼音姓名或英文词加数字,6~16 位,字母开头,全库唯一。 */ private static function account(): string { for ($try = 0; $try < 50; $try++) { $base = match (random_int(0, 3)) { 0 => self::pick(self::SURNAMES) . self::pick(self::GIVEN), 1 => self::pick(self::SURNAMES) . self::pick(self::GIVEN) . self::pick(self::GIVEN), 2 => self::pick(self::WORDS) . self::pick(self::SURNAMES), default => substr(self::pick(self::SURNAMES), 0, 1) . substr(self::pick(self::GIVEN), 0, 1) . substr(self::pick(self::GIVEN), 0, 1), }; $digits = match (random_int(0, 3)) { 0 => (string) random_int(1975, 2005), 1 => (string) random_int(10, 99), 2 => str_pad((string) random_int(1, 1231), 4, '0', STR_PAD_LEFT), default => (string) random_int(100, 999999), }; $account = substr($base . $digits, 0, 16); if (strlen($account) >= 6 && !DB::table('users')->where('account', $account)->exists()) return $account; } throw new Exception('会员账号生成失败,请重试', HttpStatus::CUSTOM_ERROR); } /** 像真实会员的昵称:中文姓名、网名,偶尔带数字。 */ private static function nickname(): string { return match (random_int(0, 4)) { 0, 1 => self::pick(self::SURNAMES_CN) . self::pick(self::GIVEN_CN), 2 => self::pick(self::NICKNAMES), 3 => self::pick(self::NICKNAMES) . random_int(1, 999), default => ucfirst(self::pick(self::WORDS)) . random_int(10, 9999), }; } /** 随机的公网 IPv4(避开保留网段)。 */ private static function ip(): string { do { $first = random_int(1, 223); } while (in_array($first, [10, 100, 127, 169, 172, 192, 198, 203], true)); return $first . '.' . random_int(0, 255) . '.' . random_int(0, 255) . '.' . random_int(1, 254); } /** * 给早期创建的模拟会员改名:账号 sim_ 开头、昵称“模拟_”开头、IP 为 127.0.0.1 的,换成和新会员一样的随机真实风格; * 同时改掉这些会员名下记录里存的旧名字,以及旧的“模拟主队 / 模拟视讯 / 模拟银行”等展示名。 * 只动 users.user_type = 2 的会员及其记录;会员编号、余额、金额、订单都不变。可重复执行,改过的不会再改。 * @return array 各项改动的行数;$dryRun 时只统计不写库 */ public static function renameLegacy(bool $dryRun = false): array { if (!MemberAudience::enabled()) throw new Exception('用户类型字段尚未迁移', HttpStatus::CUSTOM_ERROR); $counts = ['members' => 0, 'accounts' => 0, 'nicknames' => 0, 'ips' => 0, 'login_logs' => 0, 'third_game_orders' => 0, 'withdraw_orders' => 0, 'sport_orders' => 0]; $has = fn (string $table, string $column) => Schema::hasTable($table) && Schema::hasColumn($table, $column); $members = DB::table('users')->where('user_type', MemberAudience::TYPE_SIMULATED)->orderBy('id')->get(); foreach ($members as $user) { $oldAccount = (string) $user->account; $oldName = (string) $user->first_name; $changes = []; if (str_starts_with($oldAccount, 'sim_')) { $changes['account'] = self::account(); $counts['accounts']++; } if (str_starts_with($oldName, '模拟')) { $changes['first_name'] = self::nickname(); $counts['nicknames']++; } if (in_array((string) ($user->register_ip ?? ''), ['127.0.0.1', ''], true)) { $changes['register_ip'] = self::ip(); $counts['ips']++; } $ip = $changes['register_ip'] ?? (string) $user->register_ip; $name = $changes['first_name'] ?? $oldName; $work = function () use ($user, $changes, $oldAccount, $oldName, $name, $ip, $has, $dryRun, &$counts) { if ($changes && !$dryRun) DB::table('users')->where('id', $user->id)->where('user_type', MemberAudience::TYPE_SIMULATED)->update($changes); // 登录日志里存的账号和 IP if ($has('user_login', 'login_account')) { $logs = DB::table('user_login')->where('user_id', (string) $user->user_id) ->where(fn ($q) => $q->whereRaw("SUBSTR(login_account, 1, 4) = 'sim_'")->orWhere('login_ip', '127.0.0.1')); $counts['login_logs'] += $dryRun ? $logs->count() : $logs->update(['login_account' => $changes['account'] ?? $oldAccount, 'login_ip' => $ip]); } // 第三方注单里存的昵称,以及旧的“模拟视讯”等游戏名 if (Schema::hasTable('third_game_orders')) { foreach (DB::table('third_game_orders')->where('user_id', $user->id)->where(fn ($q) => $q->where('first_name', 'like', '模拟%') ->orWhere('game_name', 'like', '模拟%'))->get(['id', 'game_type']) as $order) { $game = SimulationBetService::THIRD_GAMES[(int) $order->game_type] ?? '百家乐'; if (!$dryRun) DB::table('third_game_orders')->where('id', $order->id)->update(['first_name' => $name, 'game_name' => $game, 'bet_content' => $game]); $counts['third_game_orders']++; } } // 提现单上的收款人、银行和卡号 if (Schema::hasTable('payment_orders')) { foreach (DB::table('payment_orders')->where('member_id', (string) $user->member_id)->where('type', 4) ->where(fn ($q) => $q->where('account', 'like', '模拟%')->orWhere('bank_name', '模拟银行')->orWhere('card_no', 'like', 'SIM%'))->pluck('id') as $id) { if (!$dryRun) DB::table('payment_orders')->where('id', $id)->update(['account' => $name, 'bank_name' => SimulationFundService::randomBank(), 'card_no' => SimulationFundService::randomCard()]); $counts['withdraw_orders']++; } } // 体育注单的“模拟主队 VS 模拟客队” if ($has('sport_order', 'odd_name')) { foreach (DB::table('sport_order')->where('user_id', (string) $user->user_id)->where('odd_name', 'like', '%模拟%')->get(['id', 'detail']) as $order) { $match = SimulationBetService::randomMatch(); $detail = json_decode((string) $order->detail, true); $update = ['odd_name' => $match]; if (is_array($detail) && isset($detail['title'])) $update['detail'] = json_encode(['title' => $match] + $detail, JSON_UNESCAPED_UNICODE); if (!$dryRun) DB::table('sport_order')->where('id', $order->id)->update($update); $counts['sport_orders']++; } } }; $before = array_sum($counts); $dryRun ? $work() : DB::transaction($work); if ($changes || array_sum($counts) > $before) $counts['members']++; } return $counts; } /** 8 位 1-9 数字,与前台会员编号同形;纯数字是因为 wallets.member_id 是 bigint。 */ private static function serial(): string { for ($try = 0; $try < 50; $try++) { $sn = ''; for ($i = 0; $i < 8; $i++) $sn .= (string) random_int(1, 9); if (!DB::table('users')->where('member_id', $sn)->orWhere('user_id', $sn)->exists()) return $sn; } throw new Exception('会员编号生成失败,请重试', HttpStatus::CUSTOM_ERROR); } public static function setBanned($memberId, int $banned): void { $user = SimulationSettings::member($memberId); DB::table('users')->where('id', $user->id)->update(['is_banned' => $banned ? 1 : 0]); } /** 只写登录日志,不建会话、不签发 token:模拟会员没有可登录前台的凭证。 */ public static function login($memberId, string $platform = 'H5', ?string $domain = null): array { SimulationSettings::assertEnabled(); $user = SimulationSettings::member($memberId); self::logout($memberId); $row = ['user_id' => (string) $user->user_id, 'login_account' => (string) $user->account, 'login_ip' => filter_var((string) ($user->register_ip ?? ''), FILTER_VALIDATE_IP) ? (string) $user->register_ip : self::ip(), 'login_domain' => $domain ?: SimulationSettings::get('simulation_default_domain'), 'platform' => $platform, 'status' => UserLogin::STATUS_SUCCESS, 'created_at' => now(), 'updated_at' => now()]; $row = array_filter($row, fn ($column) => Schema::hasColumn('user_login', $column), ARRAY_FILTER_USE_KEY); $id = DB::table('user_login')->insertGetId($row); $update = ['last_active_time' => time()]; if (Schema::hasColumn('users', 'last_login_time')) $update += ['last_login_time' => time(), 'login_count' => DB::raw('COALESCE(login_count, 0) + 1')]; DB::table('users')->where('id', $user->id)->update($update); return ['id' => $id, 'member_id' => (string) $user->member_id]; } public static function logout($memberId): int { $user = SimulationSettings::member($memberId); if (!Schema::hasColumn('user_login', 'logout_time')) return 0; $open = DB::table('user_login')->where('user_id', (string) $user->user_id)->where('status', UserLogin::STATUS_SUCCESS)->whereNull('logout_time')->get(['id', 'created_at']); foreach ($open as $row) { DB::table('user_login')->where('id', $row->id)->update(['logout_time' => now(), 'online_duration' => max(0, now()->getTimestamp() - strtotime((string) $row->created_at)), 'updated_at' => now()]); } return $open->count(); } /** 当前“在线”的模拟会员:有未登出的成功登录记录。 */ public static function onlineUserIds() { $query = DB::table('user_login')->where('status', UserLogin::STATUS_SUCCESS)->whereIn('user_id', MemberAudience::simulatedMembers('user_id')); if (Schema::hasColumn('user_login', 'logout_time')) $query->whereNull('logout_time'); return $query->distinct()->pluck('user_id'); } public static function detail($memberId): array { $user = SimulationSettings::member($memberId); $wallet = DB::table('wallets')->where('member_id', $user->member_id)->first(); return [ 'user' => ['id' => $user->id, 'member_id' => $user->member_id, 'account' => $user->account, 'first_name' => $user->first_name, 'is_banned' => (int) ($user->is_banned ?? 0), 'register_domain' => $user->register_domain ?? '', 'created_at' => $user->created_at], 'wallet' => $wallet ? ['available_balance' => (string) $wallet->available_balance, 'frozen_balance' => (string) $wallet->frozen_balance, 'wager_required' => (string) ($wallet->wager_required ?? 0), 'wager_completed' => (string) ($wallet->wager_completed ?? 0)] : null, 'logs' => DB::table('balance_logs')->where('member_id', $user->member_id)->orderByDesc('id')->limit(20)->get(), ]; } }