1- import {
2- createRouter ,
3- createWebHashHistory ,
4- } from 'vue-router'
5- import type { RouteRecordRaw } from 'vue-router'
1+ import { createRouter , createWebHashHistory } from 'vue-router'
62import { getToken , removeToken } from '@jetlinks-web/utils'
73import { 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
128let TokenFilterRoute : string [ ] = [ OAuth2 . path , AccountCenterBind . path , AUTHORIZE_ROUTE . path ]
139
1410let 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-
3112const 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
6142const 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
7051const 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