Files
zyt/identity-admin/scripts/build-kefu-catalog.mjs
T
2026-11-11 13:53:14 +08:00

62 lines
4.2 KiB
JavaScript

import { readFile, writeFile, mkdir } from 'node:fs/promises'
import { createHash } from 'node:crypto'
import { resolve, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import ts from '../web/node_modules/typescript/lib/typescript.js'
// Read code as data: neither the Python module nor the TypeScript routes execute.
const sourceRoot = resolve(process.argv[2] || 'C:/kefu')
const output = fileURLToPath(new URL('../integration/kefu-menu-catalog.json', import.meta.url))
const sourcePaths = ['wechat_rpa/rbac.py', 'admin-web/apps/web-antd/src/router/routes/modules/console.ts']
const [rbac, routes] = await Promise.all(sourcePaths.map(path => readFile(resolve(sourceRoot, path), 'utf8')))
const section = rbac.match(/PERMISSIONS:\s*list\[(?:tuple\[str,\s*str,\s*str\]|Permission)\]\s*=\s*\[([\s\S]*?)\n\]/)?.[1]
if (!section) throw new Error('客服权限目录结构已变化,请核对')
const permissions = [...section.matchAll(/(?:Permission)?\("([a-z][a-z0-9:-]+)",\s*"([^"\n]+)",\s*"([^"\n]+)"(?=\s*[,\)])/g)]
.map(([, code, name, group]) => ({ code, name, group }))
if (!permissions.length || new Set(permissions.map(item => item.code)).size !== permissions.length) throw new Error('客服权限目录缺失或重复')
const source = ts.createSourceFile('console.ts', routes, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS)
const property = (node, key) => node.properties.find(item => ts.isPropertyAssignment(item) && item.name.getText(source).replaceAll(/["']/g, '') === key)?.initializer
const string = node => node && ts.isStringLiteral(node) ? node.text : undefined
const routeLists = new Map()
for (const statement of source.statements) if (ts.isVariableStatement(statement)) for (const item of statement.declarationList.declarations) {
if (ts.isIdentifier(item.name) && item.initializer && ts.isArrayLiteralExpression(item.initializer)) {
routeLists.set(item.name.text, item.initializer)
}
}
const menus = []
function walk(array, base = '', group = '') {
for (const node of array.elements) {
if (!ts.isObjectLiteralExpression(node)) throw new Error('客服路由格式不支持自动提取')
const path = string(property(node, 'path'))
const key = string(property(node, 'name'))
const meta = property(node, 'meta')
if (!path || !key || !meta || !ts.isObjectLiteralExpression(meta)) throw new Error('客服路由字段缺失')
const title = string(property(meta, 'title'))
if (!title) throw new Error('客服路由名称缺失')
const fullPath = path.startsWith('/') ? path : `${base}/${path}`
// The always-available personal landing page is not a role-granted business menu.
if (fullPath === '/group/welcome') continue
const children = property(node, 'children')
if (children) {
const childList = ts.isIdentifier(children) ? routeLists.get(children.text) : children
if (!childList || !ts.isArrayLiteralExpression(childList)) throw new Error(`客服子菜单格式不支持自动提取: ${key}`)
walk(childList, fullPath, title)
}
else {
const authority = property(meta, 'authority')
const codes = authority && ts.isArrayLiteralExpression(authority) ? authority.elements.map(string) : []
if (codes.some(code => !permissions.some(item => item.code === code))) throw new Error(`客服菜单权限不在目录中: ${key}`)
menus.push({ key, name: title, group: group || title, path: fullPath, permissions: codes })
}
}
}
const routeArray = routeLists.get('routes')
if (!routeArray) throw new Error('未找到客服菜单数组')
walk(routeArray)
const catalog = { version: 1, client_id: 'kefu', name: '企微客服', authorization_mode: 'kefu-local-roles',
note: '集团角色仅控制集团菜单入口;客服页面及按钮的实际授权由客服管理员维护。导入不授予任何角色。',
source: sourcePaths.map((path, index) => ({ path, sha256: createHash('sha256').update([rbac, routes][index]).digest('hex') })), menus, permissions }
await mkdir(dirname(output), { recursive: true })
await writeFile(output, JSON.stringify(catalog, null, 2) + '\n')
console.log(`客服目录已生成:${menus.length} 个页面,${permissions.length} 项操作权限;不包含账号或凭据。`)