两个平级菜单合成 /ops 一个入口(两个 Tab,按权限显隐);底层两张表、两套后端、两枚权限码不变;/after-sales、/exceptions 保留为重定向不废深链。异常管理连同权限名与三语文案改名「预警管理」。设计见 docs/design/20261012-售后与异常合并为一个入口.md。
994 lines
37 KiB
TypeScript
994 lines
37 KiB
TypeScript
import type { LucideIcon } from 'lucide-react';
|
||
import type { UserRole } from '../hooks/useAuth';
|
||
import { MENU_CATALOG_MAP, MODULE_MENU_KEYS, type MenuCatalogEntry } from '../config/menuCatalog';
|
||
import { resolveMenuIcon, MENU_ICON_MAP } from '../config/menuIcons';
|
||
import { sanitizeSvg } from './sanitizeSvg';
|
||
|
||
export interface NavSecondaryItemConfig {
|
||
menuKey: string;
|
||
label?: string;
|
||
visible: boolean;
|
||
/**
|
||
* 仅用于**默认布局**:该(新增)菜单项补进已保存分组时,插到哪个菜单项**后面**。
|
||
* 老租户的分组结构与默认分组不同(例如 orders-* 挂在「业务流程」而不是「订单管理」),
|
||
* 没有这个锚点就只能追加到分组末尾,位置会离用户预期很远。
|
||
*/
|
||
afterMenuKey?: string;
|
||
}
|
||
|
||
export interface NavMenuGroupConfig {
|
||
id: string;
|
||
label: string;
|
||
icon: string;
|
||
/** AI 生成的 SVG 矢量图标(优先于 icon 名称) */
|
||
iconSvg?: string;
|
||
visible: boolean;
|
||
roles?: UserRole[];
|
||
items: NavSecondaryItemConfig[];
|
||
}
|
||
|
||
export interface NavMenuStandaloneConfig {
|
||
menuKey: string;
|
||
label?: string;
|
||
visible: boolean;
|
||
}
|
||
|
||
export type NavMenuOrderEntry =
|
||
| { kind: 'standalone'; menuKey: string }
|
||
| { kind: 'group'; groupId: string };
|
||
|
||
export interface NavMenuLayoutConfig {
|
||
version: 1;
|
||
order: NavMenuOrderEntry[];
|
||
groups: NavMenuGroupConfig[];
|
||
standalone: NavMenuStandaloneConfig[];
|
||
}
|
||
|
||
export interface RuntimeMenuItem {
|
||
path: string;
|
||
label: string;
|
||
menuKey?: string;
|
||
groupId?: string;
|
||
icon?: LucideIcon;
|
||
iconSvg?: string;
|
||
permission?: string | string[];
|
||
roles?: UserRole[];
|
||
accessMatch?: 'all' | 'any';
|
||
children?: RuntimeMenuItem[];
|
||
}
|
||
|
||
const MASTER_DATA_GROUP_ID = 'grp-master-data';
|
||
const MASTER_DATA_GROUP_LABEL = '基础数据';
|
||
|
||
/** 基础数据分组默认顺序(含显示名覆盖) */
|
||
const MASTER_DATA_CANONICAL_ITEMS: NavSecondaryItemConfig[] = [
|
||
{ menuKey: 'customers-list', visible: true },
|
||
{ menuKey: 'products-skus', visible: true },
|
||
{ menuKey: 'products-categories', visible: true },
|
||
{ menuKey: 'customers-quote-versions', label: '报价管理', visible: true },
|
||
{ menuKey: 'suppliers-list', visible: true },
|
||
{ menuKey: 'orders-bids', visible: false },
|
||
{ menuKey: 'inventory', label: '仓库管理', visible: true },
|
||
{ menuKey: 'product-trace', visible: true },
|
||
{ menuKey: 'quality-reports', visible: true },
|
||
{ menuKey: 'products-units', visible: true },
|
||
];
|
||
|
||
const MASTER_DATA_CANONICAL_KEYS = MASTER_DATA_CANONICAL_ITEMS.map((item) => item.menuKey);
|
||
|
||
const BUSINESS_FLOW_GROUP_ID = 'grp-business';
|
||
const BUSINESS_FLOW_GROUP_LABEL = '业务流程';
|
||
|
||
/**
|
||
* 「业务流程」分组的规范条目与顺序 —— **以主系统当前布局为模板**(开发者 2026-10-27:
|
||
* 「参考主系统现在的路由配置为模板,后续迁移/重新部署到新电脑上就好不变了」)。
|
||
*
|
||
* 主系统模板把**全部作业类**菜单都收在「业务流程」里(订单 / 采购 / 验收 / 分拣 / 配送 /
|
||
* 售后 / 异常),顶层只剩:数据看板、订单录入 + 业务流程 / 基础数据 / 财务结算 / 系统管理。
|
||
* 我方差异只有一条:`orders-price-qty`(跨系统改价/改量**申请**;主系统那边是「待审」)。
|
||
*/
|
||
const BUSINESS_FLOW_CANONICAL_ITEMS: NavSecondaryItemConfig[] = [
|
||
{ menuKey: 'orders-list', visible: true },
|
||
{ menuKey: 'purchase-list', visible: true },
|
||
{ menuKey: 'purchase-preparations', visible: true },
|
||
// 跨系统改价/改量申请:用户 2026-10-11 指定排在「供应商备货表」下方(勿动位置)
|
||
{ menuKey: 'orders-price-qty', visible: true, afterMenuKey: 'purchase-preparations' },
|
||
{ menuKey: 'inspection-list', visible: true },
|
||
{ menuKey: 'inspection-processing', visible: true },
|
||
{ menuKey: 'picking', visible: true },
|
||
{ menuKey: 'delivery', visible: true },
|
||
{ menuKey: 'ops', visible: true },
|
||
{ menuKey: 'inspection-review', visible: true },
|
||
];
|
||
|
||
const BUSINESS_FLOW_CANONICAL_KEYS = BUSINESS_FLOW_CANONICAL_ITEMS.map((item) => item.menuKey);
|
||
|
||
/**
|
||
* 顶层顺序(模板顺序,取自主系统最新那一份布局 BR002 行):
|
||
* 数据看板 → 订单录入 → 业务流程 → 基础数据 → 财务结算 → 系统管理。
|
||
* 管理员自建的分组/独立项不会被丢掉,统一排在后面(保持它们原有相对顺序)。
|
||
*/
|
||
const CANONICAL_TOP_LEVEL_ORDER: NavMenuOrderEntry[] = [
|
||
{ kind: 'standalone', menuKey: 'dashboard' },
|
||
{ kind: 'standalone', menuKey: 'customer-order' },
|
||
{ kind: 'group', groupId: BUSINESS_FLOW_GROUP_ID },
|
||
{ kind: 'group', groupId: MASTER_DATA_GROUP_ID },
|
||
{ kind: 'group', groupId: 'grp-finance' },
|
||
{ kind: 'group', groupId: 'grp-system' },
|
||
];
|
||
|
||
const DEFAULT_GROUPS: NavMenuGroupConfig[] = [
|
||
{
|
||
id: BUSINESS_FLOW_GROUP_ID,
|
||
label: BUSINESS_FLOW_GROUP_LABEL,
|
||
icon: 'CheckCircle',
|
||
visible: true,
|
||
items: BUSINESS_FLOW_CANONICAL_ITEMS.map((item) => ({ ...item })),
|
||
},
|
||
{
|
||
id: MASTER_DATA_GROUP_ID,
|
||
label: MASTER_DATA_GROUP_LABEL,
|
||
icon: 'Store',
|
||
visible: true,
|
||
items: [
|
||
...MASTER_DATA_CANONICAL_ITEMS.map((item) => ({ ...item })),
|
||
],
|
||
},
|
||
{
|
||
id: 'grp-finance',
|
||
// 模板里这个分组的显示名是「财务结算」(我方旧默认是「财务管理」)—— 以模板为准
|
||
label: '财务结算',
|
||
icon: 'CreditCard',
|
||
visible: true,
|
||
items: [
|
||
{ menuKey: 'finance-overview', visible: true },
|
||
{ menuKey: 'finance-costs', visible: true },
|
||
{ menuKey: 'finance-customer-statements', visible: true },
|
||
{ menuKey: 'finance-supplier-statements', visible: true },
|
||
],
|
||
},
|
||
{
|
||
id: 'grp-system',
|
||
label: '系统管理',
|
||
icon: 'Settings',
|
||
visible: true,
|
||
roles: ['super_admin', 'admin'],
|
||
items: [
|
||
{ menuKey: 'system-config', visible: true },
|
||
{ menuKey: 'system-ai-models', visible: true },
|
||
{ menuKey: 'system-ai-prompts', visible: true },
|
||
{ menuKey: 'system-weigh-ota', visible: true },
|
||
{ menuKey: 'system-menu-layout', visible: true },
|
||
{ menuKey: 'system-print-templates', visible: true },
|
||
{ menuKey: 'system-branches', visible: true },
|
||
{ menuKey: 'system-users', visible: true },
|
||
{ menuKey: 'system-roles', visible: true },
|
||
{ menuKey: 'system-logs', visible: true },
|
||
{ menuKey: 'system-notifications', visible: true },
|
||
],
|
||
},
|
||
];
|
||
|
||
/** 顶层默认只有「数据看板 / 订单录入」两个独立项(模板口径) */
|
||
const DEFAULT_STANDALONE: NavMenuStandaloneConfig[] = [
|
||
{ menuKey: 'dashboard', visible: true },
|
||
{ menuKey: 'customer-order', visible: true },
|
||
];
|
||
|
||
const DEFAULT_ORDER: NavMenuOrderEntry[] = [...CANONICAL_TOP_LEVEL_ORDER];
|
||
|
||
/** 导航里常见中文组名 → 内置 groupId(租户可能改过 id,仍用这套词条) */
|
||
const EXTRA_GROUP_LABEL_ALIASES: Record<string, string> = {
|
||
财务结算: 'grp-finance',
|
||
};
|
||
|
||
export function canonicalNavGroupIdForLabel(label: string): string | undefined {
|
||
const trimmed = label.trim();
|
||
if (!trimmed) return undefined;
|
||
const fromDefault = DEFAULT_GROUPS.find((group) => group.label === trimmed);
|
||
if (fromDefault) return fromDefault.id;
|
||
return EXTRA_GROUP_LABEL_ALIASES[trimmed];
|
||
}
|
||
|
||
export function createDefaultNavMenuLayout(): NavMenuLayoutConfig {
|
||
return {
|
||
version: 1,
|
||
order: DEFAULT_ORDER.map((entry) => ({ ...entry })),
|
||
groups: DEFAULT_GROUPS.map((group) => ({
|
||
...group,
|
||
items: group.items.map((item) => ({ ...item })),
|
||
})),
|
||
standalone: DEFAULT_STANDALONE.map((item) => ({ ...item })),
|
||
};
|
||
}
|
||
|
||
function cloneLayout(config: NavMenuLayoutConfig): NavMenuLayoutConfig {
|
||
return JSON.parse(JSON.stringify(config)) as NavMenuLayoutConfig;
|
||
}
|
||
|
||
function normalizeGroupItem(raw: unknown): NavSecondaryItemConfig | null {
|
||
if (!raw || typeof raw !== 'object') return null;
|
||
const item = raw as Record<string, unknown>;
|
||
if (typeof item.menuKey !== 'string' || !MENU_CATALOG_MAP[item.menuKey]) return null;
|
||
return {
|
||
menuKey: item.menuKey,
|
||
label: typeof item.label === 'string' && item.label.trim() ? item.label.trim() : undefined,
|
||
visible: item.visible !== false,
|
||
};
|
||
}
|
||
|
||
function normalizeGroup(raw: unknown): NavMenuGroupConfig | null {
|
||
if (!raw || typeof raw !== 'object') return null;
|
||
const group = raw as Record<string, unknown>;
|
||
if (typeof group.id !== 'string' || typeof group.label !== 'string') return null;
|
||
const items = Array.isArray(group.items)
|
||
? (group.items.map(normalizeGroupItem).filter(Boolean) as NavSecondaryItemConfig[])
|
||
: [];
|
||
const sanitizedIconSvg = typeof group.iconSvg === 'string' ? sanitizeSvg(group.iconSvg) : '';
|
||
return {
|
||
id: group.id,
|
||
label: group.label.trim() || '未命名菜单',
|
||
icon: typeof group.icon === 'string' ? group.icon : 'Package',
|
||
iconSvg: sanitizedIconSvg || undefined,
|
||
visible: group.visible !== false,
|
||
roles: Array.isArray(group.roles) ? (group.roles as UserRole[]) : undefined,
|
||
items,
|
||
};
|
||
}
|
||
|
||
function normalizeStandalone(raw: unknown): NavMenuStandaloneConfig | null {
|
||
if (!raw || typeof raw !== 'object') return null;
|
||
const item = raw as Record<string, unknown>;
|
||
if (typeof item.menuKey !== 'string') return null;
|
||
if (!MENU_CATALOG_MAP[item.menuKey]) return null;
|
||
return {
|
||
menuKey: item.menuKey,
|
||
label: typeof item.label === 'string' && item.label.trim() ? item.label.trim() : undefined,
|
||
visible: item.visible !== false,
|
||
};
|
||
}
|
||
|
||
function normalizeOrderEntry(raw: unknown): NavMenuOrderEntry | null {
|
||
if (!raw || typeof raw !== 'object') return null;
|
||
const entry = raw as Record<string, unknown>;
|
||
if (entry.kind === 'standalone' && typeof entry.menuKey === 'string') {
|
||
return MENU_CATALOG_MAP[entry.menuKey] ? { kind: 'standalone', menuKey: entry.menuKey } : null;
|
||
}
|
||
if (entry.kind === 'group' && typeof entry.groupId === 'string') {
|
||
return { kind: 'group', groupId: entry.groupId };
|
||
}
|
||
return null;
|
||
}
|
||
|
||
function findGroupByIdOrLabel(
|
||
layout: NavMenuLayoutConfig,
|
||
groupId: string,
|
||
label: string
|
||
): NavMenuGroupConfig | undefined {
|
||
return (
|
||
layout.groups.find((group) => group.id === groupId) ||
|
||
layout.groups.find((group) => group.label.trim() === label.trim())
|
||
);
|
||
}
|
||
|
||
/** 将 catalog 新增的二级菜单补进已有分组(不覆盖已保存配置) */
|
||
function appendMissingDefaultGroupItems(merged: NavMenuLayoutConfig, defaults: NavMenuLayoutConfig) {
|
||
const assigned = collectGroupedMenuKeys(merged);
|
||
const standaloneOrderKeys = collectStandaloneMenuKeysFromOrder(merged);
|
||
defaults.groups.forEach((defaultGroup) => {
|
||
const target = findGroupByIdOrLabel(merged, defaultGroup.id, defaultGroup.label);
|
||
if (!target) return;
|
||
defaultGroup.items.forEach((item) => {
|
||
if (assigned.has(item.menuKey)) return;
|
||
if (standaloneOrderKeys.has(item.menuKey)) return;
|
||
const anchorIndex = item.afterMenuKey
|
||
? target.items.findIndex((existing) => existing.menuKey === item.afterMenuKey)
|
||
: -1;
|
||
if (anchorIndex >= 0) target.items.splice(anchorIndex + 1, 0, { ...item });
|
||
else target.items.push({ ...item });
|
||
assigned.add(item.menuKey);
|
||
});
|
||
});
|
||
}
|
||
|
||
/** 去掉历史「履约作业」分组,条目并入「业务流程」(若存在) */
|
||
function dissolveLegacyFulfillmentOpsGroup(layout: NavMenuLayoutConfig): NavMenuLayoutConfig {
|
||
const legacy = layout.groups.filter((group) => group.label.trim() === '履约作业');
|
||
if (!legacy.length) return layout;
|
||
|
||
const next = cloneLayout(layout);
|
||
const flow =
|
||
next.groups.find((group) => group.label.trim() === '业务流程') ||
|
||
next.groups.find((group) => group.id === 'grp-business');
|
||
const assigned = collectGroupedMenuKeys(next);
|
||
|
||
for (const group of legacy) {
|
||
for (const item of group.items) {
|
||
if (!flow || assigned.has(item.menuKey)) continue;
|
||
flow.items.push({ ...item });
|
||
assigned.add(item.menuKey);
|
||
}
|
||
}
|
||
|
||
const legacyIds = new Set(legacy.map((group) => group.id));
|
||
next.groups = next.groups.filter((group) => group.label.trim() !== '履约作业');
|
||
next.order = next.order.filter(
|
||
(entry) => !(entry.kind === 'group' && legacyIds.has(entry.groupId))
|
||
);
|
||
return next;
|
||
}
|
||
|
||
/**
|
||
* 若存在「基础数据」分组:将规范条目迁入并按默认顺序排列。
|
||
* 自定义显示名优先保留;无自定义时用 canonical 覆盖名(如仓库管理)。
|
||
*/
|
||
function applyMasterDataGroupCanonicalOrder(layout: NavMenuLayoutConfig): NavMenuLayoutConfig {
|
||
const next = cloneLayout(layout);
|
||
const target = findGroupByIdOrLabel(next, MASTER_DATA_GROUP_ID, MASTER_DATA_GROUP_LABEL);
|
||
if (!target) return next;
|
||
|
||
const preferredLabel = new Map(
|
||
MASTER_DATA_CANONICAL_ITEMS.filter((item) => item.label).map((item) => [item.menuKey, item.label as string])
|
||
);
|
||
const found = new Map<string, NavSecondaryItemConfig>();
|
||
|
||
for (const group of next.groups) {
|
||
for (const item of group.items) {
|
||
if (!MASTER_DATA_CANONICAL_KEYS.includes(item.menuKey) || found.has(item.menuKey)) continue;
|
||
found.set(item.menuKey, { ...item });
|
||
}
|
||
}
|
||
for (const item of next.standalone) {
|
||
if (!MASTER_DATA_CANONICAL_KEYS.includes(item.menuKey) || found.has(item.menuKey)) continue;
|
||
found.set(item.menuKey, {
|
||
menuKey: item.menuKey,
|
||
label: item.label,
|
||
visible: item.visible !== false,
|
||
});
|
||
}
|
||
|
||
for (const menuKey of MASTER_DATA_CANONICAL_KEYS) {
|
||
removeMenuKeyFromGroups(next, menuKey);
|
||
removeStandaloneFromLayout(next, menuKey);
|
||
}
|
||
|
||
const group = findGroupByIdOrLabel(next, MASTER_DATA_GROUP_ID, MASTER_DATA_GROUP_LABEL);
|
||
if (!group) return next;
|
||
|
||
const rest = group.items.filter((item) => !MASTER_DATA_CANONICAL_KEYS.includes(item.menuKey));
|
||
const ordered: NavSecondaryItemConfig[] = MASTER_DATA_CANONICAL_ITEMS.map((canon) => {
|
||
const existing = found.get(canon.menuKey);
|
||
if (existing) {
|
||
return {
|
||
...existing,
|
||
label: existing.label || preferredLabel.get(canon.menuKey) || canon.label,
|
||
};
|
||
}
|
||
return { ...canon };
|
||
});
|
||
group.items = [...ordered, ...rest];
|
||
|
||
if (!hasOrderEntry(next.order, { kind: 'group', groupId: group.id })) {
|
||
const ordersIdx = next.order.findIndex(
|
||
(entry) => entry.kind === 'group' && entry.groupId === 'grp-orders'
|
||
);
|
||
const entry = { kind: 'group' as const, groupId: group.id };
|
||
if (ordersIdx >= 0) next.order.splice(ordersIdx + 1, 0, entry);
|
||
else next.order.push(entry);
|
||
}
|
||
|
||
const legacyEmptyLabels = new Set(['客户管理', '商品管理', '供应商管理']);
|
||
const legacyEmptyIds = new Set(['grp-customers', 'grp-products', 'grp-suppliers']);
|
||
next.groups = next.groups.filter((g) => {
|
||
if (g.items.length > 0) return true;
|
||
return !legacyEmptyIds.has(g.id) && !legacyEmptyLabels.has(g.label.trim());
|
||
});
|
||
const remainingIds = new Set(next.groups.map((g) => g.id));
|
||
next.order = next.order.filter(
|
||
(entry) => entry.kind !== 'group' || remainingIds.has(entry.groupId)
|
||
);
|
||
|
||
return next;
|
||
}
|
||
|
||
/**
|
||
* 「业务流程」分组归位:把**作业类**菜单从旧的分组/独立项里收进「业务流程」,
|
||
* 并按模板顺序排列;随后清掉已经空掉的旧分组(订单管理 / 采购管理 等)。
|
||
*
|
||
* 为什么需要它(开发者 2026-10-27「路由又乱了,参考主系统现在的路由配置为模板」):
|
||
* 布局是**按分公司存库**的(`system_configs.nav_menu_layout`),一旦保存过就永远覆盖代码默认 ⇒
|
||
* 老布局(11 个顶层:订单管理 / 采购管理 / 配送管理 / 售后管理 / 异常管理 …)会在**每个环境各自**
|
||
* 保留下来,迁移/换机后依旧"乱"。归位放在**代码里、每次加载都跑**,所以:
|
||
* · 不依赖任何一次性 SQL、换库换机都一样;管理员自建的**分组与顺序不被入侵**(只搬规范条目)。
|
||
* 与 `applyMasterDataGroupCanonicalOrder`(基础数据)同款做法,先例见 2026-10-11 的同类修复。
|
||
*/
|
||
function applyBusinessFlowGroupCanonicalOrder(layout: NavMenuLayoutConfig): NavMenuLayoutConfig {
|
||
const next = cloneLayout(layout);
|
||
const target = findGroupByIdOrLabel(next, BUSINESS_FLOW_GROUP_ID, BUSINESS_FLOW_GROUP_LABEL);
|
||
if (!target) return next;
|
||
|
||
// 先收集现存条目(保留管理员的显示名与可见性;可见性缺省视为可见)
|
||
const found = new Map<string, NavSecondaryItemConfig>();
|
||
for (const group of next.groups) {
|
||
for (const item of group.items) {
|
||
if (!BUSINESS_FLOW_CANONICAL_KEYS.includes(item.menuKey) || found.has(item.menuKey)) continue;
|
||
found.set(item.menuKey, { ...item });
|
||
}
|
||
}
|
||
for (const item of next.standalone) {
|
||
if (!BUSINESS_FLOW_CANONICAL_KEYS.includes(item.menuKey) || found.has(item.menuKey)) continue;
|
||
found.set(item.menuKey, { menuKey: item.menuKey, label: item.label, visible: item.visible !== false });
|
||
}
|
||
|
||
for (const menuKey of BUSINESS_FLOW_CANONICAL_KEYS) {
|
||
removeMenuKeyFromGroups(next, menuKey);
|
||
removeStandaloneFromLayout(next, menuKey);
|
||
}
|
||
|
||
const group = findGroupByIdOrLabel(next, BUSINESS_FLOW_GROUP_ID, BUSINESS_FLOW_GROUP_LABEL);
|
||
if (!group) return next;
|
||
group.visible = group.visible !== false;
|
||
const rest = group.items.filter((item) => !BUSINESS_FLOW_CANONICAL_KEYS.includes(item.menuKey));
|
||
group.items = [
|
||
...BUSINESS_FLOW_CANONICAL_ITEMS.map((canon) => {
|
||
const existing = found.get(canon.menuKey);
|
||
return existing ? { ...existing, afterMenuKey: canon.afterMenuKey } : { ...canon };
|
||
}),
|
||
...rest,
|
||
];
|
||
|
||
// 空掉的旧分组直接删(不删有自建条目的);订单管理/采购管理这两个旧默认分组无条件收掉
|
||
const legacyGroupIds = new Set(['grp-orders', 'grp-purchase']);
|
||
const legacyLabels = new Set(['订单管理', '采购管理']);
|
||
next.groups = next.groups.filter((g) => {
|
||
if (g.id === group.id) return true;
|
||
if (g.items.length > 0) return !legacyGroupIds.has(g.id) && !legacyLabels.has(g.label.trim());
|
||
return !legacyGroupIds.has(g.id) && !legacyLabels.has(g.label.trim());
|
||
});
|
||
|
||
/**
|
||
* 财务分组的显示名收敛:**只在它还叫我们的旧默认「财务管理」时**改成模板的「财务结算」——
|
||
* 若管理员已改成别的名字(自定义),一律不动。
|
||
*/
|
||
const finance = next.groups.find((g) => g.id === 'grp-finance');
|
||
if (finance && finance.label.trim() === '财务管理') finance.label = '财务结算';
|
||
|
||
// 顶层顺序回到模板:数据看板 / 订单录入 / 业务流程 / 基础数据 / 财务结算 / 系统管理
|
||
const remainingIds = new Set(next.groups.map((g) => g.id));
|
||
const placedGroups = new Set<string>();
|
||
const placedStandalone = new Set<string>();
|
||
const ordered: NavMenuOrderEntry[] = [];
|
||
/**
|
||
* 模板条目里的分组 id 可能对不上:老租户/老模板用过别的 id(如 `grp-products` = 基础数据、
|
||
* `grp-suppliers` = 业务流程)。所以按 **id 或规范显示名** 两头找(与 `findGroupByIdOrLabel` 同款口径),
|
||
* 否则这些分组会被当成"自建分组"排到模板四组后面 ⇒ 顺序又乱了。
|
||
*/
|
||
const resolveCanonicalGroup = (groupId: string) => {
|
||
const byId = next.groups.find((g) => g.id === groupId);
|
||
if (byId) return byId;
|
||
const label = DEFAULT_GROUPS.find((g) => g.id === groupId)?.label;
|
||
return label ? next.groups.find((g) => g.label.trim() === label) : undefined;
|
||
};
|
||
for (const entry of CANONICAL_TOP_LEVEL_ORDER) {
|
||
if (entry.kind === 'group') {
|
||
const group = resolveCanonicalGroup(entry.groupId);
|
||
if (!group || !remainingIds.has(group.id)) continue;
|
||
if (!group.visible) continue;
|
||
ordered.push({ kind: 'group', groupId: group.id });
|
||
placedGroups.add(group.id);
|
||
} else {
|
||
const hasItem = next.standalone.some((item) => item.menuKey === entry.menuKey && item.visible !== false);
|
||
if (!hasItem) continue;
|
||
ordered.push({ ...entry });
|
||
placedStandalone.add(entry.menuKey);
|
||
}
|
||
}
|
||
for (const entry of next.order) {
|
||
if (entry.kind === 'group') {
|
||
if (placedGroups.has(entry.groupId) || !remainingIds.has(entry.groupId)) continue;
|
||
ordered.push({ ...entry });
|
||
placedGroups.add(entry.groupId);
|
||
} else {
|
||
if (placedStandalone.has(entry.menuKey)) continue;
|
||
if (!next.standalone.some((item) => item.menuKey === entry.menuKey)) continue;
|
||
ordered.push({ ...entry });
|
||
placedStandalone.add(entry.menuKey);
|
||
}
|
||
}
|
||
next.order = ordered;
|
||
return next;
|
||
}
|
||
|
||
export function mergeNavMenuLayout(saved: unknown): NavMenuLayoutConfig {
|
||
const defaults = createDefaultNavMenuLayout();
|
||
if (!saved || typeof saved !== 'object') return defaults;
|
||
|
||
const raw = saved as Record<string, unknown>;
|
||
if (raw.version !== 1) return defaults;
|
||
|
||
const merged = cloneLayout(defaults);
|
||
|
||
const savedGroups = Array.isArray(raw.groups)
|
||
? (raw.groups.map(normalizeGroup).filter(Boolean) as NavMenuGroupConfig[])
|
||
: [];
|
||
const savedStandalone = Array.isArray(raw.standalone)
|
||
? (raw.standalone.map(normalizeStandalone).filter(Boolean) as NavMenuStandaloneConfig[])
|
||
: [];
|
||
const savedOrder = Array.isArray(raw.order)
|
||
? (raw.order.map(normalizeOrderEntry).filter(Boolean) as NavMenuOrderEntry[])
|
||
: [];
|
||
|
||
if (Array.isArray(raw.groups)) {
|
||
const defaultGroupMap = Object.fromEntries(defaults.groups.map((g) => [g.id, g]));
|
||
const usedSecondary = new Set<string>();
|
||
|
||
merged.groups = savedGroups.map((group) => {
|
||
const fallback = defaultGroupMap[group.id];
|
||
const items = group.items.map((item) => ({ ...item }));
|
||
items.forEach((item) => usedSecondary.add(item.menuKey));
|
||
return {
|
||
id: group.id,
|
||
label: group.label || fallback?.label || '未命名菜单',
|
||
icon: group.icon || fallback?.icon || 'Package',
|
||
iconSvg: group.iconSvg || fallback?.iconSvg,
|
||
visible: group.visible,
|
||
roles: group.roles || fallback?.roles,
|
||
items,
|
||
};
|
||
});
|
||
|
||
defaults.groups.forEach((defaultGroup) => {
|
||
if (findGroupByIdOrLabel(merged, defaultGroup.id, defaultGroup.label)) return;
|
||
const remainingItems = defaultGroup.items.filter((item) => !usedSecondary.has(item.menuKey));
|
||
if (remainingItems.length === 0) return;
|
||
merged.groups.push({
|
||
...defaultGroup,
|
||
items: remainingItems.map((item) => ({ ...item })),
|
||
});
|
||
if (!hasOrderEntry(merged.order, { kind: 'group', groupId: defaultGroup.id })) {
|
||
merged.order.push({ kind: 'group', groupId: defaultGroup.id });
|
||
}
|
||
});
|
||
|
||
appendMissingDefaultGroupItems(merged, defaults);
|
||
}
|
||
|
||
if (Array.isArray(raw.standalone)) {
|
||
const defaultStandaloneMap = Object.fromEntries(defaults.standalone.map((s) => [s.menuKey, s]));
|
||
const seen = new Set<string>();
|
||
merged.standalone = savedStandalone.map((item) => {
|
||
seen.add(item.menuKey);
|
||
const fallback = defaultStandaloneMap[item.menuKey];
|
||
return {
|
||
menuKey: item.menuKey,
|
||
label: item.label || fallback?.label,
|
||
visible: item.visible,
|
||
};
|
||
});
|
||
const groupedKeys = collectGroupedMenuKeys(merged);
|
||
defaults.standalone.forEach((item) => {
|
||
if (seen.has(item.menuKey) || groupedKeys.has(item.menuKey)) return;
|
||
merged.standalone.push({ ...item });
|
||
if (!hasStandaloneOrderEntry(merged, item.menuKey)) {
|
||
merged.order.push({ kind: 'standalone', menuKey: item.menuKey });
|
||
}
|
||
});
|
||
}
|
||
|
||
if (Array.isArray(raw.order)) {
|
||
merged.order = savedOrder.map((entry) => ({ ...entry }));
|
||
const placedKeys = collectConfiguredMenuKeys(merged);
|
||
defaults.order.forEach((entry) => {
|
||
if (hasOrderEntry(merged.order, entry)) return;
|
||
if (entry.kind === 'standalone') {
|
||
if (placedKeys.has(entry.menuKey)) return;
|
||
}
|
||
if (entry.kind === 'group') {
|
||
if (merged.groups.some((group) => group.id === entry.groupId)) return;
|
||
}
|
||
merged.order.push({ ...entry });
|
||
});
|
||
}
|
||
|
||
if (!Array.isArray(raw.groups)) {
|
||
appendMissingDefaultGroupItems(merged, defaults);
|
||
}
|
||
|
||
// ⚠️ 关键收尾(2026-10-11 修):`merged.order` 在 458 行被"保存的 order"整体覆盖后,
|
||
// 之前为**新建分组**补的 order 条目会被冲掉 ⇒ 该分组存在于 `groups` 却不在 `order` 里,
|
||
// `buildRuntimeMenuTree` 只按 `order` 渲染 ⇒ 分组(连同里面的新菜单项)**永远不显示**。
|
||
// 这里统一兜底:凡是有可见条目、却没有 order 条目的分组,按 `groups` 顺序补到末尾。
|
||
// 实测场景:平台保存的布局里没有「订单管理」分组(orders-* 挂在「业务流程」下),
|
||
// 新加的 orders-price-qty 默认分组恰好是 grp-orders ⇒ 不补就是"用户看不到入口"。
|
||
ensureGroupOrderEntries(merged);
|
||
|
||
return applyBusinessFlowGroupCanonicalOrder(
|
||
applyMasterDataGroupCanonicalOrder(
|
||
dissolveLegacyFulfillmentOpsGroup(reconcileExclusivePlacements(merged))
|
||
)
|
||
);
|
||
}
|
||
|
||
/** 给"有可见条目但缺 order 条目"的分组补 order 条目(保持既有顺序,只追加) */
|
||
function ensureGroupOrderEntries(layout: NavMenuLayoutConfig) {
|
||
const orderedGroupIds = new Set(
|
||
layout.order.filter((entry) => entry.kind === 'group').map((entry) => entry.groupId)
|
||
);
|
||
layout.groups.forEach((group) => {
|
||
if (orderedGroupIds.has(group.id)) return;
|
||
if (!group.visible) return;
|
||
if (!group.items.some((item) => item.visible)) return;
|
||
layout.order.push({ kind: 'group', groupId: group.id });
|
||
orderedGroupIds.add(group.id);
|
||
});
|
||
}
|
||
|
||
function collectGroupedMenuKeys(layout: NavMenuLayoutConfig): Set<string> {
|
||
const keys = new Set<string>();
|
||
layout.groups.forEach((group) => group.items.forEach((item) => keys.add(item.menuKey)));
|
||
return keys;
|
||
}
|
||
|
||
function collectConfiguredMenuKeys(layout: NavMenuLayoutConfig): Set<string> {
|
||
const keys = collectGroupedMenuKeys(layout);
|
||
layout.standalone.forEach((item) => keys.add(item.menuKey));
|
||
layout.order.forEach((entry) => {
|
||
if (entry.kind === 'standalone') keys.add(entry.menuKey);
|
||
});
|
||
return keys;
|
||
}
|
||
|
||
function collectStandaloneMenuKeysFromOrder(layout: NavMenuLayoutConfig): Set<string> {
|
||
return new Set(
|
||
layout.order
|
||
.filter((entry): entry is { kind: 'standalone'; menuKey: string } => entry.kind === 'standalone')
|
||
.map((entry) => entry.menuKey)
|
||
);
|
||
}
|
||
|
||
function hasStandaloneOrderEntry(layout: NavMenuLayoutConfig, menuKey: string) {
|
||
return layout.order.some(
|
||
(entry) => entry.kind === 'standalone' && entry.menuKey === menuKey
|
||
);
|
||
}
|
||
|
||
function hasOrderEntry(order: NavMenuOrderEntry[], entry: NavMenuOrderEntry) {
|
||
return order.some((item) => {
|
||
if (item.kind !== entry.kind) return false;
|
||
if (item.kind === 'standalone' && entry.kind === 'standalone') {
|
||
return item.menuKey === entry.menuKey;
|
||
}
|
||
if (item.kind === 'group' && entry.kind === 'group') {
|
||
return item.groupId === entry.groupId;
|
||
}
|
||
return false;
|
||
});
|
||
}
|
||
|
||
function reconcileExclusivePlacements(layout: NavMenuLayoutConfig): NavMenuLayoutConfig {
|
||
const next = cloneLayout(layout);
|
||
const standaloneKeys = new Set(
|
||
next.order
|
||
.filter((entry): entry is { kind: 'standalone'; menuKey: string } => entry.kind === 'standalone')
|
||
.map((entry) => entry.menuKey)
|
||
);
|
||
|
||
next.groups = next.groups.map((group) => ({
|
||
...group,
|
||
items: group.items.filter((item) => !standaloneKeys.has(item.menuKey)),
|
||
}));
|
||
|
||
const groupedKeys = new Set<string>();
|
||
next.groups.forEach((group) => group.items.forEach((item) => groupedKeys.add(item.menuKey)));
|
||
|
||
groupedKeys.forEach((menuKey) => {
|
||
if (standaloneKeys.has(menuKey)) return;
|
||
next.standalone = next.standalone.filter((item) => item.menuKey !== menuKey);
|
||
next.order = next.order.filter(
|
||
(entry) => !(entry.kind === 'standalone' && entry.menuKey === menuKey)
|
||
);
|
||
});
|
||
|
||
return next;
|
||
}
|
||
|
||
export type MenuPlacement =
|
||
| { type: 'standalone' }
|
||
| { type: 'grouped'; groupId: string }
|
||
| { type: 'unassigned' };
|
||
|
||
export function getMenuPlacement(layout: NavMenuLayoutConfig, menuKey: string): MenuPlacement {
|
||
if (layout.order.some((entry) => entry.kind === 'standalone' && entry.menuKey === menuKey)) {
|
||
return { type: 'standalone' };
|
||
}
|
||
for (const group of layout.groups) {
|
||
if (group.items.some((item) => item.menuKey === menuKey)) {
|
||
return { type: 'grouped', groupId: group.id };
|
||
}
|
||
}
|
||
return { type: 'unassigned' };
|
||
}
|
||
|
||
/** 从导航配置中解析模块显示名称(自定义名称优先,否则目录默认名) */
|
||
export function resolveMenuItemLabel(
|
||
layout: NavMenuLayoutConfig,
|
||
menuKey: string,
|
||
fallback?: string
|
||
): string {
|
||
const standalone = layout.standalone.find((item) => item.menuKey === menuKey);
|
||
if (standalone?.label?.trim()) return standalone.label.trim();
|
||
|
||
for (const group of layout.groups) {
|
||
const item = group.items.find((entry) => entry.menuKey === menuKey);
|
||
if (item?.label?.trim()) return item.label.trim();
|
||
}
|
||
|
||
return fallback || MENU_CATALOG_MAP[menuKey]?.defaultLabel || menuKey;
|
||
}
|
||
|
||
/** 从运行时导航树中按路径解析页面标题(已含自定义名称) */
|
||
export function findRuntimeMenuLabelByPath(
|
||
items: RuntimeMenuItem[],
|
||
pathname: string
|
||
): string | undefined {
|
||
const normalized =
|
||
pathname.length > 1 && pathname.endsWith('/') ? pathname.slice(0, -1) : pathname;
|
||
|
||
let best: { path: string; label: string } | undefined;
|
||
|
||
const visit = (list: RuntimeMenuItem[]) => {
|
||
for (const item of list) {
|
||
if (item.children?.length) visit(item.children);
|
||
if (normalized === item.path || normalized.startsWith(`${item.path}/`)) {
|
||
if (!best || item.path.length > best.path.length) {
|
||
best = { path: item.path, label: item.label };
|
||
}
|
||
}
|
||
}
|
||
};
|
||
|
||
visit(items);
|
||
return best?.label;
|
||
}
|
||
|
||
/** 可添加到指定组内的模块(不含已在该组内的) */
|
||
export function getAddableModulesForGroup(layout: NavMenuLayoutConfig, groupId: string): string[] {
|
||
const group = layout.groups.find((item) => item.id === groupId);
|
||
const inGroup = new Set(group?.items.map((item) => item.menuKey) || []);
|
||
return MODULE_MENU_KEYS.filter((menuKey) => !inGroup.has(menuKey));
|
||
}
|
||
|
||
function removeMenuKeyFromGroups(layout: NavMenuLayoutConfig, menuKey: string) {
|
||
layout.groups.forEach((group) => {
|
||
group.items = group.items.filter((item) => item.menuKey !== menuKey);
|
||
});
|
||
}
|
||
|
||
function removeStandaloneFromLayout(layout: NavMenuLayoutConfig, menuKey: string) {
|
||
layout.standalone = layout.standalone.filter((item) => item.menuKey !== menuKey);
|
||
layout.order = layout.order.filter(
|
||
(entry) => !(entry.kind === 'standalone' && entry.menuKey === menuKey)
|
||
);
|
||
}
|
||
|
||
/** 模块单独显示在导航栏 */
|
||
export function setMenuAsStandalone(
|
||
layout: NavMenuLayoutConfig,
|
||
menuKey: string,
|
||
label?: string
|
||
): NavMenuLayoutConfig {
|
||
const next = cloneLayout(layout);
|
||
removeMenuKeyFromGroups(next, menuKey);
|
||
|
||
const existing = next.standalone.find((item) => item.menuKey === menuKey);
|
||
if (existing) {
|
||
if (label?.trim()) existing.label = label.trim();
|
||
existing.visible = true;
|
||
} else {
|
||
next.standalone.push({ menuKey, label: label?.trim() || undefined, visible: true });
|
||
}
|
||
|
||
if (!next.order.some((entry) => entry.kind === 'standalone' && entry.menuKey === menuKey)) {
|
||
next.order.push({ kind: 'standalone', menuKey });
|
||
}
|
||
return next;
|
||
}
|
||
|
||
/** 模块放入指定组(组内二级展示) */
|
||
export function setMenuAsSecondary(
|
||
layout: NavMenuLayoutConfig,
|
||
menuKey: string,
|
||
groupId: string,
|
||
label?: string
|
||
): NavMenuLayoutConfig {
|
||
const next = cloneLayout(layout);
|
||
const target = next.groups.find((group) => group.id === groupId);
|
||
if (!target) return layout;
|
||
|
||
removeStandaloneFromLayout(next, menuKey);
|
||
removeMenuKeyFromGroups(next, menuKey);
|
||
|
||
const existingLabel = label?.trim() || undefined;
|
||
if (!target.items.some((item) => item.menuKey === menuKey)) {
|
||
target.items.push({ menuKey, label: existingLabel, visible: true });
|
||
}
|
||
|
||
if (!next.order.some((entry) => entry.kind === 'group' && entry.groupId === groupId)) {
|
||
next.order.push({ kind: 'group', groupId });
|
||
}
|
||
return next;
|
||
}
|
||
|
||
export interface DeleteMenuGroupOptions {
|
||
/** 删除后子页面移入的目标一级菜单;不传则改为一级独立显示 */
|
||
moveItemsToGroupId?: string;
|
||
}
|
||
|
||
/** 删除菜单组(名称可改、可新建、可删除) */
|
||
export function deleteMenuGroup(
|
||
layout: NavMenuLayoutConfig,
|
||
groupId: string,
|
||
options: DeleteMenuGroupOptions = {}
|
||
): NavMenuLayoutConfig {
|
||
const next = cloneLayout(layout);
|
||
const group = next.groups.find((item) => item.id === groupId);
|
||
if (!group) return layout;
|
||
|
||
const movingItems = group.items.map((item) => ({ ...item }));
|
||
next.groups = next.groups.filter((item) => item.id !== groupId);
|
||
next.order = next.order.filter((entry) => !(entry.kind === 'group' && entry.groupId === groupId));
|
||
|
||
if (movingItems.length === 0) return next;
|
||
|
||
const targetGroupId = options.moveItemsToGroupId;
|
||
if (targetGroupId && next.groups.some((item) => item.id === targetGroupId)) {
|
||
movingItems.forEach((item) => {
|
||
next.groups = next.groups.map((g) =>
|
||
g.id === targetGroupId && !g.items.some((i) => i.menuKey === item.menuKey)
|
||
? { ...g, items: [...g.items, item] }
|
||
: g
|
||
);
|
||
});
|
||
if (!next.order.some((entry) => entry.kind === 'group' && entry.groupId === targetGroupId)) {
|
||
next.order.push({ kind: 'group', groupId: targetGroupId });
|
||
}
|
||
return next;
|
||
}
|
||
|
||
movingItems.forEach((item) => {
|
||
if (!next.standalone.some((s) => s.menuKey === item.menuKey)) {
|
||
next.standalone.push({ menuKey: item.menuKey, label: item.label, visible: item.visible });
|
||
}
|
||
if (!next.order.some((entry) => entry.kind === 'standalone' && entry.menuKey === item.menuKey)) {
|
||
next.order.push({ kind: 'standalone', menuKey: item.menuKey });
|
||
}
|
||
});
|
||
return next;
|
||
}
|
||
|
||
function catalogToRuntime(entry: MenuCatalogEntry, labelOverride?: string): RuntimeMenuItem {
|
||
return {
|
||
path: entry.path,
|
||
label: labelOverride || entry.defaultLabel,
|
||
menuKey: entry.menuKey,
|
||
icon: resolveMenuIcon(entry.icon),
|
||
permission: entry.permission,
|
||
roles: entry.roles,
|
||
accessMatch: entry.accessMatch,
|
||
};
|
||
}
|
||
|
||
/** 单独显示的一级菜单:不使用 ChevronRight 占位图标 */
|
||
function catalogToStandaloneRuntime(entry: MenuCatalogEntry, labelOverride?: string): RuntimeMenuItem {
|
||
const iconName = entry.icon;
|
||
const icon =
|
||
iconName && iconName !== 'ChevronRight' && MENU_ICON_MAP[iconName]
|
||
? MENU_ICON_MAP[iconName]
|
||
: undefined;
|
||
return {
|
||
path: entry.path,
|
||
label: labelOverride || entry.defaultLabel,
|
||
menuKey: entry.menuKey,
|
||
icon,
|
||
permission: entry.permission,
|
||
roles: entry.roles,
|
||
accessMatch: entry.accessMatch,
|
||
};
|
||
}
|
||
|
||
function groupPermission(items: RuntimeMenuItem[]): string[] | undefined {
|
||
const codes = new Set<string>();
|
||
items.forEach((item) => {
|
||
if (!item.permission) return;
|
||
if (Array.isArray(item.permission)) item.permission.forEach((p) => codes.add(p));
|
||
else codes.add(item.permission);
|
||
});
|
||
return codes.size > 0 ? [...codes] : undefined;
|
||
}
|
||
|
||
function groupBasePath(items: RuntimeMenuItem[]): string {
|
||
if (items.length === 0) return '/';
|
||
const paths = items.map((item) => item.path).sort((a, b) => a.length - b.length);
|
||
const parts = paths[0].split('/').filter(Boolean);
|
||
return parts.length >= 1 ? `/${parts[0]}` : paths[0];
|
||
}
|
||
|
||
export function buildRuntimeMenuTree(config: NavMenuLayoutConfig): RuntimeMenuItem[] {
|
||
const standaloneMap = Object.fromEntries(
|
||
config.standalone.map((item) => [item.menuKey, item])
|
||
);
|
||
const groupMap = Object.fromEntries(config.groups.map((group) => [group.id, group]));
|
||
|
||
const result: RuntimeMenuItem[] = [];
|
||
|
||
config.order.forEach((entry) => {
|
||
if (entry.kind === 'standalone') {
|
||
const standalone = standaloneMap[entry.menuKey];
|
||
const catalog = MENU_CATALOG_MAP[entry.menuKey];
|
||
if (!standalone?.visible || !catalog) return;
|
||
result.push(catalogToStandaloneRuntime(catalog, standalone.label));
|
||
return;
|
||
}
|
||
|
||
const group = groupMap[entry.groupId];
|
||
if (!group?.visible) return;
|
||
const children = group.items
|
||
.filter((item) => item.visible)
|
||
.map((item) => {
|
||
const catalog = MENU_CATALOG_MAP[item.menuKey];
|
||
if (!catalog) return null;
|
||
return catalogToRuntime(catalog, item.label);
|
||
})
|
||
.filter(Boolean) as RuntimeMenuItem[];
|
||
if (children.length === 0) return;
|
||
|
||
const groupRoles = group.roles || [];
|
||
result.push({
|
||
path: groupBasePath(children),
|
||
label: group.label,
|
||
groupId: group.id,
|
||
icon: group.iconSvg ? undefined : resolveMenuIcon(group.icon),
|
||
iconSvg: group.iconSvg,
|
||
permission: groupPermission(children),
|
||
roles: groupRoles.length > 0 ? groupRoles : undefined,
|
||
children,
|
||
});
|
||
});
|
||
|
||
return result;
|
||
}
|
||
|
||
export function moveSecondaryMenuItem(
|
||
layout: NavMenuLayoutConfig,
|
||
menuKey: string,
|
||
targetGroupId: string
|
||
): NavMenuLayoutConfig {
|
||
return setMenuAsSecondary(layout, menuKey, targetGroupId);
|
||
}
|
||
|
||
/** 调整组内模块顺序 */
|
||
export function reorderGroupMenuItems(
|
||
layout: NavMenuLayoutConfig,
|
||
groupId: string,
|
||
fromIndex: number,
|
||
toIndex: number
|
||
): NavMenuLayoutConfig {
|
||
if (fromIndex === toIndex) return layout;
|
||
const next = cloneLayout(layout);
|
||
const group = next.groups.find((item) => item.id === groupId);
|
||
if (!group) return layout;
|
||
const items = [...group.items];
|
||
if (fromIndex < 0 || fromIndex >= items.length || toIndex < 0 || toIndex >= items.length) {
|
||
return layout;
|
||
}
|
||
const [moved] = items.splice(fromIndex, 1);
|
||
items.splice(toIndex, 0, moved);
|
||
group.items = items;
|
||
return next;
|
||
}
|
||
|
||
export function createMenuGroupId(label: string) {
|
||
const slug = label.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^\w-]/g, '') || 'group';
|
||
return `grp-${slug}-${Date.now().toString(36)}`;
|
||
}
|