/
index.js
66 lines (62 loc) 路 1.61 KB
/
index.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
import Vue from 'vue'
import Router from 'vue-router'
import Cookies from 'js-cookie'
import store from '@/store/index.js'
Vue.use(Router);
let routes = new Router({
linkActiveClass: 'active',
mode: 'history',
routes: [{
path: '/list',
name: 'list',
meta: {
requiresAuth: true
},
component: () =>
import ( /* webpackChunkName: "list" */ '@/page/list/list.vue')
}, {
path: '/edit/:id',
name: 'edit',
meta: {
requiresAuth: true
},
component: () =>
import ( /* webpackChunkName: "edit" */ '@/page/edit/edit.vue')
}, {
path: '/detail/:id',
name: 'detail',
meta: {
requiresAuth: true
},
component: () =>
import ( /* webpackChunkName: "detail" */ '@/page/detail/detail.vue')
}, {
path: '/login',
name: 'login',
component: () =>
import ( /* webpackChunkName: "login" */ '@/page/login/login.vue')
}]
});
routes.beforeEach((to, from, next) => {
let token = Cookies('username');
if (!to.name) {
next({
name: 'list'
});
} else if (to.name == 'login') {
if (!token || !store.state.auth.username) {
next();
} else {
next({
name: 'list'
});
}
} else if (to.params.status == 2 || (to.matched.some(record => record.meta.requiresAuth) && (!token || token === null))) {
next({
path: '/login'
});
} else {
next();
}
});
export default routes