响应拦截器对err处理
This commit is contained in:
parent
f0ffd89af2
commit
67073c5e86
@ -127,7 +127,7 @@ const actions = {
|
||||
async login({ commit }, data) {
|
||||
const res = await login(data)
|
||||
console.log('将登录页的逻辑移动到 vuex 里面')
|
||||
commit('setToken', res.data.data)
|
||||
commit('setToken', res)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
import axios from 'axios'
|
||||
import { Message } from 'element-ui'
|
||||
// import { MessageBox, Message } from 'element-ui'
|
||||
// import store from '@/store'
|
||||
// import { getToken } from '@/utils/auth'
|
||||
@ -84,7 +85,20 @@ service.interceptors.request.use(
|
||||
|
||||
// 响应拦截器
|
||||
service.interceptors.response.use(res => {
|
||||
return res
|
||||
const { message, data, success } = res.data
|
||||
if (success) {
|
||||
// 已经对res的成功与否进行处理, 直接返回data
|
||||
return data
|
||||
} else {
|
||||
Message.error(message || '系统错误') // 饿了么弹窗组件
|
||||
return Promise.reject(message) // 这里的reject是为了可以继续链式调用, 比如跳到login/index handleLogin catch
|
||||
}
|
||||
}, err => {
|
||||
console.dir(err)
|
||||
// 提示错误
|
||||
Message.error(err.message)
|
||||
// reject
|
||||
return Promise.reject(err)
|
||||
})
|
||||
|
||||
export default service
|
||||
|
@ -126,9 +126,10 @@ export default {
|
||||
},
|
||||
async handleLogin() {
|
||||
try {
|
||||
await this.$refs.loginForm.validate()
|
||||
this.$store.dispatch('user/login', this.loginForm)
|
||||
await this.$refs.loginForm.validate() // 数据校验
|
||||
await this.$store.dispatch('user/login', this.loginForm) // 必须加await
|
||||
} catch (error) {
|
||||
console.log('捕获错误')
|
||||
console.log(error)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user