'use strict'; /** * 7. 种子菜单:依据后端已有功能模块设计菜单树,供前端通过 /api/menus/tree 拉取并动态生成侧边栏与路由。 * 菜单 component 字段指向前端 views 下的页面(默认统一指向 views/Generic.vue 占位页)。 * 仅在 menus 表为空时插入,避免重复。 */ const seedMenus = [ { name: '仪表盘', type: 2, path: '/dashboard', icon: 'HomeFilled', component: 'views/Home.vue', sort: 100, }, { name: '系统管理', type: 1, icon: 'Setting', sort: 90, children: [ { name: '用户管理', type: 2, path: '/system/user', icon: 'User', permission: 'user:list', component: 'views/Generic.vue', sort: 90 }, { name: '主体管理', type: 2, path: '/system/principal', icon: 'OfficeBuilding', permission: 'principal:list', component: 'views/Principal/Index.vue', sort: 80 }, { name: '主体用户', type: 2, path: '/system/principal-user', icon: 'Connection', permission: 'principalUser:list', component: 'views/Generic.vue', sort: 70 }, { name: '菜单管理', type: 2, path: '/system/menu', icon: 'Menu', permission: 'menu:list', component: 'views/Generic.vue', sort: 60 }, { name: '外部API', type: 2, path: '/system/external-api', icon: 'Link', permission: 'principalExternalApi:list', component: 'views/Generic.vue', sort: 50 }, ], }, { name: '商品管理', type: 1, icon: 'Goods', sort: 80, children: [ { name: '商品', type: 2, path: '/product', icon: 'Box', permission: 'product:list', component: 'views/Generic.vue', sort: 80 }, { name: '组合商品', type: 2, path: '/combo-product', icon: 'Files', permission: 'comboProduct:list', component: 'views/Generic.vue', sort: 70 }, ], }, { name: '订单管理', type: 1, icon: 'List', sort: 70, children: [ { name: '订单', type: 2, path: '/order', icon: 'Tickets', permission: 'order:list', component: 'views/Generic.vue', sort: 70 }, ], }, ]; module.exports = { async up(queryInterface) { const sequelize = queryInterface.sequelize; const [rows] = await sequelize.query('SELECT COUNT(*) AS count FROM menus', { type: sequelize.QueryTypes.SELECT, }); if (Number(rows.count) > 0) return; // 已存在菜单数据,跳过种子 async function insertMenu(m, parentId) { await sequelize.query( `INSERT INTO menus (parent_id, name, type, path, icon, permission, component, sort, status, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())`, { replacements: [ parentId, m.name, m.type, m.path || null, m.icon || null, m.permission || null, m.component || null, m.sort || 0, 1, ], } ); const [idRows] = await sequelize.query('SELECT LAST_INSERT_ID() AS id', { type: sequelize.QueryTypes.SELECT, }); return Number(idRows.id); } async function insertTree(list, parentId) { for (const m of list) { const id = await insertMenu(m, parentId); if (m.children && m.children.length) { await insertTree(m.children, id); } } } await insertTree(seedMenus, 0); }, async down(queryInterface) { await queryInterface.sequelize.query('DELETE FROM menus'); }, };