You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.5 KiB
121 lines
3.5 KiB
import axios from 'axios'
|
|
import {
|
|
MessageBox,
|
|
Message
|
|
} from 'element-ui'
|
|
import store from '@/store'
|
|
import {
|
|
getToken
|
|
} from '@/utils/auth'
|
|
|
|
// create an axios instance
|
|
const service = axios.create({
|
|
baseURL: '', // url = base url + request url
|
|
// withCredentials: true, // send cookies when cross-domain requests
|
|
timeout: 300000 // request timeout
|
|
})
|
|
|
|
// request interceptor
|
|
service.interceptors.request.use(
|
|
config => {
|
|
// do something before request is sent
|
|
|
|
if (store.getters.token) {
|
|
// let each request carry token
|
|
// ['X-Token'] is a custom headers key
|
|
// please modify it according to the actual situation
|
|
config.headers.Authorization = getToken()
|
|
config.headers['Accept-Language'] = "zh-Hans"
|
|
let apifoxToken = localStorage.getItem('apifoxToken')
|
|
if (apifoxToken) {
|
|
config.headers['apifoxToken'] = apifoxToken
|
|
}
|
|
|
|
}
|
|
// else {
|
|
// config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8;'
|
|
// }
|
|
return config
|
|
},
|
|
error => {
|
|
// do something with request error
|
|
return Promise.reject(error)
|
|
}
|
|
)
|
|
|
|
// response interceptor
|
|
service.interceptors.response.use(
|
|
/**
|
|
* If you want to get http information such as headers or status
|
|
* Please return response => response
|
|
*/
|
|
|
|
/**
|
|
* Determine the request status by custom code
|
|
* Here is just an example
|
|
* You can also judge the status by HTTP Status Code
|
|
*/
|
|
response => {
|
|
|
|
const res = response.data
|
|
if(response.headers['content-disposition']){
|
|
res.disposition = response.headers['content-disposition']
|
|
} else if (response.headers['x-response']) {
|
|
res.headers = response.headers['x-response']
|
|
}
|
|
// if the custom code is not 20000, it is judged as an error.
|
|
// if (res.code !== 20000) {
|
|
// Message({
|
|
// message: res.message || 'Error',
|
|
// type: 'error',
|
|
// duration: 5 * 1000
|
|
// })
|
|
|
|
// // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
|
|
// if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
|
|
// // to re-login
|
|
// MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
|
|
// confirmButtonText: 'Re-Login',
|
|
// cancelButtonText: 'Cancel',
|
|
// type: 'warning'
|
|
// }).then(() => {
|
|
// store.dispatch('user/resetToken').then(() => {
|
|
// location.reload()
|
|
// })
|
|
// })
|
|
// }
|
|
// // return Promise.reject(new Error(res.message || 'Error'))
|
|
return res
|
|
// } else {
|
|
// return res
|
|
// }
|
|
},
|
|
error => {
|
|
let err = error.response.data.error // for debug
|
|
if (err && err.validationErrors) {
|
|
for (let item in err.validationErrors) {
|
|
Message({
|
|
message: err.validationErrors[item].message,
|
|
type: 'error',
|
|
duration: 5 * 1000
|
|
})
|
|
}
|
|
} else {
|
|
console.log(err)
|
|
// 用户登录,用户名密码错误,报400,后端返回 invalid_grant 信息,令牌受限
|
|
// 不知道其他业务,报错,是否回返回该值,如果遇到 令牌受限 ,不在此提示信息,方便登录时给提示框
|
|
if (err == 'invalid_grant') {
|
|
|
|
} else {
|
|
Message({
|
|
message: err.message,
|
|
type: 'error',
|
|
duration: 5 * 1000
|
|
})
|
|
}
|
|
}
|
|
return Promise.reject(error)
|
|
}
|
|
)
|
|
|
|
export default service
|
|
|