oms/backend/database/migrations/20260909000700-seed-menus.js
liufumingren 8597cf5ac9 feat(backend): 用户鉴权、菜单权限、主体与系统基础模块
- 登录/邮箱验证码/密码流程(controller/auth.js、lib/emailCode.js、middleware/jwt_auth.js)
- 主体(principal)隔离与主体用户、对外接口配置模块及 principalCheck 中间件
- 菜单、用户、文件上传、快递公司字典(express_companies)等基础模块
- 通用工具:分页(lib/pagination.js)、缓存(service/cache.js)
- 全量接口契约(contract/index.js)与路由注册(router.js)、config.default.js 配置
- 相关迁移:菜单组件调整与菜单种子、创建 files 表
2026-09-13 02:23:34 +08:00

88 lines
3.3 KiB
JavaScript

'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');
},
};