import { pages, groups } from '@/pages.json' import { state } from '@/store/index.uts' type Group = { id : string, name : string, children : (Group | null)[] | null } type PageGroup = { id : string, name : string, index : number } type Page = { path : string, style : UTSJSONObject, group ?: string | null } export type MenuItem = { id : string, name : string, index : number, children : (MenuItem | null)[], pages : (Page | null)[] } export function generateMenu(tabBarType : string) : (MenuItem | null)[] { let res : (MenuItem | null)[] = [] const tabBarPages = JSON.parse(JSON.stringify(pages))!.filter((page : Page) : boolean => page.path.startsWith(tabBarType) && hasPageGroup(page.group)) const groupTree = JSON.parse<(Group | null)[]>(JSON.stringify(groups)) tabBarPages.forEach(page => { let menuItemArr : (MenuItem | null)[] = res let currentMenu : MenuItem | null = null const groupIndexList = page.group!.split(',').map((index : string) : number => parseInt(index)) let currentGroups : (Group | null)[] | null = groupTree const pageGroups : PageGroup[] = [] groupIndexList.forEach((groupIndex, index) => { if (index > 0) { pageGroups.push({ id: currentGroups![groupIndex]!.id, name: currentGroups![groupIndex]!.name, index: groupIndex } as PageGroup) } currentGroups = currentGroups![groupIndex]!.children }) const lastGroup = pageGroups[pageGroups.length - 1] let hasPageGroup = false if (page.path.endsWith(camelToDash(lastGroup.name)) && pageGroups.length > 1) { hasPageGroup = true pageGroups.pop() } const groupLength = pageGroups.length pageGroups.forEach((group, groupIndex) => { const { index, id, name } = group const validIndex = index fillMenuArrayWithNull(menuItemArr, validIndex) if (menuItemArr[validIndex] == null) { menuItemArr[validIndex] = { id: id.split('.').slice(-1)[0], name, index: validIndex, children: [] as (MenuItem | null)[], pages: [] as (Page | null)[] } as MenuItem } currentMenu = menuItemArr[validIndex] if (groupIndex < groupLength - 1) { menuItemArr = menuItemArr[validIndex]!.children as (MenuItem | null)[] } }) if (hasPageGroup) { const pageIndex = lastGroup.index fillPageArrayWithNull(currentMenu!.pages, pageIndex) if (currentMenu!.pages[pageIndex] == null) { currentMenu!.pages[pageIndex] = page } else { currentMenu!.pages.push(page) } } else { currentMenu!.pages.push(page) } }) return removeNullItem(res) } function hasPageGroup(value : string | null) : boolean { // #ifdef APP-ANDROID return value !== null // #endif // #ifndef APP-ANDROID return !!value // #endif } function isNumber(value : number | null) : boolean { // #ifdef APP-ANDROID return value !== null // #endif // #ifndef APP-ANDROID return typeof value === 'number' // #endif } function camelToDash(camelStr : string) : string { return camelStr.replace(/([A-Z])/g, '-$1').toLowerCase() } function fillMenuArrayWithNull(arr : (MenuItem | null)[], index : number) : void { const len = arr.length for (let i = 0; i <= index - (len - 1); i++) { arr.push(null) } } function fillPageArrayWithNull(arr : (Page | null)[], index : number) : void { const len = arr.length for (let i = 0; i <= index - (len - 1); i++) { arr.push(null) } } function removeNullItem(arr : (MenuItem | null)[]) : (MenuItem | null)[] { const res = arr.filter((item : MenuItem | null) : boolean => item !== null) res.forEach(menuItem => { addSetTabBarPage(menuItem!) // #ifdef APP-ANDROID menuItem.children = removeNullItem(menuItem.children) menuItem.pages = menuItem.pages.filter((item : Page | null) : boolean => item !== null) // #endif // #ifndef APP-ANDROID menuItem!.children = removeNullItem(menuItem!.children) menuItem!.pages = menuItem!.pages.filter((item : Page | null) : boolean => item !== null) // #endif }) return res } function addSetTabBarPage(menuItem : MenuItem) { const { id, name } = menuItem if (id == 'page' && name == '页面和路由' && !state.noMatchLeftWindow) { menuItem.pages.push({ path: 'set-tab-bar', style: { navigationBarTitleText: '设置 TabBar' } } as Page) } }