/** * 随机新增 20 个测试用户(默认密码 123456) * 用法:node server/scripts/seed-users.mjs */ const BASE = process.env.F9MES_API || 'http://localhost:5136'; const PHONE = '13800000000'; const PASSWORD = '123456'; const SURNAMES = '王李张刘陈杨黄赵周吴徐孙马朱胡郭何高林罗郑梁谢宋唐许韩冯邓曹彭曾肖田董袁潘于蒋蔡余杜叶程苏魏吕丁任沈姚卢姜崔钟谭陆汪范金石廖贾夏韦傅方白邹孟熊秦邱江尹薛闫段雷侯龙史陶黎贺顾毛郝龚邵万钱严覃武戴莫孔向汤'.split(''); const GIVEN = '伟芳娜敏静丽强磊军洋勇艳杰娟涛明超秀兰霞平刚桂英华建国志强建华建军建国云萍晓华小红美玲玉珍玉梅国强建平'.split(''); function rand(arr) { return arr[Math.floor(Math.random() * arr.length)]; } /** 随机 11 位手机号(138/139/150/151/152/158/159/186/188/189 开头) */ function randPhone() { const head = rand(['138', '139', '150', '151', '152', '158', '159', '186', '188', '189']); let tail = ''; for (let i = 0; i < 8; i++) tail += Math.floor(Math.random() * 10); return head + tail; } async function api(h, method, url, body) { const res = await fetch(BASE + url, { method, headers: h, body: body ? JSON.stringify(body) : undefined, }); const text = await res.text(); let json = null; try { json = JSON.parse(text); } catch { /* ignore */ } return { ok: res.ok, json, text }; } async function main() { const login = await api({ 'Content-Type': 'application/json' }, 'POST', '/api/auth/login', { phone: PHONE, password: PASSWORD, }); if (!login.ok || login.json?.code !== 0) throw new Error('管理员登录失败: ' + login.text.slice(0, 200)); const token = login.json.data.token; const h = { Authorization: 'Bearer ' + token, 'Content-Type': 'application/json' }; // 获取现有手机号,避免冲突 const exist = await api(h, 'GET', '/api/data/BaseSys_User/all'); const items = exist.json?.data?.items || (Array.isArray(exist.json?.data) ? exist.json.data : []); const used = new Set(items.map((u) => u.phone || u.Phone).filter(Boolean)); let created = 0, skipped = 0; const results = []; while (created < 20) { const phone = randPhone(); if (used.has(phone)) { skipped++; continue; } used.add(phone); const name = rand(SURNAMES) + rand(GIVEN); const gender = Math.random() < 0.5 ? 1 : 2; const r = await api(h, 'POST', '/api/basesys/user/create', { phone, password: PASSWORD, name, gender, status: 1, }); if (r.ok && r.json?.code === 0) { const id = r.json.data?.id ?? r.json.data; results.push({ id, name, phone }); created++; } else { // 该手机号可能已被占(并发/冲突),换号重试 skipped++; } } console.log('新增用户 ' + created + ' 个(跳过/重试 ' + skipped + ' 次)'); console.log('默认密码:' + PASSWORD); console.table(results.map((u) => ({ ID: u.id, 姓名: u.name, 手机号: u.phone }))); console.log('\n完成。'); } main().catch((e) => { console.error('ERR: ' + e.message); process.exit(1); });