|
|
@@ -29,24 +29,16 @@ export default {
|
|
|
token: state => state.main.token
|
|
|
})
|
|
|
},
|
|
|
- watch: {
|
|
|
- $route: {
|
|
|
- handler (newValue) {
|
|
|
- const token = newValue.query['id_token']
|
|
|
- if (token && token !== this.token) {
|
|
|
- localStorage.setItem('token', token)
|
|
|
- this.$store.commit(SET_TOKEN, { token })
|
|
|
- this.fetchUserInfo()
|
|
|
- }
|
|
|
- },
|
|
|
- immediate: true
|
|
|
- }
|
|
|
- },
|
|
|
created () {
|
|
|
// 为每个请求添加Auth字段
|
|
|
this.$axios.interceptors.request.use(config => {
|
|
|
- config.headers['Authorization'] = this.token
|
|
|
+ config.headers = {
|
|
|
+ 'Authorization': this.token,
|
|
|
+ ...(config.headers ?? {})
|
|
|
+ }
|
|
|
return config
|
|
|
+ }, error => {
|
|
|
+ return Promise.reject(error)
|
|
|
})
|
|
|
|
|
|
// 拦截 401 - 无登录信息 前往登录界面
|
|
|
@@ -81,19 +73,27 @@ export default {
|
|
|
})
|
|
|
},
|
|
|
mounted () {
|
|
|
- // 获取用户信息
|
|
|
- this.fetchUserInfo()
|
|
|
+ const handler = () => {
|
|
|
+ const token = this.$route.query['id_token']
|
|
|
+ if (token) {
|
|
|
+ localStorage.setItem('token', token)
|
|
|
+ this.$store.commit(SET_TOKEN, { token })
|
|
|
+ this.fetchUserInfo()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ handler()
|
|
|
+ this.$watch('$route.query', handler)
|
|
|
},
|
|
|
methods: {
|
|
|
fetchUserInfo () {
|
|
|
- getUserInfo().then(res => {
|
|
|
+ return getUserInfo().then(res => {
|
|
|
this.$store.commit(SET_USER_INFO, { userInfo: res })
|
|
|
if (res.role === userType.student) {
|
|
|
this.$router.push({ name: 'student-explore' })
|
|
|
} else {
|
|
|
this.$router.push({ name: 'teacher-explore' })
|
|
|
}
|
|
|
- }).catch(err => this.$setErrorMessage(err.response.data.msg))
|
|
|
+ })
|
|
|
}
|
|
|
}
|
|
|
}
|