VUE

登录请求

1
2
3
4
5
6
7
axios.post("http://localhost:8080/login", this.staff).then(res => {  
if (res.data != null) {
localStorage.setItem('access-admin', JSON.stringify(res.data))
this.$router.push('/home')
console.log(res.data)
}
})

路由守卫

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
router.beforeEach((to, from, next) => {  
if (to.path.startsWith('/login')) {
window.localStorage.removeItem('access-admin')
next()
} else {
let admin = JSON.parse(window.localStorage.getItem('access-admin'))
if (!admin) {
next({path:'/login'})
} else {
// 校验 token 合法性
axios({
url: 'http://localhost:8080/checkToken',
method: 'get',
headers: {
token: admin.token
}
}).then(res => {
if (!res.data) {
console.log('校验失败')
next({path: '/error'})
}
})
next()
}
}
})