开发者报障(截图):「为什么路由又乱了?参考主系统现在的路由配置为模板,
后续无论是迁移重新部署到新的电脑上就好不变了」。
根因(两条叠加,都是"结构没有单一权威"):
1. 布局**按分公司存库**(system_configs.nav_menu_layout)⇒ 保存过就永久覆盖代码默认,
每个环境各自保留自己那份旧结构(本地/测试 8444/将来新机器各不同);
2. 侧边栏缓存指纹 `catalogSignature()` 只取菜单 key 列表 ⇒ 只改**结构**时指纹不变,
老会话继续显示旧布局,刷新也不好。
⇒ 现象:顶层 11 项(订单管理/采购管理/配送管理/售后管理/异常管理 各自成组),
而主系统现在只有 6 项顶层。
模板(读主系统库 system_configs.nav_menu_layout 的 BR002 行):
数据看板 / 订单录入 + 业务流程 / 基础数据 / 财务结算 / 系统管理;
作业类菜单(订单/采购/验收/分拣/配送/售后/异常)全部收在「业务流程」里。
差异逐条核过:我方缺 orders-allocation/customers-alias-library/finance-invoices(目录里没有 ⇒ 自然缺席);
orders-price-qty(跨系统改价申请)按用户 2026-10-11 指定留在「供应商备货表」下方。
落地:
· 代码默认 = 模板(DEFAULT_GROUPS/DEFAULT_STANDALONE/DEFAULT_ORDER 重写,顶层只剩 6 项);
· 新增 applyBusinessFlowGroupCanonicalOrder():老布局每次加载自动把作业类菜单收进「业务流程」
并按模板排序、清掉空掉的旧分组(含订单管理/采购管理);顶层顺序由 CANONICAL_TOP_LEVEL_ORDER
决定,分组按 id **或规范显示名**两头认(老租户用过 grp-products/grp-suppliers 这类别名 id);
· 归位放在**代码里、每次加载都跑**(不写一次性 SQL)⇒ 换库/换机/重新部署结果都一样;
· 缓存指纹加入 LAYOUT_TEMPLATE_VERSION='v2' ⇒ 所有端(含老会话 sessionStorage)自动重建一次;
· 不入侵管理员:自建分组与其相对顺序保留(排在模板四组之后)、自定义显示名保留、
分组内非规范条目留在规范条目之后;唯一显示名收敛是财务分组**仅当仍叫旧默认「财务管理」**时改为「财务结算」。
测试:menuLayout.newMenu 10 例(新增 5:顶层 6 项 / 老布局归位 / 业务流程内部顺序 /
自建分组与自定义名不被入侵 / 幂等);原「保存过的顺序不能被重排」一条与本决策直接冲突,
已按新口径改写并写明原因。前端全量 714 用例全绿;tsc 通过;3021 bundle 已重建。
设计:docs/design/20261027-导航菜单按主系统模板收敛.md
159 lines
5.2 KiB
TypeScript
159 lines
5.2 KiB
TypeScript
import {
|
||
buildRuntimeMenuTree,
|
||
type NavMenuLayoutConfig,
|
||
type RuntimeMenuItem,
|
||
} from './menuLayout';
|
||
import { MENU_CATALOG } from '../config/menuCatalog';
|
||
import { loadNavMenuLayoutForScope } from './menuLayoutLoad';
|
||
import { resolveSidebarMenuScope } from './menuLayoutBranch';
|
||
|
||
export interface CachedSidebarMenu {
|
||
layout: NavMenuLayoutConfig;
|
||
menuItems: RuntimeMenuItem[];
|
||
fromDirectRecord: boolean;
|
||
}
|
||
|
||
interface StoredSidebarMenu {
|
||
layout: NavMenuLayoutConfig;
|
||
fromDirectRecord: boolean;
|
||
/** 写入缓存时的菜单目录指纹(见 catalogSignature) */
|
||
catalogSignature?: string;
|
||
}
|
||
|
||
/**
|
||
* **布局模板版本**:改动"顶层结构/分组归属"时必须 +1 —— 它参与 `catalogSignature()`,
|
||
* 从而让所有端(含老会话的 sessionStorage)**重建一次**。
|
||
* 为什么单独要它:`catalogSignature` 只看菜单目录的 key 列表,**结构变化它察觉不到**
|
||
* ⇒ 不加就会出现"代码已按主系统模板收敛,用户界面仍是旧的 11 个顶层"。
|
||
* v2 = 2026-10-27 按主系统模板收敛(顶层只留 数据看板/订单录入 + 业务流程/基础数据/财务结算/系统管理)。
|
||
*/
|
||
export const LAYOUT_TEMPLATE_VERSION = 'v2';
|
||
|
||
const SESSION_STORAGE_KEY = 'nav-sidebar-menu-cache-v2';
|
||
|
||
const cache = new Map<string, CachedSidebarMenu>();
|
||
const inflight = new Map<string, Promise<CachedSidebarMenu>>();
|
||
|
||
/**
|
||
* 菜单目录指纹:目录(MENU_CATALOG)一变,指纹就变 ⇒ 旧缓存自动作废。
|
||
*
|
||
* 为什么需要它(2026-10-11 实测):侧边栏把"合并后的布局"缓存在 sessionStorage,**无 TTL**;
|
||
* 新版本加了菜单项时,老会话里的缓存仍是没有该项的旧布局 ⇒ 用户看不到新入口
|
||
* (刷新也不会好,因为 sessionStorage 跨刷新存在)。
|
||
*/
|
||
export function catalogSignature(): string {
|
||
return `${LAYOUT_TEMPLATE_VERSION}::${MENU_CATALOG.map((entry) => entry.menuKey).sort().join('|')}`;
|
||
}
|
||
|
||
export function sidebarMenuCacheKey(profileId: string, scope: string): string {
|
||
return `${profileId}:${scope}`;
|
||
}
|
||
|
||
function createCacheEntry(layout: NavMenuLayoutConfig, fromDirectRecord: boolean): CachedSidebarMenu {
|
||
return {
|
||
layout,
|
||
menuItems: buildRuntimeMenuTree(layout),
|
||
fromDirectRecord,
|
||
};
|
||
}
|
||
|
||
function readSessionCache(key: string): CachedSidebarMenu | undefined {
|
||
try {
|
||
const raw = sessionStorage.getItem(SESSION_STORAGE_KEY);
|
||
if (!raw) return undefined;
|
||
const all = JSON.parse(raw) as Record<string, StoredSidebarMenu>;
|
||
const entry = all[key];
|
||
if (!entry?.layout) return undefined;
|
||
// 目录变了(发过新版)⇒ 视为未命中,强制重新拉取并按新默认布局合并。
|
||
// 没有指纹的条目 = 本次修复之前写入的旧缓存,同样作废(否则老会话仍看不到新菜单)。
|
||
if (entry.catalogSignature !== catalogSignature()) return undefined;
|
||
return createCacheEntry(entry.layout, Boolean(entry.fromDirectRecord));
|
||
} catch {
|
||
return undefined;
|
||
}
|
||
}
|
||
|
||
function writeSessionCache(key: string, entry: CachedSidebarMenu) {
|
||
try {
|
||
const raw = sessionStorage.getItem(SESSION_STORAGE_KEY);
|
||
const all = raw ? (JSON.parse(raw) as Record<string, StoredSidebarMenu>) : {};
|
||
all[key] = {
|
||
layout: entry.layout,
|
||
fromDirectRecord: entry.fromDirectRecord,
|
||
catalogSignature: catalogSignature(),
|
||
};
|
||
sessionStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(all));
|
||
} catch {
|
||
// ignore quota / private mode failures
|
||
}
|
||
}
|
||
|
||
export function readSidebarMenuCache(key: string): CachedSidebarMenu | undefined {
|
||
const memoryHit = cache.get(key);
|
||
if (memoryHit) return memoryHit;
|
||
|
||
const sessionHit = readSessionCache(key);
|
||
if (sessionHit) {
|
||
cache.set(key, sessionHit);
|
||
return sessionHit;
|
||
}
|
||
|
||
return undefined;
|
||
}
|
||
|
||
export function isSidebarMenuInflight(key: string): boolean {
|
||
return inflight.has(key);
|
||
}
|
||
|
||
export function clearSidebarMenuCache(): void {
|
||
cache.clear();
|
||
inflight.clear();
|
||
try {
|
||
sessionStorage.removeItem(SESSION_STORAGE_KEY);
|
||
sessionStorage.removeItem('nav-sidebar-menu-cache-v1');
|
||
} catch {
|
||
// ignore
|
||
}
|
||
}
|
||
|
||
export async function fetchSidebarMenuCached(
|
||
profileId: string,
|
||
scope: string
|
||
): Promise<CachedSidebarMenu> {
|
||
const key = sidebarMenuCacheKey(profileId, scope);
|
||
const hit = readSidebarMenuCache(key);
|
||
if (hit) return hit;
|
||
|
||
const pending = inflight.get(key);
|
||
if (pending) return pending;
|
||
|
||
const promise = loadNavMenuLayoutForScope(scope)
|
||
.then(({ layout, fromDirectRecord }) => {
|
||
const entry = createCacheEntry(layout, fromDirectRecord);
|
||
cache.set(key, entry);
|
||
writeSessionCache(key, entry);
|
||
return entry;
|
||
})
|
||
.finally(() => {
|
||
inflight.delete(key);
|
||
});
|
||
|
||
inflight.set(key, promise);
|
||
return promise;
|
||
}
|
||
|
||
/** 登录/会话恢复时预加载侧边栏菜单,避免 Layout 首次渲染出现骨架屏闪烁 */
|
||
export async function prefetchSidebarMenuForProfile(profile: {
|
||
id: string;
|
||
branch_code: string | null;
|
||
roles: string[];
|
||
}): Promise<void> {
|
||
const isSuperAdmin = profile.roles.includes('super_admin');
|
||
const scope = resolveSidebarMenuScope(isSuperAdmin, profile.branch_code);
|
||
try {
|
||
await fetchSidebarMenuCached(profile.id, scope);
|
||
} catch (error) {
|
||
console.error('预加载导航菜单失败:', error);
|
||
}
|
||
}
|