'use strict'; /** * 为售后模块新增页面权限授权,并创建财务组角色。 * * 操作: * 1. 新建财务组角色(role_code='finance') * 2. 为以下角色追加页面权限 524 (afterSale.workOrder)、525 (afterSale.workOrder.list): * - 经理 (role_code=2) * - 客服组-主管 (role_code=05) * - 客服组-员工 (role_code=04) * - 仓库组-主管 (role_code=warehouse) * - 仓库组-员工 (role_code=warehouse_role) * - 生产组-主管 (role_code=03) * - 生产组-拣货 (role_code=021) * * 幂等设计: * - 财务组角色使用 INSERT IGNORE,已存在则跳过 * - 权限追加使用 JSON_ARRAY_APPEND,不重复添加 */ const NEW_PAGE_IDS = [524, 525]; // 财务组基础权限:block(518) + 所有web_pages(519,520,521,524,525) + 只读API(522,508) const FINANCE_PERMISSIONS = [518, 519, 520, 521, 524, 525, 522, 508]; module.exports = { // eslint-disable-next-line no-unused-vars async up(queryInterface, Sequelize) { const { sequelize } = queryInterface; // ── 1. 创建财务组角色(如果不存在)── await sequelize.query(` INSERT IGNORE INTO wms_role (role_code, role_name, description, role_type, scope, is_system, status, pid, created_by, permissions, created_at, updated_at) VALUES ('finance', '财务组', '售后模块财务审核角色', 1, 1, 1, 1, 8, 1, ?, NOW(), NOW()) `, { replacements: [JSON.stringify(FINANCE_PERMISSIONS)], }); // ── 2. 为已有角色追加页面权限 ── const targetRoleCodes = ['2', '05', '04', 'warehouse', 'warehouse_role', '03', '021']; for (const roleCode of targetRoleCodes) { for (const permId of NEW_PAGE_IDS) { // 仅当该角色的 permissions 数组中不包含此权限ID时才追加 // JSON_CONTAINS 检查是否已存在 // 注意:permissions 列为 LONGTEXT,JSON 函数需显式 CAST // 安全守卫:permissions 必须是合法 JSON 数组(以 [ 开头且非 NULL), // 避免在非法 JSON(如旧版范围格式 [4-121])或 NULL 上执行 JSON_ARRAY_APPEND 导致权限被清空。 await sequelize.query( `UPDATE wms_role SET permissions = JSON_ARRAY_APPEND(CAST(permissions AS JSON), '$', CAST(? AS UNSIGNED)), updated_at = NOW() WHERE role_code = ? AND permissions IS NOT NULL AND permissions != '' AND permissions != '*' AND JSON_VALID(permissions) = 1 AND LEFT(permissions, 1) = '[' AND JSON_CONTAINS(CAST(permissions AS JSON), CAST(? AS JSON), '$') = 0`, { replacements: [permId, roleCode, String(permId)] } ); } } }, // eslint-disable-next-line no-unused-vars async down(queryInterface, Sequelize) { const { sequelize } = queryInterface; // ── 1. 移除财务组角色 ── await sequelize.query( "DELETE FROM wms_role WHERE role_code = 'finance'" ); // ── 2. 从各角色移除页面权限 ── const targetRoleCodes = ['2', '05', '04', 'warehouse', 'warehouse_role', '03', '021']; for (const roleCode of targetRoleCodes) { for (const permId of NEW_PAGE_IDS) { // 从 JSON 数组中移除指定值(MySQL 5.7+ 无直接 JSON_REMOVE_BY_VALUE,用 JSON_SEARCH) // 注意:permissions 列为 LONGTEXT,JSON 函数需显式 CAST // 安全守卫:同 up(),防止在非法 JSON 上操作 await sequelize.query( `UPDATE wms_role SET permissions = JSON_REMOVE( CAST(permissions AS JSON), REPLACE(JSON_UNQUOTE(JSON_SEARCH(CAST(permissions AS JSON), 'one', CAST(? AS CHAR), NULL, '$')), '', '') ), updated_at = NOW() WHERE role_code = ? AND permissions IS NOT NULL AND permissions != '' AND permissions != '*' AND JSON_VALID(permissions) = 1 AND LEFT(permissions, 1) = '[' AND JSON_CONTAINS(CAST(permissions AS JSON), CAST(? AS JSON), '$') = 1`, { replacements: [permId, roleCode, String(permId)] } ); } } }, };