'use strict'; module.exports = app => { const { INTEGER, STRING, DATE, TINYINT, ENUM } = app.Sequelize; const Permission = app.model.define('permission', { id: { type: INTEGER, primaryKey: true, autoIncrement: true, allowNull: false, comment: '权限ID', }, path: { type: STRING(255), allowNull: false, validate: { notEmpty: { msg: '路由路径不能为空', }, }, comment: '路由路径', }, component: { type: STRING(255), allowNull: true, comment: '组件路径', }, name: { type: STRING(100), allowNull: true, comment: '路由名称', }, redirect: { type: STRING(255), allowNull: true, comment: '重定向路径', }, meta_title: { type: STRING(100), allowNull: true, comment: '页面标题', }, meta_icon: { type: STRING(50), allowNull: true, comment: '图标', }, hidden: { type: TINYINT(1), allowNull: false, defaultValue: 0, validate: { isIn: { args: [[ 0, 1 ]], msg: '隐藏字段只能是0或1', }, }, comment: '是否隐藏(0:显示,1:隐藏)', }, always_show: { type: TINYINT(1), allowNull: false, defaultValue: 0, validate: { isIn: { args: [[ 0, 1 ]], msg: '始终显示字段只能是0或1', }, }, comment: '是否始终显示', }, parent_id: { type: INTEGER, allowNull: true, defaultValue: null, set(value) { this.setDataValue('parent_id', value === 0 ? null : value); }, comment: '父级ID', }, sort: { type: INTEGER, allowNull: false, defaultValue: 0, comment: '排序', }, status: { type: TINYINT(1), allowNull: false, defaultValue: 1, validate: { isIn: { args: [[ 0, 1 ]], msg: '状态只能是0或1', }, }, comment: '状态(0:禁用,1:启用)', }, created_at: { type: DATE, allowNull: false, comment: '创建时间', }, update_at: { type: DATE, allowNull: false, comment: '更新时间', }, type: { type: ENUM('web_pages', 'pda_pages', 'btns', 'api', 'miniprogram_pages', 'block', 'workerstation_pages'), allowNull: false, defaultValue: 'web_pages', comment: '权限类型(web_pages:电脑端页面,pda_pages:PDA页面,btns:按钮权限,api:接口权限,miniprogram_pages:小程序页面,block:功能模块,workerstation_pages:工作台菜单)', }, }, { tableName: 'wms_permission', freezeTableName: true, underscored: true, timestamps: true, paranoid: false, // 明确禁用软删除 createdAt: 'created_at', updatedAt: 'update_at', comment: '权限路由表', indexes: [ { name: 'idx_parent_id', fields: [ 'parent_id' ], }, { name: 'idx_status', fields: [ 'status' ], }, ], }); // 关联关系 - 自关联,构建树形结构 Permission.associate = function() { const { Permission } = app.model; // 子权限 Permission.hasMany(Permission, { foreignKey: 'parent_id', as: 'children', scope: { status: 1, }, }); // 父权限 Permission.belongsTo(Permission, { foreignKey: 'parent_id', as: 'parent', }); }; // 实例方法:获取类型对应的中文名称 Permission.prototype.getTypeName = function() { const typeMap = { web_pages: '电脑端页面', pda_pages: 'PDA页面', btns: '按钮权限', api: '接口权限', miniprogram_pages: '小程序页面', block: '功能模块', workerstation_pages: '工作台菜单', }; return typeMap[this.type] || this.type; }; // 实例方法:检查是否启用 Permission.prototype.isEnabled = function() { return this.status === 1; }; // 实例方法:检查是否隐藏 Permission.prototype.isHidden = function() { return this.hidden === 1; }; // 类方法:根据类型获取权限列表 Permission.findByType = async function(type, includeChildren = false) { const where = { status: 1 }; if (type) { where.type = type; } const options = { where, order: [[ 'sort', 'ASC' ], [ 'id', 'ASC' ]], }; if (includeChildren) { options.include = [{ model: this.associations.children.target, as: 'children', where: { status: 1 }, required: false, order: [[ 'sort', 'ASC' ], [ 'id', 'ASC' ]], }]; } return await this.findAll(options); }; // 类方法:构建树形结构的权限列表 Permission.getTreeByType = async function(type) { const permissions = await this.findByType(type); return this.buildPermissionTree(permissions); }; // 静态方法:构建权限树 Permission.buildPermissionTree = function(permissions) { const permissionMap = new Map(); const rootPermissions = []; // 创建映射 permissions.forEach(permission => { permissionMap.set(permission.id, { id: permission.id, path: permission.path, component: permission.component, name: permission.name, redirect: permission.redirect, meta: { title: permission.meta_title, icon: permission.meta_icon, }, hidden: permission.hidden === 1, alwaysShow: permission.always_show === 1, sort: permission.sort, type: permission.type, children: [], }); }); // 构建树形结构 permissions.forEach(permission => { const node = permissionMap.get(permission.id); if (!permission.parent_id) { rootPermissions.push(node); } else { const parent = permissionMap.get(permission.parent_id); if (parent) { parent.children.push(node); } } }); // 排序 const sortTree = nodes => { if (!nodes || nodes.length === 0) return; nodes.sort((a, b) => { return a.sort - b.sort || a.id - b.id; }); nodes.forEach(node => { if (node.children && node.children.length > 0) { sortTree(node.children); } }); }; sortTree(rootPermissions); return rootPermissions; }; return Permission; };