import type { RoleCode } from "../api/types"; export type Leaf = { route: string; label: string }; export type Group = { key: string; label: string; children: Leaf[] }; export type NavItem = Group | Leaf; const isGroup = (n: NavItem): n is Group => "children" in n; export { isGroup }; /** * 角色 → 菜单树(册1+册2 范围),同时是**路由级鉴权的唯一真源**(`routesFor`): * 菜单收口 + `App.tsx::RequireRole` 双层,页面内再由后端 AC 裁决。 * ⚠️ 新增页面必须同步登记进本表,否则会被 `RequireRole` 判为越界而落回首页。 */ const ALL: Record = { gOrg: { key: "org", label: "组织与区域", children: [ { route: "regions", label: "区域管理" }, { route: "operators", label: "运营商" }, { route: "partners", label: "合伙人" }, { route: "entteams", label: "运营团队" }, { route: "opsteams", label: "运维团队" }, { route: "members", label: "成员账号" }, ], }, gCust: { key: "cust", label: "客户与订阅", children: [ { route: "customers", label: "客户管理" }, { route: "imports", label: "批量导入" }, { route: "subaccounts", label: "分账号" }, ], }, // 第3册:套餐目录/合同管理/全部订阅(只读视图)。⛔ 挂进 `gCust`——那组 partner 也在用, // 而「全部订阅」是跨客户只读视图,办理入口在客户详情的套餐订阅管理页。 gPlan: { key: "plan", label: "套餐与合同", children: [ { route: "catalog", label: "套餐目录" }, { route: "contracts", label: "合同管理" }, { route: "subs", label: "全部订阅" }, ], }, gSys: { key: "sys", label: "系统", children: [{ route: "audit", label: "审计日志" }], }, }; const leaf = (route: string, label: string): Leaf => ({ route, label }); const NAV: Record = { admin: [leaf("home", "概览"), ALL.gOrg, ALL.gCust, ALL.gPlan, leaf("audit", "审计日志")], // 运营主账号保留「组织与区域」组(手册 08 册 TC-OR-11 步骤 3:主账号侧栏**有**该组, // 差异只在子账号),页内写入口与数据按负责范围收窄。 opsMain: [leaf("home", "概览"), ALL.gOrg, ALL.gCust, ALL.gPlan, leaf("audit", "审计日志")], // 运营子账号:去「组织与区域」整组(TC-AC-05/TC-OR-11),数据范围不变。 opsSub: [leaf("home", "概览"), ALL.gCust, ALL.gPlan], teamLead: [ leaf("home", "概览"), { key: "t", label: "我的团队", children: [{ route: "members", label: "成员账号" }] }, ALL.gCust, // ⛔ 挂 gPlan:套餐目录/合同管理/全部订阅都是运营侧口径 // (订阅册 §5.2「办理入口在客户管理的套餐订阅管理页」,`list_all` 是跨客户运营视图), // 团队长是客户侧角色,客户详情内也⛔ 渲染该入口。 ], ops: [leaf("home", "概览"), ALL.gCust, ALL.gPlan], partner: [leaf("home", "概览"), ALL.gCust], operator: [ leaf("home", "概览"), { key: "ro", label: "客户(只读)", children: [{ route: "customers", label: "客户清单" }] }, ], // 「分账号」是本公司下属分账号的维护页,⛔ 挂在「我的账号」下(该分组原先除它无别项, // 点开侧栏集合不变、像个空分组)。客户主账号的自助改密(TC-AC-14)是另一个入口,待补。 enterprise: [leaf("home", "概览"), leaf("subaccounts", "分账号")], // 下属分账号本人:维护分账号是客户主账号的权限(TC-AC-04 步骤 4/TC-CU-62),侧栏不给入口。 sub: [leaf("home", "概览")], }; export function navFor(role: RoleCode | undefined): NavItem[] { if (!role) return []; // 运营商是只读主体:去掉可写分组已在 NAV 中静态表达,这里再兜一层。 return NAV[role] ?? []; } /** * 角色可进入的路由集合(分组展开成叶子)。⚠️ 分组对象没有 `route`, * ⛔ 只比对 `navFor` 顶层项——那样「组织与区域」「客户与订阅」组内的路由会全判越界。 */ export function routesFor(role: RoleCode | undefined): string[] { return navFor(role).flatMap((n) => (isGroup(n) ? n.children.map((c) => c.route) : [n.route])); } export function firstRoute(role: RoleCode | undefined): string { const items = navFor(role); for (const n of items) { if (isGroup(n)) return n.children[0]!.route; return n.route; } return "home"; } /** 详情页等非菜单路由的高亮归并。 */ const ALIAS: Record = { customer: "customers", entteamscope: "entteams", opsteamscope: "opsteams", partnerscope: "partners", importbatch: "imports", }; export function activeRoute(route: string): string { return ALIAS[route] ?? route; }