干燥机配套车间生产管理系统/云平台服务端
baoshiwei
2024-05-27 fa3ac93010bea3805438ee3ab0a182bfbf7423da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import type { RouteRecordNormalized } from 'vue-router'
import type { Menu, MenuModule } from '/@/router/types'
 
import { pathToRegexp } from 'path-to-regexp'
import { PermissionModeEnum } from '/@/enums/appEnum'
import { router } from '/@/router'
import { getAllParentPath, transformMenuModule } from '/@/router/helper/menuHelper'
import { useAppStoreWithOut } from '/@/store/modules/app'
import { usePermissionStore } from '/@/store/modules/permission'
import { filter } from '/@/utils/helper/treeHelper'
import { isUrl } from '/@/utils/is'
 
const modules = import.meta.glob('./modules/**/*.ts', { eager: true })
 
const menuModules: MenuModule[] = []
 
Object.keys(modules).forEach((key) => {
    const mod = (modules as Recordable)[key].default || {}
    const modList = Array.isArray(mod) ? [...mod] : [mod]
    menuModules.push(...modList)
})
 
// ===========================
// ==========Helper===========
// ===========================
 
const getPermissionMode = () => {
    const appStore = useAppStoreWithOut()
    return appStore.getProjectConfig.permissionMode
}
const isBackMode = () => {
    return getPermissionMode() === PermissionModeEnum.BACK
}
 
const isRouteMappingMode = () => {
    return getPermissionMode() === PermissionModeEnum.ROUTE_MAPPING
}
 
const isRoleMode = () => {
    return getPermissionMode() === PermissionModeEnum.ROLE
}
 
const staticMenus: Menu[] = []
;(() => {
    menuModules.sort((a, b) => {
        return (a.orderNo || 0) - (b.orderNo || 0)
    })
 
    for (const menu of menuModules) {
        staticMenus.push(transformMenuModule(menu))
    }
})()
 
async function getAsyncMenus() {
    const permissionStore = usePermissionStore()
    if (isBackMode()) {
        return permissionStore.getBackMenuList.filter((item) => !item.meta?.hideMenu && !item.hideMenu)
    }
    if (isRouteMappingMode()) {
        return permissionStore.getFrontMenuList.filter((item) => !item.hideMenu)
    }
    return staticMenus
}
 
export const getMenus = async (): Promise<Menu[]> => {
    const menus = await getAsyncMenus()
    if (isRoleMode()) {
        const routes = router.getRoutes()
        return filter(menus, basicFilter(routes))
    }
    return menus
}
 
export async function getCurrentParentPath(currentPath: string) {
    const menus = await getAsyncMenus()
    const allParentPath = await getAllParentPath(menus, currentPath)
    return allParentPath?.[0]
}
 
// Get the level 1 menu, delete children
export async function getShallowMenus(): Promise<Menu[]> {
    const menus = await getAsyncMenus()
    const shallowMenuList = menus.map((item) => ({ ...item, children: undefined }))
    if (isRoleMode()) {
        const routes = router.getRoutes()
        return shallowMenuList.filter(basicFilter(routes))
    }
    return shallowMenuList
}
 
// Get the children of the menu
export async function getChildrenMenus(parentPath: string) {
    const menus = await getMenus()
    const parent = menus.find((item) => item.path === parentPath)
    if (!parent || !parent.children || !!parent?.meta?.hideChildrenInMenu) {
        return [] as Menu[]
    }
    if (isRoleMode()) {
        const routes = router.getRoutes()
        return filter(parent.children, basicFilter(routes))
    }
    return parent.children
}
 
function basicFilter(routes: RouteRecordNormalized[]) {
    return (menu: Menu) => {
        const matchRoute = routes.find((route) => {
            if (isUrl(menu.path)) return true
 
            if (route.meta?.carryParam) {
                return pathToRegexp(route.path).test(menu.path)
            }
            const isSame = route.path === menu.path
            if (!isSame) return false
 
            if (route.meta?.ignoreAuth) return true
 
            return isSame || pathToRegexp(route.path).test(menu.path)
        })
 
        if (!matchRoute) return false
        menu.icon = (menu.icon || matchRoute.meta.icon) as string
        menu.meta = matchRoute.meta
        return true
    }
}