@@ -8,7 +8,7 @@ import getPageTitle from '@/utils/get-page-title'
88
99NProgress . configure ( { showSpinner : false } ) // NProgress Configuration
1010
11- const whiteList = [ '/login' , '/index' , '/line' , '/home' , '/404' , '/' , '' , '/echarts' , '/md' , '/out' , '/excel' , '/upload' ] // 不重定向白名单
11+ const whiteList = [ '/login' , '/auth-redirect' ] // no redirect whitelist
1212
1313router . beforeEach ( async ( to , from , next ) => {
1414 // start progress bar
@@ -26,15 +26,26 @@ router.beforeEach(async(to, from, next) => {
2626 next ( { path : '/' } )
2727 NProgress . done ( )
2828 } else {
29- const hasGetUserInfo = store . getters . name
30- if ( hasGetUserInfo ) {
29+ // determine whether the user has obtained his permission roles through getInfo
30+ const hasRoles = store . getters . roles && store . getters . roles . length > 0
31+ if ( hasRoles ) {
3132 next ( )
3233 } else {
3334 try {
3435 // get user info
35- await store . dispatch ( 'user/getInfo' )
36+ // note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
37+ const { roles } = await store . dispatch ( 'user/getInfo' )
3638
37- next ( )
39+ // generate accessible routes map based on roles
40+ const accessRoutes = await store . dispatch ( 'permission/generateRoutes' , roles )
41+
42+ // dynamically add accessible routes
43+ router . addRoutes ( accessRoutes )
44+ console . log ( router )
45+
46+ // hack method to ensure that addRoutes is complete
47+ // set the replace: true, so the navigation will not leave a history record
48+ next ( { ...to , replace : true } )
3849 } catch ( error ) {
3950 // remove token and go to login page to re-login
4051 await store . dispatch ( 'user/resetToken' )
0 commit comments