122 lines
4.0 KiB
JavaScript
122 lines
4.0 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* 为群选择 / 群成员选择下拉接口创建 API 权限记录并授权。
|
||
*
|
||
* 权限明细(固定 ID,当前迁移内 max_id=565):
|
||
* 566 — inbox.group.quickList API:群快速查询
|
||
* 567 — inbox.group.members API:群成员列表
|
||
*
|
||
* 角色授权(客服组-主管及以上):
|
||
* 26(仓库组-主管) → 28(客服组-主管) → 29(生产组-主管) → 8(经理)
|
||
* 超级管理员(7)/仓库管理员(2) 已持有 * 通配符,自动拥有,无需处理
|
||
*
|
||
* 幂等:INSERT IGNORE 创建权限记录;角色权限数组追加后去重排序,已存在跳过。
|
||
*/
|
||
|
||
const TABLE = 'wms_permission';
|
||
const ROLE_TABLE = 'wms_role';
|
||
const COLUMNS = 'id, path, component, name, redirect, meta_title, meta_icon, hidden, always_show, parent_id, type, sort, status, created_at, update_at';
|
||
|
||
/** 权限ID → 授权角色ID列表(不含通配符角色) */
|
||
const PERMISSION_ROLES = {
|
||
566: [ 26, 28, 29, 8 ],
|
||
567: [ 26, 28, 29, 8 ],
|
||
};
|
||
|
||
module.exports = {
|
||
// eslint-disable-next-line no-unused-vars
|
||
async up(queryInterface, Sequelize) {
|
||
// ── 1. 创建 API 权限记录(固定 ID)──
|
||
const apis = [
|
||
[ 566, 'inbox.group.quickList', 'API:群快速查询' ],
|
||
[ 567, 'inbox.group.members', 'API:群成员列表' ],
|
||
];
|
||
|
||
for (const [ id, path, metaTitle ] of apis) {
|
||
await queryInterface.sequelize.query(
|
||
`INSERT IGNORE INTO \`${TABLE}\` (${COLUMNS}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())`,
|
||
{ replacements: [ id, path, null, path, null, metaTitle, null, 0, 0, null, 'api', 0, 1 ] }
|
||
);
|
||
}
|
||
|
||
// ── 2. 角色授权注入 ──
|
||
for (const permId of Object.keys(PERMISSION_ROLES).map(Number)) {
|
||
for (const roleId of PERMISSION_ROLES[permId]) {
|
||
const rows = await queryInterface.sequelize.query(
|
||
`SELECT id, permissions FROM \`${ROLE_TABLE}\` WHERE id = ?`,
|
||
{ replacements: [ roleId ], type: queryInterface.sequelize.QueryTypes.SELECT }
|
||
);
|
||
const record = Array.isArray(rows) ? rows[0] : null;
|
||
if (!record) {
|
||
continue;
|
||
}
|
||
|
||
// 通配符角色跳过
|
||
if (record.permissions === '*') {
|
||
continue;
|
||
}
|
||
|
||
let perms;
|
||
try {
|
||
perms = typeof record.permissions === 'string'
|
||
? JSON.parse(record.permissions)
|
||
: record.permissions;
|
||
} catch (e) {
|
||
perms = null;
|
||
}
|
||
|
||
if (!Array.isArray(perms)) {
|
||
continue;
|
||
}
|
||
|
||
if (!perms.includes(permId)) {
|
||
perms.push(permId);
|
||
perms.sort((a, b) => Number(a) - Number(b));
|
||
await queryInterface.sequelize.query(
|
||
`UPDATE \`${ROLE_TABLE}\` SET permissions = ? WHERE id = ?`,
|
||
{ replacements: [ JSON.stringify(perms), roleId ] }
|
||
);
|
||
}
|
||
}
|
||
}
|
||
},
|
||
|
||
// eslint-disable-next-line no-unused-vars
|
||
async down(queryInterface, Sequelize) {
|
||
await queryInterface.sequelize.query(
|
||
'DELETE FROM `wms_permission` WHERE id IN (566, 567)'
|
||
);
|
||
|
||
const permIds = [ 566, 567 ];
|
||
for (const roleId of Object.values(PERMISSION_ROLES).flat()) {
|
||
const rows = await queryInterface.sequelize.query(
|
||
`SELECT id, permissions FROM \`${ROLE_TABLE}\` WHERE id = ?`,
|
||
{ replacements: [ roleId ], type: queryInterface.sequelize.QueryTypes.SELECT }
|
||
);
|
||
const record = Array.isArray(rows) ? rows[0] : null;
|
||
if (!record || record.permissions === '*') {
|
||
continue;
|
||
}
|
||
|
||
let perms;
|
||
try {
|
||
perms = typeof record.permissions === 'string'
|
||
? JSON.parse(record.permissions)
|
||
: record.permissions;
|
||
} catch (e) {
|
||
perms = null;
|
||
}
|
||
if (!Array.isArray(perms) || !perms.some(p => permIds.includes(p))) {
|
||
continue;
|
||
}
|
||
|
||
perms = perms.filter(p => !permIds.includes(p));
|
||
await queryInterface.sequelize.query(
|
||
`UPDATE \`${ROLE_TABLE}\` SET permissions = ? WHERE id = ?`,
|
||
{ replacements: [ JSON.stringify(perms), roleId ] }
|
||
);
|
||
}
|
||
},
|
||
};
|