@@ -2,6 +2,7 @@ import Vue from 'vue'
2
2
import Router from 'vue-router'
3
3
import store from './../store'
4
4
import Home from '@/components/home'
5
+ import DGlobal from 'common/js/global.js'
5
6
// const Pic = r => require.ensure([], () => r(require('@/components/pic/pic.vue')), 'pic')
6
7
// import Pic from '@/components/pic/pic.vue'
7
8
const Music = r => require . ensure ( [ ] , ( ) => r ( require ( '@/components/music/music.vue' ) ) , 'music' )
@@ -60,7 +61,7 @@ const Reward = r => require.ensure([], () => r(require('@/components/reward/rewa
60
61
61
62
Vue . use ( Router )
62
63
63
- export default new Router ( {
64
+ const myRouter = new Router ( {
64
65
// mode: 'history',
65
66
hashbang : true ,
66
67
history : false , // 这个参数改为false就可以了
@@ -88,18 +89,27 @@ export default new Router({
88
89
{
89
90
path : '/' ,
90
91
redirect : store . getters . getMusicRouter ,
92
+ meta : {
93
+ auth : true
94
+ } ,
91
95
components : {
92
96
listinfo : MusicSheet
93
97
}
94
98
} ,
95
99
{
96
100
path : '/music/search' ,
101
+ meta : {
102
+ auth : true
103
+ } ,
97
104
components : {
98
105
fullscreen : MusicSearch
99
106
}
100
107
} ,
101
108
{
102
109
path : '/music/albumlist/:id' ,
110
+ meta : {
111
+ auth : true
112
+ } ,
103
113
name : 'albumlist' ,
104
114
components : {
105
115
listinfo : MusicAlbumList
@@ -109,39 +119,57 @@ export default new Router({
109
119
{
110
120
path : '/music/collection' ,
111
121
name : 'collection' ,
122
+ meta : {
123
+ auth : true
124
+ } ,
112
125
components : {
113
126
listinfo : MusicCollection
114
127
}
115
128
} ,
116
129
{
117
130
path : '/music/sheet/:id' ,
118
131
name : 'musicindex' ,
132
+ meta : {
133
+ auth : true
134
+ } ,
119
135
components : {
120
136
listinfo : MusicSheet
121
137
}
122
138
} ,
123
139
{
124
140
path : '/music/sheet/' ,
125
141
redirect : store . getters . getMusicRouter ,
142
+ meta : {
143
+ auth : true
144
+ } ,
126
145
components : {
127
146
listinfo : MusicSheet
128
147
}
129
148
} ,
130
149
{
131
150
path : '/music/playlist/' ,
151
+ meta : {
152
+ auth : true
153
+ } ,
132
154
components : {
133
155
listinfo : MusicPlayList
134
156
}
135
157
} ,
136
158
{
137
159
path : '/music/toplist/' ,
160
+ meta : {
161
+ auth : true
162
+ } ,
138
163
components : {
139
164
listinfo : MusicToplist
140
165
}
141
166
} ,
142
167
{
143
168
path : '/music/searchlist/:w' ,
144
169
name : 'searchlist' ,
170
+ meta : {
171
+ auth : true
172
+ } ,
145
173
components : {
146
174
listinfo : MusicSearchList
147
175
}
@@ -256,9 +284,35 @@ export default new Router({
256
284
} ,
257
285
{
258
286
path : '/user/info' ,
287
+ meta : {
288
+ auth : true
289
+ } ,
259
290
component : UserInfo
260
291
}
261
292
]
262
293
}
263
294
]
264
295
} )
296
+
297
+ myRouter . beforeEach ( ( to , from , next ) => {
298
+ if ( DGlobal . storage . getCookie ( 'c_user_info' ) && ! store . getters . getUserInfo ) {
299
+ DGlobal . storage . setCookie ( 'c_user_info' , unescape ( DGlobal . storage . getCookie ( 'c_user_info' ) ) , 60 * 60 * 1000 * 24 )
300
+ store . dispatch ( {
301
+ type : 'set_UserInfo' ,
302
+ data : JSON . parse ( unescape ( DGlobal . storage . getCookie ( 'c_user_info' ) ) )
303
+ } )
304
+ }
305
+
306
+ if ( to . meta && to . meta . auth ) {
307
+ if ( store . getters . getUserInfo ) {
308
+ next ( )
309
+ } else {
310
+ const url = encodeURIComponent ( to . fullPath )
311
+ next ( `/user/login?redirect_url=${ url } ` )
312
+ }
313
+ } else {
314
+ next ( )
315
+ }
316
+ } )
317
+
318
+ export default myRouter
0 commit comments