完善页面
This commit is contained in:
@@ -1,24 +1,85 @@
|
||||
import type { MenuItem } from '../types.uts'
|
||||
// utils/nav.uts
|
||||
import type { MenuItem, MenuGroup, MenuChild } from '../types.uts'
|
||||
|
||||
export function findActiveByCurrentPage(
|
||||
menuList: MenuItem[],
|
||||
currentPage: string
|
||||
): { activeMenuId: string, activeSubId: string } {
|
||||
|
||||
const page = currentPage || ''
|
||||
|
||||
// 1) currentPage 直接是一级 menu id
|
||||
const mById = menuList.find(m => m.id === page)
|
||||
if (mById) {
|
||||
const leaf = firstLeafOfMenu(mById)
|
||||
return { activeMenuId: mById.id, activeSubId: leaf?.id ?? '' }
|
||||
}
|
||||
|
||||
// 2) currentPage 是 path(/pages/xxx 或 pages/xxx)
|
||||
const pageNorm = normalize(page)
|
||||
|
||||
export function findActiveByCurrentPage(menuList: MenuItem[], currentPage: string) {
|
||||
// currentPage 既可能是顶级菜单 id,也可能是子页面 id(如 user-list)
|
||||
// 返回:activeMenuId / activeSubId / activeGroupTitle
|
||||
for (const m of menuList) {
|
||||
if (m.id === currentPage) {
|
||||
return { activeMenuId: m.id, activeSubId: '', activeGroupTitle: '' }
|
||||
}
|
||||
const groups = m.groups || []
|
||||
const groups = m.groups ?? []
|
||||
|
||||
// group / child / 四级 全部扫描
|
||||
for (const g of groups) {
|
||||
for (const c of g.children) {
|
||||
if (c.id === currentPage) {
|
||||
return { activeMenuId: m.id, activeSubId: c.id, activeGroupTitle: g.title }
|
||||
}
|
||||
// group 叶子(可选)
|
||||
if (g.path && normalize(g.path) === pageNorm) {
|
||||
return { activeMenuId: m.id, activeSubId: '' }
|
||||
}
|
||||
|
||||
const cs = g.children ?? []
|
||||
for (const c of cs) {
|
||||
// 用 id 命中
|
||||
if (c.id === page) return { activeMenuId: m.id, activeSubId: c.id }
|
||||
// 用 path 命中
|
||||
if (c.path && normalize(c.path) === pageNorm) return { activeMenuId: m.id, activeSubId: c.id }
|
||||
|
||||
// 四级
|
||||
const ds = c.children ?? []
|
||||
const hit = findInChildren(ds, page, pageNorm)
|
||||
if (hit) return { activeMenuId: m.id, activeSubId: hit.id }
|
||||
}
|
||||
}
|
||||
}
|
||||
return { activeMenuId: menuList[0]?.id || 'home', activeSubId: '', activeGroupTitle: '' }
|
||||
|
||||
// 3) 找不到:兜底 home
|
||||
return { activeMenuId: 'home', activeSubId: '' }
|
||||
}
|
||||
|
||||
function findInChildren(list: MenuChild[], targetId: string, targetPathNorm: string): MenuChild | null {
|
||||
for (const n of list) {
|
||||
if (n.id === targetId) return n
|
||||
if (n.path && normalize(n.path) === targetPathNorm) return n
|
||||
const deep = n.children ?? []
|
||||
if (deep.length > 0) {
|
||||
const hit = findInChildren(deep, targetId, targetPathNorm)
|
||||
if (hit) return hit
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function normalize(p: string): string {
|
||||
if (!p) return ''
|
||||
const s = p.startsWith('/') ? p.slice(1) : p
|
||||
const q = s.indexOf('?')
|
||||
return q >= 0 ? s.slice(0, q) : s
|
||||
}
|
||||
|
||||
// 你可以把 index.uvue 里的 firstLeafOfMenu 移到这里复用(保持逻辑一致)
|
||||
function firstLeafOfMenu(m: MenuItem): MenuChild | null {
|
||||
const gs = m.groups ?? []
|
||||
if (gs.length === 0) return null
|
||||
const g0 = gs[0]
|
||||
const cs = g0.children ?? []
|
||||
if (cs.length === 0) return null
|
||||
let n = cs[0]
|
||||
while (n.children && n.children.length > 0) n = n.children[0]
|
||||
return n
|
||||
}
|
||||
|
||||
|
||||
export function getCurrentRoutePath(): string {
|
||||
// 使用页面栈获取当前路由(uni-app标准能力)
|
||||
// getCurrentPages 用于获取当前页面栈实例 :contentReference[oaicite:2]{index=2}
|
||||
|
||||
Reference in New Issue
Block a user