-
Notifications
You must be signed in to change notification settings - Fork 178
/
useRouteStore.ts
175 lines (163 loc) · 6.1 KB
/
useRouteStore.ts
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import type { Plugin, SystemSettings } from '#/global'
/**
* MineAdmin is committed to providing solutions for quickly building web applications
* Please view the LICENSE file that was distributed with this source code,
* For the full copyright and license information.
* Thank you very much for using MineAdmin.
*
* @Author X.Mo<[email protected]>
* @Link https://github.com/mineadmin
*/
import type { Router, RouteRecordRaw } from 'vue-router'
import dashboardRoute from '@/router/static-routes/dashboardRoute'
import welcomeRoute from '@/router/static-routes/welcomeRoute'
import usePluginStore from '@/store/modules/usePluginStore.ts'
const useRouteStore = defineStore(
'useRouteStore',
() => {
const defaultSetting = ref<SystemSettings.all>(useDefaultSetting())
// 原始路由
const routesRaw = ref<RouteRecordRaw[]>([])
const flatteningRoutesList = ref<RouteRecordRaw[]>([])
async function initRoutes(router: Router, routes: any[]) {
const MineRootLayoutRoute = getMineRootLayoutRoute()
router.hasRoute('MineRootLayoutRoute') && router.removeRoute('MineRootLayoutRoute')
router.addRoute(MineRootLayoutRoute)
routesRaw.value = router.getRoutes()
routes = menuToRoutes(routes)
router.getRoutes().find((route, key) => {
if (route.name === 'MineRootLayoutRoute') {
// @ts-expect-error eslint-disable-next-line ts/ban-ts-comment
routesRaw.value[key].children.push(...routes)
}
})
flatteningRoutesList.value = flatteningRoutes(routes)
flatteningRoutesList.value.map((routeItem: RouteRecordRaw) => router.addRoute('MineRootLayoutRoute', routeItem))
const plugins = usePluginStore().getPluginConfig() as { [ key: string ]: Plugin.PluginConfig }
Object.keys(plugins).map((name: string) => {
const plugin = plugins[name] as Plugin.PluginConfig
if (plugin.config?.enable === true && plugin?.views) {
plugin.views.map((item: Plugin.Views) => {
const route = toRecordRawRoute(item)
MineRootLayoutRoute.children?.push(route)
router.addRoute('MineRootLayoutRoute', route)
})
}
})
await usePluginStore().callHooks('registerRoute', router, routesRaw.value)
}
function getMineRootLayoutRoute(): RouteRecordRaw {
const welcomePage: SystemSettings.welcomePage = defaultSetting.value.welcomePage
return {
name: 'MineRootLayoutRoute',
path: '/',
component: () => import('@/layouts'),
redirect: welcomePage.path,
children: [
Object.assign(welcomeRoute, {
name: welcomePage.name,
path: welcomePage.path,
meta: {
title: welcomePage.title,
i18n: 'menu.welcome',
icon: welcomePage.icon,
type: 'M',
affix: true,
breadcrumbEnable: true,
copyright: true,
cache: true,
},
}),
toRecordRawRoute(dashboardRoute),
toRecordRawRoute({
path: '/:pathMatch(.*)*',
name: 'MineSystemError',
component: () => import(('@/layouts/[...all].tsx')),
meta: {
hidden: true,
i18n: 'menu.pageError',
},
}),
],
}
}
/**
* 扁平化为一层路由
*/
function flatteningRoutes(routes: any[] = [], breadcrumb: any[] = []) {
const res: any = []
routes.forEach((route) => {
const tmp = { ...route }
if (tmp.children) {
const childrenBreadcrumb = [...breadcrumb]
childrenBreadcrumb.push(route)
const tmpRoute = { ...route }
tmpRoute.meta = tmpRoute?.meta ?? {}
tmpRoute.meta.breadcrumb = childrenBreadcrumb
delete tmpRoute.children
res.push(tmpRoute)
const childrenRoutes = flatteningRoutes(tmp.children, childrenBreadcrumb)
childrenRoutes.map((item: any) => res.push(item))
}
else {
const tmpBreadcrumb = [...breadcrumb]
tmpBreadcrumb.push(tmp)
tmp.meta = tmp?.meta ?? {}
tmp.meta.breadcrumb = tmpBreadcrumb
res.push(tmp)
}
})
return res
}
function toRecordRawRoute(route: any) {
return flatteningRoutes([route])[0].meta.breadcrumb[0]
}
const moduleViews = import.meta.glob('../../modules/**/views/**/**.{vue,jsx,tsx}')
const pluginViews = import.meta.glob('../../plugins/*/**/views/**/**.{vue,jsx,tsx}')
/**
* 菜单转路由
* @param routerMap
*/
function menuToRoutes(routerMap: any[]) {
const accessedRouters: any = []
routerMap.forEach((item: any) => {
if (item.meta?.type !== 'B') {
if (item.meta.type === 'I') {
item.path = `/MineIframe/${item.name}`
item.component = () => import(('@/layouts/components/iframe/index.tsx'))
}
const suffix: string = item.meta?.componentSuffix ?? '.vue'
let component: any | null = null
if (item.component && item.meta?.type !== 'I') {
if (moduleViews[`../../modules/${item.component}${suffix}`]) {
component = moduleViews[`../../modules/${item.component}${suffix}`]
}
else if (pluginViews[`../../plugins/${item.component}${suffix}`]) {
component = pluginViews[`../../plugins/${item.component}${suffix}`]
}
else {
// console.warn(`MineAdmin-UI: 路由 [${item.meta.title}] 找不到 ${item.component}${suffix} 页面`)
}
}
const route = {
path: item.path,
name: item.name,
meta: item.meta,
children: item.children ? menuToRoutes(item.children) : null,
component: item.meta?.type === 'I' ? item.component : component,
}
accessedRouters.push(route)
}
})
return accessedRouters
}
return {
initRoutes,
toRecordRawRoute,
flatteningRoutes,
routesRaw,
flatteningRoutesList,
}
},
)
export default useRouteStore