nav.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import type { RoleCode } from "../api/types";
  2. export type Leaf = { route: string; label: string };
  3. export type Group = { key: string; label: string; children: Leaf[] };
  4. export type NavItem = Group | Leaf;
  5. const isGroup = (n: NavItem): n is Group => "children" in n;
  6. export { isGroup };
  7. /**
  8. * 角色 → 菜单树(册1+册2 范围),同时是**路由级鉴权的唯一真源**(`routesFor`):
  9. * 菜单收口 + `App.tsx::RequireRole` 双层,页面内再由后端 AC 裁决。
  10. * ⚠️ 新增页面必须同步登记进本表,否则会被 `RequireRole` 判为越界而落回首页。
  11. */
  12. const ALL: Record<string, NavItem> = {
  13. gOrg: {
  14. key: "org",
  15. label: "组织与区域",
  16. children: [
  17. { route: "regions", label: "区域管理" },
  18. { route: "operators", label: "运营商" },
  19. { route: "partners", label: "合伙人" },
  20. { route: "entteams", label: "运营团队" },
  21. { route: "opsteams", label: "运维团队" },
  22. { route: "members", label: "成员账号" },
  23. ],
  24. },
  25. gCust: {
  26. key: "cust",
  27. label: "客户与订阅",
  28. children: [
  29. { route: "customers", label: "客户管理" },
  30. { route: "imports", label: "批量导入" },
  31. { route: "subaccounts", label: "分账号" },
  32. ],
  33. },
  34. // 第3册:套餐目录/合同管理/全部订阅(只读视图)。⛔ 挂进 `gCust`——那组 partner 也在用,
  35. // 而「全部订阅」是跨客户只读视图,办理入口在客户详情的套餐订阅管理页。
  36. gPlan: {
  37. key: "plan",
  38. label: "套餐与合同",
  39. children: [
  40. { route: "catalog", label: "套餐目录" },
  41. { route: "contracts", label: "合同管理" },
  42. { route: "subs", label: "全部订阅" },
  43. ],
  44. },
  45. gSys: {
  46. key: "sys",
  47. label: "系统",
  48. children: [{ route: "audit", label: "审计日志" }],
  49. },
  50. };
  51. const leaf = (route: string, label: string): Leaf => ({ route, label });
  52. const NAV: Record<RoleCode, NavItem[]> = {
  53. admin: [leaf("home", "概览"), ALL.gOrg, ALL.gCust, ALL.gPlan, leaf("audit", "审计日志")],
  54. // 运营主账号保留「组织与区域」组(手册 08 册 TC-OR-11 步骤 3:主账号侧栏**有**该组,
  55. // 差异只在子账号),页内写入口与数据按负责范围收窄。
  56. opsMain: [leaf("home", "概览"), ALL.gOrg, ALL.gCust, ALL.gPlan, leaf("audit", "审计日志")],
  57. // 运营子账号:去「组织与区域」整组(TC-AC-05/TC-OR-11),数据范围不变。
  58. opsSub: [leaf("home", "概览"), ALL.gCust, ALL.gPlan],
  59. teamLead: [
  60. leaf("home", "概览"),
  61. { key: "t", label: "我的团队", children: [{ route: "members", label: "成员账号" }] },
  62. ALL.gCust,
  63. // ⛔ 挂 gPlan:套餐目录/合同管理/全部订阅都是运营侧口径
  64. // (订阅册 §5.2「办理入口在客户管理的套餐订阅管理页」,`list_all` 是跨客户运营视图),
  65. // 团队长是客户侧角色,客户详情内也⛔ 渲染该入口。
  66. ],
  67. ops: [leaf("home", "概览"), ALL.gCust, ALL.gPlan],
  68. partner: [leaf("home", "概览"), ALL.gCust],
  69. operator: [
  70. leaf("home", "概览"),
  71. { key: "ro", label: "客户(只读)", children: [{ route: "customers", label: "客户清单" }] },
  72. ],
  73. // 「分账号」是本公司下属分账号的维护页,⛔ 挂在「我的账号」下(该分组原先除它无别项,
  74. // 点开侧栏集合不变、像个空分组)。客户主账号的自助改密(TC-AC-14)是另一个入口,待补。
  75. enterprise: [leaf("home", "概览"), leaf("subaccounts", "分账号")],
  76. // 下属分账号本人:维护分账号是客户主账号的权限(TC-AC-04 步骤 4/TC-CU-62),侧栏不给入口。
  77. sub: [leaf("home", "概览")],
  78. };
  79. export function navFor(role: RoleCode | undefined): NavItem[] {
  80. if (!role) return [];
  81. // 运营商是只读主体:去掉可写分组已在 NAV 中静态表达,这里再兜一层。
  82. return NAV[role] ?? [];
  83. }
  84. /**
  85. * 角色可进入的路由集合(分组展开成叶子)。⚠️ 分组对象没有 `route`,
  86. * ⛔ 只比对 `navFor` 顶层项——那样「组织与区域」「客户与订阅」组内的路由会全判越界。
  87. */
  88. export function routesFor(role: RoleCode | undefined): string[] {
  89. return navFor(role).flatMap((n) => (isGroup(n) ? n.children.map((c) => c.route) : [n.route]));
  90. }
  91. export function firstRoute(role: RoleCode | undefined): string {
  92. const items = navFor(role);
  93. for (const n of items) {
  94. if (isGroup(n)) return n.children[0]!.route;
  95. return n.route;
  96. }
  97. return "home";
  98. }
  99. /** 详情页等非菜单路由的高亮归并。 */
  100. const ALIAS: Record<string, string> = {
  101. customer: "customers",
  102. entteamscope: "entteams",
  103. opsteamscope: "opsteams",
  104. partnerscope: "partners",
  105. importbatch: "imports",
  106. };
  107. export function activeRoute(route: string): string {
  108. return ALIAS[route] ?? route;
  109. }