Skip to content

Commit 227337d

Browse files
committed
添加登陆拦截
1 parent ec1e74f commit 227337d

File tree

6 files changed

+7208
-12
lines changed

6 files changed

+7208
-12
lines changed

src/App.vue

-5
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,6 @@ export default {
183183
getIsBingBg (nowval, oldval) {
184184
this.fetchData()
185185
localStorage.setItem('globalInfo', JSON.stringify(store.getters.getGlobalInfo))
186-
},
187-
'$route': function () {
188-
this.updateLoginInfo()
189186
}
190187
},
191188
computed: {
@@ -228,8 +225,6 @@ export default {
228225
this.isApp()
229226
// 加载数据
230227
this.fetchData()
231-
// 判断是否登录
232-
this.updateLoginInfo()
233228
// 挂载 onresize事件
234229
window.onresize = () => {
235230
this.isApp()

src/common/api/user.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fecth from 'utils/fecth.js'
22
import store from 'store'
3-
import router from 'router'
3+
// import router from 'router'
44
const LOGIN_INFO = {
55
code: 0,
66
msg: '用户未登录'
@@ -21,7 +21,6 @@ export function todoUserInfo () {
2121
let loginInfo = store.getters.getUserInfo
2222
return new Promise((resolve, reject) => {
2323
if (loginInfo === null) {
24-
router.push('/user/login')
2524
reject(LOGIN_INFO)
2625
} else {
2726
resolve(loginInfo)

src/components/user/login/login.vue

+7-4
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@
7979
}).then((res) => {
8080
if (res.data.code === '1') {
8181
Storage.setCookie('c_user_info', JSON.stringify(res.data), 60 * 60 * 1000 * 24)
82-
this.$router.push('/home')
82+
this.$store.dispatch('set_UserInfo', res.data)
83+
const url = decodeURIComponent(this.$route.query['redirect_url'])
84+
if (url) {
85+
this.$router.push(url)
86+
} else {
87+
this.$router.push('/home')
88+
}
8389
} else {
8490
this.$msg(res.data.msg)
8591
}
@@ -137,9 +143,6 @@
137143
},
138144
components: {
139145
usercompletion
140-
},
141-
mounted () {
142-
// console.log(Utils.formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss'))
143146
}
144147
}
145148
</script>

src/main.js

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import App from './App'
55
import router from './router'
66
import VueLazyload from 'vue-lazyload'
77
import './../static/font-icon/style.css'
8+
import store from '@/store'
89
import Msg from 'vue-message'
910

1011
import ElementUI from 'element-ui'
@@ -27,6 +28,7 @@ Vue.config.productionTip = false
2728
const vueExp = new Vue({
2829
el: '#app',
2930
router,
31+
store,
3032
template: '<App/>',
3133
render: h => h(App)
3234
})

src/router/index.js

+55-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Vue from 'vue'
22
import Router from 'vue-router'
33
import store from './../store'
44
import Home from '@/components/home'
5+
import DGlobal from 'common/js/global.js'
56
// const Pic = r => require.ensure([], () => r(require('@/components/pic/pic.vue')), 'pic')
67
// import Pic from '@/components/pic/pic.vue'
78
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
6061

6162
Vue.use(Router)
6263

63-
export default new Router({
64+
const myRouter = new Router({
6465
// mode: 'history',
6566
hashbang: true,
6667
history: false, // 这个参数改为false就可以了
@@ -88,18 +89,27 @@ export default new Router({
8889
{
8990
path: '/',
9091
redirect: store.getters.getMusicRouter,
92+
meta: {
93+
auth: true
94+
},
9195
components: {
9296
listinfo: MusicSheet
9397
}
9498
},
9599
{
96100
path: '/music/search',
101+
meta: {
102+
auth: true
103+
},
97104
components: {
98105
fullscreen: MusicSearch
99106
}
100107
},
101108
{
102109
path: '/music/albumlist/:id',
110+
meta: {
111+
auth: true
112+
},
103113
name: 'albumlist',
104114
components: {
105115
listinfo: MusicAlbumList
@@ -109,39 +119,57 @@ export default new Router({
109119
{
110120
path: '/music/collection',
111121
name: 'collection',
122+
meta: {
123+
auth: true
124+
},
112125
components: {
113126
listinfo: MusicCollection
114127
}
115128
},
116129
{
117130
path: '/music/sheet/:id',
118131
name: 'musicindex',
132+
meta: {
133+
auth: true
134+
},
119135
components: {
120136
listinfo: MusicSheet
121137
}
122138
},
123139
{
124140
path: '/music/sheet/',
125141
redirect: store.getters.getMusicRouter,
142+
meta: {
143+
auth: true
144+
},
126145
components: {
127146
listinfo: MusicSheet
128147
}
129148
},
130149
{
131150
path: '/music/playlist/',
151+
meta: {
152+
auth: true
153+
},
132154
components: {
133155
listinfo: MusicPlayList
134156
}
135157
},
136158
{
137159
path: '/music/toplist/',
160+
meta: {
161+
auth: true
162+
},
138163
components: {
139164
listinfo: MusicToplist
140165
}
141166
},
142167
{
143168
path: '/music/searchlist/:w',
144169
name: 'searchlist',
170+
meta: {
171+
auth: true
172+
},
145173
components: {
146174
listinfo: MusicSearchList
147175
}
@@ -256,9 +284,35 @@ export default new Router({
256284
},
257285
{
258286
path: '/user/info',
287+
meta: {
288+
auth: true
289+
},
259290
component: UserInfo
260291
}
261292
]
262293
}
263294
]
264295
})
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

Comments
 (0)