¶Ô±ÈÐÂÎļþ |
| | |
| | | import router from './router' |
| | | import store from './store' |
| | | import { Message } from 'element-ui' |
| | | import NProgress from 'nprogress' |
| | | import 'nprogress/nprogress.css' |
| | | import { getToken } from '@/utils/auth' |
| | | |
| | | NProgress.configure({ showSpinner: false }) |
| | | |
| | | const whiteList = ['/login', '/auth-redirect', '/bind', '/register'] |
| | | |
| | | router.beforeEach((to, from, next) => { |
| | | NProgress.start() |
| | | if (getToken()) { |
| | | /* has token*/ |
| | | if (to.path === '/login') { |
| | | next({ path: '/' }) |
| | | NProgress.done() |
| | | } else { |
| | | if (store.getters.roles.length === 0) { |
| | | // 夿å½åç¨æ·æ¯å¦å·²æåå®user_infoä¿¡æ¯ |
| | | store.dispatch('GetInfo').then(res => { |
| | | // æåuser_info |
| | | const roles = res.roles |
| | | store.dispatch('GenerateRoutes', { roles }).then(accessRoutes => { |
| | | // æµè¯ é»è®¤éæé¡µé¢ |
| | | // store.dispatch('permission/generateRoutes', { roles }).then(accessRoutes => { |
| | | // æ ¹æ®rolesæéçæå¯è®¿é®çè·¯ç±è¡¨ |
| | | router.addRoutes(accessRoutes) // å¨ææ·»å å¯è®¿é®è·¯ç±è¡¨ |
| | | next({ ...to, replace: true }) // hackæ¹æ³ ç¡®ä¿addRoutes已宿 |
| | | }) |
| | | }) |
| | | .catch(err => { |
| | | store.dispatch('FedLogOut').then(() => { |
| | | Message.error(err) |
| | | next({ path: '/' }) |
| | | }) |
| | | }) |
| | | } else { |
| | | next() |
| | | // 没æå¨ææ¹åæéçéæ±å¯ç´æ¥next() å é¤ä¸æ¹æé夿 â |
| | | // if (hasPermission(store.getters.roles, to.meta.roles)) { |
| | | // next() |
| | | // } else { |
| | | // next({ path: '/401', replace: true, query: { noGoBack: true }}) |
| | | // } |
| | | // å¯å â |
| | | } |
| | | } |
| | | } else { |
| | | // 没ætoken |
| | | if (whiteList.indexOf(to.path) !== -1) { |
| | | // å¨å
ç»å½ç½ååï¼ç´æ¥è¿å
¥ |
| | | next() |
| | | } else { |
| | | next(`/login?redirect=${to.fullPath}`) // å¦åå
¨é¨éå®åå°ç»å½é¡µ |
| | | NProgress.done() |
| | | } |
| | | } |
| | | }) |
| | | |
| | | router.afterEach(() => { |
| | | NProgress.done() |
| | | }) |