// 测试 system-info 页面的 nav 匹配逻辑 // 模拟 menu 配置(简化版本) const menuList = [ { id: "home", title: "首页", groups: [], }, { id: "maintain", title: "维护", groups: [ { id: "dev-config", title: "开发配置", children: [ { id: "dev-config-category", title: "配置分类", path: "/pages/mall/admin/maintain/dev-config/category", }, ], }, { id: "system-info", title: "系统信息", path: "/pages/mall/admin/maintain/system-info", children: [], // 注意:这是一个 group 叶子节点 }, ], }, ]; // nav.uts 中的匹配逻辑 function findActiveByCurrentPage(menuList, currentPage) { const page = currentPage || ""; // 1) currentPage 直接是一级 menu id const mById = menuList.find((m) => m.id === page); if (mById) { return { activeMenuId: mById.id, activeSubId: "", reason: "一级菜单ID匹配", }; } // 2) currentPage 是 path(/pages/xxx 或 pages/xxx) const pageNorm = normalize(page); for (const m of menuList) { const groups = m.groups ?? []; // group / child 扫描 for (const g of groups) { // group 叶子 - 检查 id if (g.id === page) { console.log(`✓ Group ID 匹配: g.id="${g.id}" === page="${page}"`); return { activeMenuId: m.id, activeSubId: g.id, reason: "Group ID匹配", }; } // group 叶子 - 检查 path if (g.path && normalize(g.path) === pageNorm) { console.log( `✓ Group Path 匹配: normalize("${g.path}") === normalize("${page}")`, ); return { activeMenuId: m.id, activeSubId: g.id, reason: "Group Path匹配", }; } const cs = g.children ?? []; for (const c of cs) { // 用 id 命中 if (c.id === page) { console.log(`✓ Child ID 匹配: c.id="${c.id}" === page="${page}"`); return { activeMenuId: m.id, activeSubId: c.id, reason: "Child ID匹配", }; } // 用 path 命中 if (c.path && normalize(c.path) === pageNorm) { console.log( `✓ Child Path 匹配: normalize("${c.path}") === normalize("${page}")`, ); return { activeMenuId: m.id, activeSubId: c.id, reason: "Child Path匹配", }; } } } } // 3) 找不到:兜底 home return { activeMenuId: "home", activeSubId: "", reason: "未匹配,默认home" }; } function normalize(p) { if (!p) return ""; const s = p.startsWith("/") ? p.slice(1) : p; const q = s.indexOf("?"); return q >= 0 ? s.slice(0, q) : s; } // 测试用例 console.log("\n=== 测试 system-info 页面匹配 ===\n"); const testCases = [ "system-info", "/pages/mall/admin/maintain/system-info", "pages/mall/admin/maintain/system-info", ]; testCases.forEach((testPage, idx) => { console.log(`\n测试 ${idx + 1}: currentPage = "${testPage}"`); const result = findActiveByCurrentPage(menuList, testPage); console.log( `结果: activeMenuId="${result.activeMenuId}", activeSubId="${result.activeSubId}"`, ); console.log(`原因: ${result.reason}`); if ( result.activeMenuId === "maintain" && result.activeSubId === "system-info" ) { console.log("✅ PASS - 正确匹配到 maintain/system-info"); } else { console.log("❌ FAIL - 应该匹配到 maintain/system-info"); } }); console.log("\n=== 测试完成 ===\n");