forked from EasyDarwin/EasyDarwin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
86 lines (80 loc) · 2.65 KB
/
router.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import Vue from 'vue'
import Router from 'vue-router'
import store from './store'
import AdminLTE from 'components/AdminLTE.vue'
const Dashboard = () => import(/* webpackChunkName: 'dashboard' */ 'components/Dashboard.vue')
const PusherList = () => import(/* webpackChunkName: 'pushers' */ 'components/PusherList.vue')
const PlayerList = () => import(/* webpackChunkName: 'players' */ 'components/PlayerList.vue')
const User = () => import(/* webpackChunkName: 'user' */ 'components/User.vue')
const About = () => import(/* webpackChunkName: 'about' */ 'components/About.vue')
Vue.use(Router);
const router = new Router({
routes: [
{
path: '/',
component: AdminLTE,
// meta: { needLogin: true },
children: [
{
path: '',
component: Dashboard,
props: true
}, {
path: 'pushers/:page?',
component: PusherList,
props: true
}, {
path: 'players/:page?',
component: PlayerList,
props: true
}, {
path: 'users/:page?',
component: User,
props: true
}, {
path: 'about',
component: About
}, {
path: 'logout',
async beforeEnter(to, from, next) {
await store.dispatch("logout");
window.location.href = `/login.html`;
}
}, {
path: '*',
redirect: '/'
}
]
}
],
linkActiveClass: 'active'
})
router.beforeEach(async (to, from, next) => {
var userInfo = await store.dispatch("getUserInfo");
if (!userInfo) {
if (to.matched.some((record => {
return record.meta.needLogin || record.meta.role;
}))) {
window.location.href = '/login.html';
return;
}
} else {
var roles = userInfo.roles||[];
var menus = store.state.menus.reduce((pval, cval) => {
pval[cval.path] = cval;
return pval;
},{})
var _roles = [];
var menu = menus[to.path];
if(menu) {
_roles.push(...(menu.roles||[]));
}
if(_roles.length > 0 && !_roles.some(val => {
return roles.indexOf(val) >= 0;
})) {
return;
}
}
next();
})
export default router;