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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
| import Vue from 'vue'
| import VueRouter from 'vue-router'
| import { getToken } from "../utils/auth";
|
| Vue.use(VueRouter);
|
| const routes = [
| {
| path: '/',
| redirect: '/login'
| },
| {
| path: '/login',
| component: () => import('../views/login')
| },
| {
| path: '/register',
| component: () => import('../views/register')
| },
| {
| path: '/layout',
| component: () => import('../layout'),
| children: [
| {
| path: '/user',
| component: () => import('../views/user')
| },
| {
| path: '',
| redirect: '/home'
| },
| {
| path: '/home',
| component: () => import('../views/home')
| },
|
|
| {
| path: '/power',
| component: () => import('../views/power')
| },
| {
| path: '/role',
| component: () => import('../views/role')
| },
| {
| path: '/device/parameter',
| component: () => import('../views/device/parameter'),
|
| },
| {
| path: '/device/talk',
| component: () => import('../views/device/talkvue'),
|
| },
| {
| path: '/device/alarm',
| component: () => import('../views/device/alarm')
| },
| {
| path: '/device/iostate',
| component: () => import('../views/device/iostate')
| },
| {
| path: '/device/control',
| component: () => import('../views/device/control')
| },
| {
| path: '/Electrical/Parameter',
| component: () => import('../views/Electrical/Parameter')
| },
| {
| path: '/Electrical/Action',
| component: () => import('../views/Electrical/Action')
| },
| {
| path: '/Electrical/Sign',
| component: () => import('../views/Electrical/Sign')
| },
| {
| path: '/Electrical/State',
| component: () => import('../views/Electrical/State')
| },
|
|
|
|
| ]
| }
| ];
|
| const router = new VueRouter({
| routes
| });
|
| // 路由控制守卫,解决需要登录才可以访问的页面
| router.beforeEach((to, from, next) => {
| // 如果访问的是登录或注册页面直接放行
| if (to.path === '/login' || to.path === '/register') return next();
|
| // 获取token
| const hasToken = getToken();
|
|
|
|
| const isExist = Vue.prototype.$tagList.some(tag => tag.path === to.fullPath);
|
| if (!isExist) {
| Vue.prototype.$tagList.push({
| path: to.fullPath,
| data: null
| });
| }
|
| // 没有token则跳转到登录页面
| if (!hasToken) return next('/login');
|
| // 有token则根据需要进行跳转
| if (to.path === '/') {
| // 如果目标路由是根路径,则跳转到首页
| return next('/home');
| }
|
| // 其他情况放行
| return next();
| });
|
| export default router
|
|