Files
SupplierSystem/src/utils/menuLayoutCache.ts
T
Ubuntu aa2db57b0f
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
feat(main-system): 免入库单取货链路 + 实收数量「事实登记 + 事后同步」
免入库(主系统骑手上门取货):
- 50 展示「待取货」、副文案「待主系统骑手上门取货」、不建我方配送任务(守卫与开关解耦)
- 我方 50 即回写备货完成;两开关(SKIP_INBOUND_AWAIT_PICKUP / FULFILLMENT_EVENTS)改默认开

核货事实镜像(「核货完毕」标记):
- 按主系统 lookup 的 actual_qty 落台账,新增 GET/POST /orders/:orderCode/main-system-prep-facts[/refresh]
- 详情行内显示「核货完毕」(含核货量),分配对账兜底刷新

实收数量/单位口径变更(对方先改 + 我方事后同步):
- 台账加 applied_at;inbound 与 picking_check 同为事实登记(驳回不回滚)
- 同意才落我方值:免入库改装车核货量、送仓改验收实收(已入库走既有库存调整)、已结算/已结案只留痕
- 计划量与金额一起改,并同步结算投影(syncStatementsForOrderSafe)
- 审核页语义改「主系统实际值(已生效)/ 是否同步」+ 已处理历史(双方时间与落值结论)

台账对照与回写修复:
- 行级对照改整单一次性(同名商品不再重复认领)+ 存量纠正 healOrderPrepMapping
- 堵住客户绑定回填旁路(免入库单不再被派我方骑手)
- 回写触发函数改对象参数(修 branch/order 传反导致的静默不回写)

文档:设计文档 4 篇 +《给主系统 AI 的对接改造清单》§2.9(第六批契约)

回归:服务端全量与 HEAD 基线一致(24 个失败文件同集合);前端 140 文件 / 699 测试全绿;tsc 双端干净
2026-10-09 21:52:17 +08:00

150 lines
4.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
}
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 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);
}
}