Skip to content

Commit 4bb1d9c

Browse files
committed
refactor: 适配子模块默认路由映射
1 parent a0af63d commit 4bb1d9c

File tree

3 files changed

+236
-237
lines changed

3 files changed

+236
-237
lines changed

src/router/globModules.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
import {modules} from '@/utils/modules'
1+
import { modules } from '@/utils/modules'
22

33
const routerModules = import.meta.glob('../views/**/index.vue')
44

55
export const getAsyncRoutesMap = () => {
66
const modulesMap: Record<string, any> = {}
7-
Object.keys(routerModules).forEach(item => {
7+
Object.keys(routerModules).forEach((item) => {
88
const code = item.replace('../views/', '').replace('/index.vue', '')
99
modulesMap[code] = routerModules[item]
1010
})
1111

1212
return modulesMap
1313
}
1414

15-
1615
export const getGlobModules = async () => {
1716
const asyncRoutesMap = getAsyncRoutesMap()
1817

1918
const modulesFiles = modules()
20-
Object.values(modulesFiles).forEach(item => {
19+
Object.values(modulesFiles).forEach((item) => {
2120
const routes = item.default.getAsyncRoutesMap?.() || []
2221
Object.assign(asyncRoutesMap, routes)
2322
})
@@ -26,3 +25,15 @@ export const getGlobModules = async () => {
2625
...asyncRoutesMap
2726
}
2827
}
28+
29+
export const getDefaultModules = (tokenFilterRoute?: any) => {
30+
const modulesFiles = modules()
31+
const _modules: any[] = []
32+
Object.values(modulesFiles).forEach((item) => {
33+
const modules = item.default.getDefaultRoutes?.() || []
34+
const filter = item.default.getFilterRoutes?.() || []
35+
_modules.push(...modules)
36+
tokenFilterRoute && tokenFilterRoute.push(...filter)
37+
})
38+
return _modules
39+
}

src/router/index.ts

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,14 @@
1-
import {
2-
createRouter,
3-
createWebHashHistory,
4-
} from 'vue-router'
5-
import type { RouteRecordRaw } from 'vue-router'
1+
import { createRouter, createWebHashHistory } from 'vue-router'
62
import { getToken, removeToken } from '@jetlinks-web/utils'
73
import { NOT_FIND_ROUTE, LOGIN_ROUTE, OAuth2, OAuthWechat, AccountCenterBind, AUTHORIZE_ROUTE } from './basic'
8-
import {isSubApp} from '@/utils/consts'
9-
import { useApplication, useUserStore, useSystemStore, useMenuStore } from '@/store'
10-
import { modules } from '@/utils/modules'
4+
import { isSubApp } from '@/utils/consts'
5+
import { useApplication, useUserStore, useSystemStore, useMenuStore } from '@/store'
6+
import { getDefaultModules } from './globModules'
117

128
let TokenFilterRoute: string[] = [OAuth2.path, AccountCenterBind.path, AUTHORIZE_ROUTE.path]
139

1410
let FilterPath: string[] = [OAuth2.path, AUTHORIZE_ROUTE.path]
1511

16-
// 获取子模块默认路由
17-
const getModulesRoutes = () => {
18-
const modulesFiles = modules()
19-
const _routes: RouteRecordRaw[] = []
20-
Object.values(modulesFiles).forEach((item: any) => {
21-
const routes = item.default.getDefaultRoutes?.() || []
22-
const filter = item.default.getFilterRoutes?.() || []
23-
24-
_routes.push(...routes)
25-
TokenFilterRoute.push(...filter)
26-
})
27-
return _routes
28-
}
29-
30-
3112
const router = createRouter({
3213
history: createWebHashHistory(),
3314
routes: [
@@ -36,14 +17,14 @@ const router = createRouter({
3617
OAuthWechat,
3718
AccountCenterBind,
3819
AUTHORIZE_ROUTE,
39-
...getModulesRoutes()
20+
...getDefaultModules(TokenFilterRoute)
4021
],
4122
scrollBehavior(to, from, savedPosition) {
4223
// 子应用路由变化时通知基座
4324
if (isSubApp && to.path !== from.path) {
4425
setTimeout(() => {
4526
if ((window as any).microApp?.dispatch) {
46-
(window as any).microApp.dispatch({
27+
;(window as any).microApp.dispatch({
4728
type: 'route-change',
4829
data: {
4930
path: to.path,
@@ -54,27 +35,27 @@ const router = createRouter({
5435
}, 0)
5536
}
5637

57-
return savedPosition || {top: 0}
58-
},
38+
return savedPosition || { top: 0 }
39+
}
5940
})
6041

6142
const NoTokenJump = (to: any, next: any, isLogin: boolean) => {
6243
// 登录页,不需要token 的页面直接放行,否则跳转登录页
6344
if (isLogin || TokenFilterRoute.includes(to.path)) {
6445
next()
6546
} else {
66-
next({path: LOGIN_ROUTE.path})
47+
next({ path: LOGIN_ROUTE.path })
6748
}
6849
}
6950

7051
const getRoutesByServer = async (to: any, next: any) => {
71-
7252
const UserInfoStore = useUserStore()
7353
const SystemStore = useSystemStore()
7454
const MenuStore = useMenuStore()
7555
const application = useApplication()
7656

77-
if (!Object.keys(UserInfoStore.userInfo).length && !isSubApp) { // 不是微前端的情况下
57+
if (!Object.keys(UserInfoStore.userInfo).length && !isSubApp) {
58+
// 不是微前端的情况下
7859
// 是否有用户信息
7960
await UserInfoStore.getUserInfo()
8061
//
@@ -83,19 +64,21 @@ const getRoutesByServer = async (to: any, next: any) => {
8364
await SystemStore.queryInfo()
8465
}
8566

86-
if (isSubApp && !Object.keys(SystemStore.microApp).length) { // 获取基座给的菜单信息
67+
if (isSubApp && !Object.keys(SystemStore.microApp).length) {
68+
// 获取基座给的菜单信息
8769
const data = (window as any).microApp.getData() // 获取主应用下发的数据
8870
SystemStore.microApp.value = data
8971
await MenuStore.createRoutes(data.menuResult)
9072

9173
MenuStore.menu.forEach((r) => {
9274
router.addRoute(r)
9375
})
94-
router.addRoute( NOT_FIND_ROUTE)
95-
await next({...to, replace: true})
76+
router.addRoute(NOT_FIND_ROUTE)
77+
await next({ ...to, replace: true })
9678
}
9779

98-
if (!isSubApp && !application.appList.length) { // 是否开启微前端
80+
if (!isSubApp && !application.appList.length) {
81+
// 是否开启微前端
9982
await application.queryApplication() // 获取子应用
10083

10184
// 初始化微前端配置
@@ -126,8 +109,8 @@ const getRoutesByServer = async (to: any, next: any) => {
126109
MenuStore.menu.forEach((r) => {
127110
router.addRoute(r)
128111
})
129-
router.addRoute( NOT_FIND_ROUTE)
130-
await next({...to, replace: true})
112+
router.addRoute(NOT_FIND_ROUTE)
113+
await next({ ...to, replace: true })
131114
}
132115
} else {
133116
next()
@@ -139,7 +122,7 @@ router.beforeEach((to, from, next) => {
139122
const isLogin = to.path === LOGIN_ROUTE.path
140123
if (token) {
141124
if (isLogin) {
142-
next({path: '/'})
125+
next({ path: '/' })
143126
} else {
144127
getRoutesByServer(to, next)
145128
}
@@ -152,7 +135,7 @@ export const jumpLogin = () => {
152135
setTimeout(() => {
153136
removeToken()
154137
router.replace({
155-
path: LOGIN_ROUTE.path,
138+
path: LOGIN_ROUTE.path
156139
})
157140
})
158141
}

0 commit comments

Comments
 (0)