/** 用户菜单授权数据解析模型(任务 49):permission-menus 扁平项 → 嵌套树 + 用户已授权 id。 */ import { unwrap } from '../../api/envelope.ts' /** 后台 Vue 控制台菜单类型(与 /api/admin/current-user/menus 一致)。 */ export const ADMIN_MENU_TYPE = 'admin' /** 前端客户端菜单类型(桌面端功能模块,如视频/前端工具/运营工具/后勤工具)。 */ export const APP_MENU_TYPE = 'app' /** 授权用可点选菜单节点(携带后端 numeric column id,用于提交授权)。 */ export interface MenuOptionNode { id: number /** column_key,稳定权限标识;缺省用 id 字符串兜底。 */ key: string name: string sort: number parentId: number | null /** 所属菜单类型(admin 后台 / app 前端客户端),提交时按类型分区落库。 */ type: string /** * 当前操作者无权授予(后端 grantable=false)时置灰。授权树对非超管只下发自己已有 * 的菜单,因此这里剩下的只有「为保留层级而带上的祖先分组」:展示但不可勾,提交时剔除。 */ disabled?: boolean children?: MenuOptionNode[] } function numberOrNull(value: unknown): number | null { return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : null } /** 把单条 PermissionMenuItemVo(snake_case) 归一为授权节点;缺 id 视为无效。 */ export function parsePermissionMenuItem(raw: unknown, type = ''): MenuOptionNode | null { if (!raw || typeof raw !== 'object') return null const record = raw as Record const id = numberOrNull(record.id) if (id === null) return null const parentId = numberOrNull(record.parent_id) const sortRaw = numberOrNull(record.sort_order) const columnKey = typeof record.column_key === 'string' ? record.column_key.trim() : '' return { id, key: columnKey || String(id), name: typeof record.name === 'string' && record.name.trim() ? record.name.trim() : '未命名菜单', sort: sortRaw === null ? 0 : sortRaw, parentId: parentId === null ? null : parentId, type, // 缺省(菜单管理页等未标记的接口)按可授予处理,保持旧行为 disabled: record.grantable === false, } } /** 归一化 permission-menus 响应(信封或裸数组)为扁平授权节点列表。 */ export function parseMenuOptionList(payload: unknown, type = ''): MenuOptionNode[] { const data = unwrap(payload) if (!Array.isArray(data)) return [] return data .map((raw) => parsePermissionMenuItem(raw, type)) .filter((node): node is MenuOptionNode => node !== null) } /** 按 parent_id 组装嵌套树;同级按 (sort,key) 升序,父缺失的节点按根处理。 */ export function buildMenuOptionTree(items: MenuOptionNode[]): MenuOptionNode[] { const nodes = new Map() for (const item of items) { nodes.set(item.id, { ...item, children: [] }) } const roots: MenuOptionNode[] = [] for (const item of items) { const node = nodes.get(item.id) if (!node) continue const parent = item.parentId === null ? undefined : nodes.get(item.parentId) if (parent) { parent.children!.push(node) } else { roots.push(node) } } const order = (a: MenuOptionNode, b: MenuOptionNode) => a.sort - b.sort || a.key.localeCompare(b.key) const sortRecursive = (nodes: MenuOptionNode[]) => { nodes.sort(order) for (const node of nodes) node.children?.length && sortRecursive(node.children) } sortRecursive(roots) for (const item of items) { const node = nodes.get(item.id) if (node && !node.children?.length) delete node.children } return roots } /** 解包用户已授权 columnIds(data.columnIds)→ 去重升序正整数。 */ export function parseUserGrantedColumnIds(payload: unknown): number[] { const data = unwrap(payload) const record = data && typeof data === 'object' ? (data as Record) : null if (!record || !Array.isArray(record.columnIds)) return [] return normalizeMenuIds(record.columnIds) } /** 任意输入 id 集合归一为去重升序正整数(镜像后端 normalizeColumnIds)。 */ export function normalizeMenuIds(raw: unknown): number[] { if (!Array.isArray(raw)) return [] const ids = new Set() for (const value of raw) { const id = numberOrNull(value) if (id !== null && id >= 1) ids.add(id) } return [...ids].sort((a, b) => a - b) } /** 提交授权请求体(任务 50):{columnIds} 与 Java UserColumnPermissionUpdateRequest 对齐。 */ export function buildUserColumnPermissionPayload(raw: unknown): { columnIds: number[] } { return { columnIds: normalizeMenuIds(raw) } } /** * 把权限树勾选 id 压缩为后端意义上的“直接授权最小集”:父级已勾选时后代无需 * 重复提交(后端读取/校验时父级授权会向全部后代展开,与旧 admin.html“勾父清 * 后代”语义一致,避免 user_column_permission 存储冗余行)。未勾选父级但单独 * 勾选子级的部分授权原样保留。 * *

置灰节点(disabled,即操作者无权授予、只为还原层级而带上的祖先分组)一律剔除, * 且其勾选态不参与“父级覆盖后代”的判断:那是子节点联动派生出来的,不代表用户意图。 * 提交它等于把整个分组授出去,会连带授予树上未下发(因此看不见)的后代菜单, * 同时被后端 ensureGrantable 判越权、整单 403 回滚。 */ export function compactDirectGrantIds(checkedIds: unknown, tree: MenuOptionNode[]): number[] { const ids = normalizeMenuIds(checkedIds) if (ids.length === 0 || tree.length === 0) return ids const idSet = new Set(ids) /** 因“有已勾选祖先”而被父级覆盖、无需直接授权的 id。 */ const coveredByAncestor = new Set() /** 操作者无权授予、不得出现在提交里的 id。 */ const ungrantable = new Set() const walk = (nodes: MenuOptionNode[], ancestorChecked: boolean): void => { for (const node of nodes) { if (node.disabled) ungrantable.add(node.id) if (ancestorChecked) { coveredByAncestor.add(node.id) if (node.children?.length) walk(node.children, true) continue } if (node.children?.length) walk(node.children, !node.disabled && idSet.has(node.id)) } } walk(tree, false) return ids.filter((id) => !ungrantable.has(id) && !coveredByAncestor.has(id)) }