@@ -5,23 +5,21 @@ import NProgress from 'nprogress'
55import 'nprogress/nprogress.css'
66import { tokenFromStorage , userInfoFromStorage } from 'UTILS/storage'
77import { MessageBox } from 'element-ui'
8- import createDynamicRoutes from './create-routes'
8+ // import createDynamicRoutes from './create-routes'
9+ import createDynamicRoutes from './filter-routes'
910import constantRoutes from 'ROUTER/routes/constant'
1011
1112NProgress . configure ( { showSpinner : false } )
1213
13- const WHITELIST = [
14- '/login'
15- ]
14+ const WHITELIST = [ '/login' ]
1615
1716// Used to determine whether private routes has been added
1817let HAS_ROUTES_ADDED = false
1918
20- function setDynamicRoutesToStorage ( roles ) {
21- const currentRole = Array . isArray ( roles ) ? roles [ 0 ] : roles
19+ function setDynamicRoutesToStorage ( ) {
2220 store . commit (
2321 `login/${ loginTypes . SET_DYNAMIC_ROUTES } ` ,
24- createDynamicRoutes ( currentRole )
22+ createDynamicRoutes ( store . getters [ 'login/accessMap' ] )
2523 )
2624}
2725
@@ -40,24 +38,27 @@ function errorHandler (e, next, redirectPath) {
4038 showClose : false
4139 } )
4240 . then ( ( ) => store . dispatch ( 'login/userLogout' ) )
43- . then ( ( ) => next ( {
44- path : `/login?redirect=${ redirectPath } ` ,
45- replace : true
46- } ) )
41+ . then ( ( ) =>
42+ next ( {
43+ path : `/login?redirect=${ redirectPath } ` ,
44+ replace : true
45+ } )
46+ )
4747 NProgress . done ( )
4848 console . error ( e )
4949}
5050
5151function addRoutesToRouter ( ) {
5252 router . addRoutes ( store . getters [ 'login/dynamicRoutes' ] )
5353 console . log (
54- '%c[Routes creation]: routes has been added!' , 'color: yellow' ,
54+ '%c[Routes creation]: routes has been added!' ,
55+ 'color: yellow' ,
5556 store . getters [ 'login/dynamicRoutes' ]
5657 )
5758}
5859
5960function createRoutesMap ( to , next ) {
60- setDynamicRoutesToStorage ( store . getters [ 'login/role' ] )
61+ setDynamicRoutesToStorage ( )
6162 setGlobalRoutesToStorage ( )
6263 addRoutesToRouter ( )
6364 HAS_ROUTES_ADDED = true
@@ -80,16 +81,13 @@ router.beforeEach((to, from, next) => {
8081
8182 // ! State: User has been logged in (local token).
8283 if ( tokenFromStorage . getItem ( ) ) {
83- // 1. fetch RolesMap if necessary (when RolesMap stored by back-end)
84-
85- // local storage has a accessToken record, but current `login/accessToken`
84+ // (Optional) local storage has a accessToken record, but current `login/accessToken`
8685 // vuex state is empty string when user activate a new session (eg. new
8786 // browser tab)
87+
8888 if ( ! store . getters [ 'login/accessToken' ] ) {
89- return store . dispatch (
90- 'login/fetchUserAccess' ,
91- tokenFromStorage . getItem ( )
92- )
89+ return store
90+ . dispatch ( 'login/fetchUserAccess' , tokenFromStorage . getItem ( ) )
9391 . then ( ( ) => {
9492 // fill vuex state with user information to prevent infinity loop.
9593 store . commit (
@@ -105,38 +103,38 @@ router.beforeEach((to, from, next) => {
105103 . catch ( e => errorHandler ( e , next , to . path ) )
106104 }
107105
108- // 2 . confirm route access by user role , including global routes creation.
109- if ( ! store . getters [ 'login/role' ] ) {
106+ // step: 1 . confirm route access by user access , including global routes creation.
107+ if ( ! store . getters [ 'login/accesses' ] . length ) {
110108 // 2.1 fetch user role, then routes creation based on user role.
111- return store . dispatch (
112- 'login/fetchUserAccess' ,
113- tokenFromStorage . getItem ( )
114- )
109+ return store
110+ . dispatch ( 'login/fetchUserAccess' , tokenFromStorage . getItem ( ) )
115111 . then ( ( ) => createRoutesMap ( to , next ) )
116112 . catch ( e => errorHandler ( e , next , to . path ) )
117113 }
118114
119- // (2 .2 optional) Regenerate private routes based on user role when page
115+ // (step 1 .2 optional) Regenerate private routes based on user role when page
120116 // reload, because vuex state will be preserved by vuex-persistedstate when
121117 // page reload.
122- if ( store . getters [ 'login/role' ] && ! HAS_ROUTES_ADDED ) {
118+ if ( store . getters [ 'login/accesses' ] . length && ! HAS_ROUTES_ADDED ) {
123119 console . log (
124120 '%c[Routes creation]: Activate private routes regeneration process !' ,
125121 'color: yellow;'
126122 )
127123 return createRoutesMap ( to , next )
128124 }
129125
130- // 2.2 filter route
131- if ( ! to . meta . roles ||
132- to . meta . roles . includes ( store . getters [ 'login/role' ] [ 0 ] ) ) {
133- next ( )
134- } else {
135- next ( {
136- path : `/403?redirect=${ to . path } ` ,
137- replace : true
138- } )
139- }
126+ next ( )
127+
128+ // step: real-time routes filter
129+ // ! 如有动态权限验证的需求
130+ // if (!to.meta.access || validateAccess(route, to.meta.access)) {
131+ // next()
132+ // } else {
133+ // next({
134+ // path: `/403?redirect=${to.path}`,
135+ // replace: true
136+ // })
137+ // }
140138 } else {
141139 // ! State: user logout
142140 next ( {
0 commit comments