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
| import Vue from "vue";
| import VueRouter from "vue-router";
| import store from "../store";
|
| Vue.use(VueRouter);
|
| const routes = [
| {
| path: "/",
| redirect: "/home",
| },
| {
| path: "/home",
| name: "home",
| component: () => import(/* webpackChunkName: "home" */ "../views/home/Home.vue"),
| },
| {
| path: "/login",
| name: "login",
| component: () =>
| import(/* webpackChunkName: "login" */ "../views/login.vue"),
| },
| {
| path: "/register",
| name: "register",
| component: () =>
| import(/* webpackChunkName: "register" */ "../views/register.vue"),
| },
| {
| path: "/account",
| name: "account",
| component: () =>
| import(/* webpackChunkName: "account" */ "../views/account/account.vue"),
| },
| {
| path: "/fund",
| name: "fund",
| component: () =>
| import(/* webpackChunkName: "fund" */ "../views/fund/fund.vue"),
| },
| {
| path: "/ipo",
| name: "ipo",
| component: () =>
| import(/* webpackChunkName: "ipo" */ "../views/ipo/ipo.vue"),
| },
| {
| path: "/blockTrading",
| name: "blockTrading",
| component: () =>
| import(
| /* webpackChunkName: "blockTrading" */ "../views/blockTrading/blockTrading.vue"
| ),
| },
| {
| path: "/aiTrading",
| name: "aiTrading",
| component: () =>
| import(
| /* webpackChunkName: "aiTrading" */ "../views/aiTrading/aiTrading.vue"
| ),
| },
| ];
|
| const router = new VueRouter({
| routes,
| // 避免重复导航错误
| scrollBehavior: () => ({ y: 0 }),
| });
|
| // 路由守卫
| router.beforeEach((to, from, next) => {
| // 获取token
| const token =
| store.state.token || window.localStorage.getItem("USERTOKEN") || null;
| // 白名单路由,不需要登录就可以访问
| const whiteList = ["/login", "/register"];
|
| // 避免重复导航到当前位置
| if (to.path === from.path) {
| return next(false);
| }
|
| // // 如果已登录且要去登录或注册页
| // if (token && whiteList.includes(to.path)) {
| // // 已登录,跳转到首页
| // next("/");
| // } else if (whiteList.includes(to.path)) {
| // // 未登录,要去白名单页面,直接放行
| // next();
| // } else {
| // // 不在白名单中,需要验证token
| // if (token) {
| // // 有token,放行
| // next();
| // } else {
| // // 没有token,跳转到登录页
| // next("/login");
| // }
| // }
|
| if (to.meta.requireAuth && token) {
| next("/login");
| return;
| }
| next();
| });
|
| // 捕获导航错误
| const originalPush = VueRouter.prototype.push;
| VueRouter.prototype.push = function push(location) {
| return originalPush.call(this, location).catch((err) => {
| if (err.name !== "NavigationDuplicated") throw err;
| });
| };
|
| const originalReplace = VueRouter.prototype.replace;
| VueRouter.prototype.replace = function replace(location) {
| return originalReplace.call(this, location).catch((err) => {
| if (err.name !== "NavigationDuplicated") throw err;
| });
| };
|
| export default router;
|
|