/** * IM 管理菜单种子:协作(目录)下追加 4 个管理页面 * - 性能监测(/im/monitor) * - 服务监测(/im/service) * - 配置界面(/im/config) * - 消息与公告管理(/im/message) * ⚠️ 顶层必须用目录节点(MenuType=0),页面节点(MenuType=1)必须挂目录下。 * 用法:node server/scripts/seed-im-admin.mjs */ const BASE = process.env.F9MES_API || 'http://localhost:5136'; const PHONE = '13800000000'; const PASSWORD = '123456'; 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 */ } if (!res.ok || (json && json.code !== 0)) { throw new Error(`HTTP ${res.status} ${url}: ${text.slice(0, 300)}`); } return json; } const PAGES = [ { name: '性能监测', routePath: '/im/monitor', component: 'Im/monitor', icon: 'Monitor', sort: 20 }, { name: '服务监测', routePath: '/im/service', component: 'Im/service', icon: 'Connection', sort: 30 }, { name: '配置界面', routePath: '/im/config', component: 'Im/config', icon: 'Setting', sort: 40 }, { name: '消息与公告管理', routePath: '/im/message', component: 'Im/message', icon: 'Bell', sort: 50 }, ]; async function main() { const login = await api({ 'Content-Type': 'application/json' }, 'POST', '/api/auth/login', { phone: PHONE, password: PASSWORD, }); const token = login.data.token; const h = { Authorization: 'Bearer ' + token, 'Content-Type': 'application/json' }; const menus = await api(h, 'GET', '/api/data/BaseSys_Menu/all'); const items = menus.data?.data || []; let dir = items.find((m) => m.name === '协作' && m.menuType === 0); if (!dir) { const r = await api(h, 'POST', '/api/data/BaseSys_Menu/add', { name: '协作', menuType: 0, routePath: '', component: '', icon: 'ChatDotRound', isVisible: 1, isEnable: 1, sort: 998, parentId: 0, level: 0, path: '', }); dir = { id: r.data && (r.data.id ?? r.data) }; console.log('菜单目录「协作」已创建 id=' + dir.id); } else { console.log('菜单目录「协作」已存在 id=' + dir.id); } const dirId = dir.id; for (const p of PAGES) { const exists = items.find((m) => m.parentId === dirId && m.routePath === p.routePath); if (exists) { console.log(`菜单「${p.name}」已存在 id=${exists.id}`); continue; } const r = await api(h, 'POST', '/api/data/BaseSys_Menu/add', { name: p.name, menuType: 1, routePath: p.routePath, component: p.component, icon: p.icon, isVisible: 1, isEnable: 1, sort: p.sort, parentId: dirId, level: 1, path: '/' + dirId, }); console.log(`菜单「${p.name}」已创建 id=${r.data && (r.data.id ?? r.data)}`); } console.log('\n完成。协作菜单下已就绪 4 个 IM 管理页面。'); } main().catch((e) => { console.error('ERR: ' + e.message); process.exit(1); });