87 lines
3.2 KiB
JavaScript
87 lines
3.2 KiB
JavaScript
'use strict';
|
|
|
|
module.exports = {
|
|
/**
|
|
* 新增打印电脑模块 API 权限,并授权客服主管及以上角色。
|
|
* @param {import('sequelize').QueryInterface} queryInterface 查询接口
|
|
* @param {import('sequelize')} Sequelize Sequelize 实例
|
|
* @return {Promise<void>} 无返回值
|
|
*/
|
|
async up(queryInterface, Sequelize) {
|
|
const now = new Date();
|
|
|
|
// 1. 插入 3 条 API 权限
|
|
const permissions = [
|
|
{ path: 'print_computer.list', name: '打印电脑-列表', type: 'api', status: 1, created_at: now, update_at: now },
|
|
{ path: 'print_computer.update', name: '打印电脑-更新', type: 'api', status: 1, created_at: now, update_at: now },
|
|
{ path: 'print_computer.delete', name: '打印电脑-删除', type: 'api', status: 1, created_at: now, update_at: now },
|
|
];
|
|
|
|
await queryInterface.bulkInsert('wms_permission', permissions);
|
|
|
|
// 2. 查询刚插入的权限 ID
|
|
const [ rows ] = await queryInterface.sequelize.query(
|
|
'SELECT id FROM wms_permission WHERE path IN (\'print_computer.list\', \'print_computer.update\', \'print_computer.delete\') ORDER BY id'
|
|
);
|
|
const ids = rows.map(r => r.id);
|
|
if (ids.length !== 3) {
|
|
throw new Error(`权限插入异常:期望 3 条,实际 ${ids.length} 条`);
|
|
}
|
|
|
|
// 3. 授权客服主管及以上角色(排除 permissions='*' 的超级管理员角色)
|
|
const roleIds = [ 2, 5, 7, 8, 22, 26, 28, 29 ];
|
|
for (const permId of ids) {
|
|
await queryInterface.sequelize.query(`
|
|
UPDATE wms_role
|
|
SET permissions = JSON_ARRAY_APPEND(permissions, '$', CAST(${permId} AS UNSIGNED)),
|
|
updated_at = NOW()
|
|
WHERE id IN (${roleIds.join(',')})
|
|
AND permissions IS NOT NULL
|
|
AND permissions != '*'
|
|
AND permissions != '[]'
|
|
AND JSON_VALID(permissions) = 1
|
|
AND JSON_CONTAINS(permissions, '${permId}', '$') = 0
|
|
`);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 回滚:删除权限记录并从角色权限中移除。
|
|
* @param {import('sequelize').QueryInterface} queryInterface 查询接口
|
|
* @param {import('sequelize')} Sequelize Sequelize 实例
|
|
* @return {Promise<void>} 无返回值
|
|
*/
|
|
async down(queryInterface, Sequelize) {
|
|
const paths = [ 'print_computer.list', 'print_computer.update', 'print_computer.delete' ];
|
|
|
|
// 1. 查询权限 ID
|
|
const [ rows ] = await queryInterface.sequelize.query(
|
|
`SELECT id FROM wms_permission WHERE path IN ('${paths.join("','")}')`
|
|
);
|
|
const ids = rows.map(r => r.id);
|
|
|
|
// 2. 从角色权限中移除
|
|
const roleIds = [ 2, 5, 7, 8, 22, 26, 28, 29 ];
|
|
for (const permId of ids) {
|
|
await queryInterface.sequelize.query(`
|
|
UPDATE wms_role
|
|
SET permissions = JSON_REMOVE(
|
|
permissions,
|
|
JSON_UNQUOTE(JSON_SEARCH(permissions, 'one', '${permId}'))
|
|
),
|
|
updated_at = NOW()
|
|
WHERE id IN (${roleIds.join(',')})
|
|
AND permissions IS NOT NULL
|
|
AND permissions != '*'
|
|
AND JSON_VALID(permissions) = 1
|
|
AND JSON_CONTAINS(permissions, '${permId}', '$') = 1
|
|
`);
|
|
}
|
|
|
|
// 3. 删除权限记录
|
|
await queryInterface.bulkDelete('wms_permission', {
|
|
path: { [Sequelize.Op.in]: paths },
|
|
});
|
|
},
|
|
};
|