adapter.ts
6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* 后端权限数据 → 前端标准化权限点集合 的【适配器】。
*
* 当前后端契约未定,这里实现几种常见形态;将来后端返回格式变化,
* 只需改本文件(或新增 normalizeXxx 并切换调用),上层 PermButton / canPoint 不受影响。
*/
import type { PermissionMatrix, ResourceKey, RoleKey, PermissionState } from '../model';
import type { MenuNode } from '../../api/modules/menu';
/**
* 权限码归一(适配器)。
*
* 后端下发的权限码已是冒号分隔(如 store:list:view),且资源命名与前端一致
* (store:list / system:user / store:import / store:approve / system:role),
* 前端直接以后端格式为唯一真源,不再做「冒号→点号」或命名转换。
* 故此处原样透传:toFrontendPoint(x) === x。
*/
function toFrontendPoint(code: string): string {
return code;
}
/* ---------------- 菜单树:后端 /system/menu/tree/current → 前端菜单结构 ---------------- */
/** 前端菜单项(path/label/icon 供渲染,resource 供权限与面包屑) */
export interface MenuItemDef {
path: string;
label: string;
/** 图标名(后端下发,如 ShopOutlined);为 null 时由渲染层兜底默认图标 */
icon: string | null;
resource?: ResourceKey;
}
export interface MenuGroupDef {
group: string;
children: MenuItemDef[];
/**
* true:不渲染分组标题,children 直接平铺为该层级的菜单项。
* 用于「工作台」这类只有一个入口的分组——挂一个独占一行的标题纯属噪音。
* 后端菜单树不会下发该字段,仅由前端固定组与兜底 menu.json 标注意图。
*/
hideTitle?: boolean;
}
/**
* 菜单的 path 与 icon 现在由后端 /system/menu/tree/current 直接下发
* (node.route / node.icon),前端不再维护 permCode → path/icon 的映射表。
* 后端在菜单树里登记 route,前端无需改动即可渲染对应页面菜单。
*/
/**
* 后端菜单树 → 前端菜单分组结构。
*
* 规则:
* - 只取 nodeType 1(分组)下的 nodeType 2(页面);nodeType 3 是按钮操作点,不进菜单。
* - status !== 1(停用)的节点丢弃。
* - path 与 icon 直接取自后端下发的 node.route / node.icon,前端不再做映射。
* - permCode 与当前账号持有的权限点取交集(/current 后端已按角色过滤,这里作双保险再过滤一次)。
* - node.route 缺失 → 跳过并告警(点了会 404,不应渲染)。
* - 分组/页面均按 sort 升序。
*
* `pathByResource` 是「权限资源 → 前端实际持有的页面路径」清单(来自兜底 menu.json),
* 只在**后端数据缺陷**时使用:同一 route 被多个页面节点复用 → 点哪个菜单都进同一页。
* 路由的真源仍然是后端的 node.route;这里只在复用发生时兜底,并在控制台点名,不改正常数据的行为。
*/
export function buildMenus(
tree: MenuNode[],
owned: Iterable<string>,
pathByResource?: ReadonlyMap<string, string>,
): MenuGroupDef[] {
const points = new Set(owned);
const groups: MenuGroupDef[] = [];
// 先统计 route 复用情况(只统计会进菜单的节点)
const routeUses = new Map<string, number>();
(tree ?? []).forEach((g) => {
if (g.nodeType !== 1 || g.status !== 1) return;
(g.children ?? []).forEach((n) => {
if (n.nodeType === 2 && n.status === 1 && n.route) {
routeUses.set(n.route, (routeUses.get(n.route) ?? 0) + 1);
}
});
});
(tree ?? [])
.filter((n) => n.nodeType === 1 && n.status === 1)
.sort((a, b) => a.sort - b.sort)
.forEach((g) => {
const children: MenuItemDef[] = [];
(g.children ?? [])
.filter((n) => n.nodeType === 2 && n.status === 1)
.sort((a, b) => a.sort - b.sort)
.forEach((n) => {
const code = n.permCode;
if (!code) return;
// 归一为前端点号后与权限点集合比较(points 已是归一后的点号形态)
const point = toFrontendPoint(code);
if (!points.has(point)) return;
// 路由由后端树直接下发;缺失则跳过(不应渲染一个点了会 404 的菜单项)
if (!n.route) {
console.warn(`[menu] 后端菜单「${n.label}」(${code}) 未下发 route,已跳过`);
return;
}
const i = point.lastIndexOf(':');
const resource = (i > 0 ? point.slice(0, i) : point) as ResourceKey;
let path = n.route;
const uses = routeUses.get(path) ?? 0;
if (uses > 1) {
const repaired = pathByResource?.get(resource);
if (repaired) {
console.error(
`[menu] 后端菜单「${n.label}」(${code}) 的 route ${path} 被 ${uses} 个页面复用,` +
`已回落到前端页面清单的 ${repaired}。请把后端菜单节点的 route 改成互不相同。`,
);
path = repaired;
}
}
children.push({ path, label: n.label, icon: n.icon ?? null, resource });
});
if (children.length) groups.push({ group: g.label, children });
});
return groups;
}
/** 把任意后端返回归一为扁平权限点数组 */
export function normalizePermissions(raw: unknown): string[] {
if (!raw) return [];
// 1) 已是扁平数组:['store:list:view', 'store:approve:pass']
if (Array.isArray(raw)) {
return raw.filter((x): x is string => typeof x === 'string').map(toFrontendPoint);
}
// 2) 登录/info 的 permCodes 为权威字段,空值也不回退到旧权限字段。
if (typeof raw === 'object') {
const o = raw as Record<string, unknown>;
if (Object.hasOwn(o, 'permCodes')) return normalizePermissions(o.permCodes);
if (Array.isArray(o.permissions)) return normalizePermissions(o.permissions);
if (Array.isArray(o.list)) return normalizePermissions(o.list);
if (o.data && typeof o.data === 'object') {
const d = o.data as Record<string, unknown>;
return normalizePermissions(d);
}
}
return [];
}
/**
* 开发期兜底:用现有角色矩阵推导出权限点(view=页面资源,ck.on 额外给 :manage)。
* 仅用于未接入后端时本地演示;生产应由后端 permissions 取代(调用 loadPermissions)。
*/
export function matrixToPoints(matrix: PermissionMatrix, role: RoleKey): string[] {
const out: string[] = [];
(Object.keys(matrix) as ResourceKey[]).forEach((res) => {
const state: PermissionState = matrix[res]?.[role] ?? 'na';
if (state === 'na') return;
out.push(res);
if (state === 'ck.on') out.push(`${res}:manage`);
});
return out;
}