'use strict'; /** * 权限模型,对应 permissions 表。 * * 以「菜单」作为权限分类,每个菜单下两种权限子类: * - scope=1 集团全部:该菜单下的能力对整个集团(全部主体)生效 * - scope=2 所在公司:该菜单下的能力仅对当前所在公司(当前主体)生效 * @param {Egg.Application} app egg application * @return {object} permission model */ module.exports = app => { const { BIGINT, STRING, TINYINT, DATE } = app.Sequelize; const Permission = app.model.define('permission', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, menu_id: { type: BIGINT.UNSIGNED, allowNull: false, comment: '所属菜单(权限分类)ID' }, name: { type: STRING(100), allowNull: false, comment: '权限名称' }, code: { type: STRING(100), allowNull: false, comment: '权限标识(全局唯一)' }, scope: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '权限范围:1集团全部 2所在公司' }, status: { type: TINYINT, allowNull: false, defaultValue: 1, comment: '状态:1启用 0禁用' }, remark: { type: STRING(255), comment: '备注' }, created_at: { type: DATE, comment: '创建时间' }, updated_at: { type: DATE, comment: '更新时间' }, }, { tableName: 'permissions', comment: '权限表', }); Permission.associate = () => { app.model.Permission.belongsTo(app.model.Menu, { foreignKey: 'menu_id', as: 'menu' }); app.model.Permission.belongsToMany(app.model.Role, { through: app.model.RolePermission, foreignKey: 'permission_id', otherKey: 'role_id', as: 'roles', }); }; return Permission; };