forked from mtianyan/vue-mooc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (160 loc) · 3.67 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/index.js'
import { getUserInfo } from 'utils/cache.js'
Vue.use(Router)
const Home = () => import('pages/home/index.vue') // 首页路由
const CourseIndex = () => import('pages/course/index.vue') // 免费课程路由
const CourseDetail = () => import('pages/course-detail/index.vue') // 免费课程详情路由
const LessonIndex = () => import('pages/lesson/index.vue') // 实战课程路由
const LessonDetail = () => import('pages/lesson-detail/index.vue') // 实战课程详情路由
const ReadIndex = () => import('pages/read/index.vue') // 专栏路由
const ReadDetaiil = () => import('pages/read-detail/index.vue') // 专栏详情路由
const QuestionIndex = () => import('pages/question/index.vue') // 猿问路由
const ArticleIndex = () => import('pages/article/index.vue') // 手记路由
const UserCenter = () => import('pages/user/index.vue') // 个人中心路由
const UserCourse = () => import('pages/user-course/index.vue') // 我的课程路由
const OrderIndex = () => import('pages/order/index.vue') // 订单中心路由
const NoticeIndex = () => import('pages/notice/index.vue') // 消息中心路由
const CartIndex = () => import('pages/cart/cart.vue') // 购物车路由
const CartConfirm = () => import('pages/cart/confirm.vue') // 购物车确认路由
const CartPay = () => import('pages/cart/pay.vue') // 购物车支付路由
const IntegralIndex = () => import('pages/integral/index.vue') // 积分商场路由
const routes = [
{
path: '/',
name: 'Index',
redirect: '/home'
},
{
path: '/home',
name: 'Home',
component:Home
},
{
path: '/user',
name: 'UserCenter',
component:UserCenter,
meta: {
requireAuth: true
}
},
{
path: '/user/course',
name: 'UserCourse',
component: UserCourse,
meta: {
requireAuth: true
}
},
{
path: '/order',
name: 'OrderIndex',
component:OrderIndex,
meta: {
requireAuth: true
}
},
{
path: '/course',
name: 'CourseIndex',
component: CourseIndex,
},
{
path: '/course/:id',
name: 'CourseDetail',
component: CourseDetail
},
{
path: '/lesson',
name: 'LessonIndex',
component:LessonIndex
},
{
path: '/lesson/:id',
name: 'LessonDetail',
component: LessonDetail
},
{
path: '/notice',
name: 'NoticeIndex',
component:NoticeIndex,
meta: {
requireAuth: true
}
},
{
path: '/cart',
name: 'CartIndex',
component:CartIndex,
meta: {
requireAuth: true
}
},
{
path: '/cart/confirm',
name: 'CartConfirm',
component:CartConfirm,
meta: {
requireAuth: true
}
},
{
path: '/cart/pay/:code',
name: 'CartPay',
component:CartPay,
meta: {
requireAuth: true
}
},
{
path: '/read',
name: 'ReadIndex',
component:ReadIndex,
},
{
path: '/read/:id',
name: 'ReadDetaiil',
component:ReadDetaiil,
},
{
path: '/question',
name: 'QuestionIndex',
component:QuestionIndex
},
{
path: '/article',
name: 'ArticleIndex',
component:ArticleIndex
},
{
path: '/integral',
name: 'IntegralIndex',
component: IntegralIndex
}
]
const router = new Router({
routes: routes,
scrollBehavior () {
return {
x: 0,
y: 0
}
}
})
// 路由拦截
router.beforeEach((to, from, next) => {
let userinfo = getUserInfo()
if (to.meta.requireAuth) {
if (userinfo.id) {
next()
} else{
store.commit('login/SET_LOGIN_ACTION', 'login')
store.commit('login/SET_SHOW_LOGIN', true)
next('/home')
}
} else {
next()
}
})
export default router