permission.js
1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import router from './router'
import store from './store'
import { Message } from 'element-ui'
import { getToken, removeToken } from '@/utils/auth'
const whiteList = ['/login', '/auth-redirect', '/bind', '/register']
router.beforeEach((to, from, next) => {
if (getToken()) {
/* has token*/
if (to.path === '/login') {
next({ path: '/' })
} else {
if (store.getters.roles.length === 0) {
// 判断当前用户是否已拉取完user_info信息
store.dispatch('GetInfo').then(() => {
//获取用户表头
store.dispatch('GetTableHeaders').then(() => {
// 根据roles权限生成可访问的路由表
store.dispatch('GenerateRoutes').then(accessRoutes => {
router.addRoutes(accessRoutes) // 动态添加可访问路由表
next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
})
})
}).catch(err => {
})
} else {
next()
}
}
} else {
removeToken()
// 没有token
//wsso登录时对后端重定向带来的token进行记录
let token = location.search.split("token=")[1]
if (token != undefined && token != '' && token != null) {
setToken(token)
next({ path: '/' })
} else {
next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
}
}
})
router.afterEach(() => {
})