perf:菜单遍历逻辑优化,不固定索引获取菜单

master
黄伟杰 3 weeks ago
parent 08fe076728
commit aff935a30d

@ -190,15 +190,19 @@ export function getDynamicTabMenus(menus) {
return getTopLevelMenus(menus).filter((menu) => !looksLikeHomeMenu(menu)) return getTopLevelMenus(menus).filter((menu) => !looksLikeHomeMenu(menu))
} }
function normalizePagePath(value) {
const path = String(value || '').trim().replace(/^\/+/, '')
return path ? `/${path}` : ''
}
export function findTabMenuByPage(menus, pagePath) { export function findTabMenuByPage(menus, pagePath) {
const dynamicMenus = getDynamicTabMenus(menus) const dynamicMenus = getDynamicTabMenus(menus)
if (pagePath === 'pages/report') { const targetPath = normalizePagePath(pagePath)
return dynamicMenus[0] || null if (!targetPath) {
} return null
if (pagePath === 'pages/work') {
return dynamicMenus[1] || dynamicMenus[0] || null
} }
return null
return dynamicMenus.find((menu) => normalizePagePath(resolveMenuUrl(menu)) === targetPath) || null
} }
export function buildPageModules(tabMenu) { export function buildPageModules(tabMenu) {
@ -254,11 +258,12 @@ export function getMenuSymbol(name, index) {
} }
export function syncTabBarMenus(menus, options = {}) { export function syncTabBarMenus(menus, options = {}) {
const dynamicMenus = getDynamicTabMenus(menus) const reportMenu = findTabMenuByPage(menus, 'pages/report')
const workMenu = findTabMenuByPage(menus, 'pages/work')
return [ return [
options.homeText || '首页', options.homeText || '首页',
dynamicMenus[0]?.name || options.reportFallback || '报表', reportMenu?.name || options.reportFallback || '报表',
dynamicMenus[1]?.name || dynamicMenus[0]?.name || options.workFallback || '管理', workMenu?.name || reportMenu?.name || options.workFallback || '管理',
options.mineText || '我的' options.mineText || '我的'
] ]
} }

Loading…
Cancel
Save