jinjianback1.1/src/utils/request.js

86 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-08-30 17:42:06 +08:00
import axios from 'axios'
2019-04-19 20:41:52 +08:00
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
2017-08-30 17:42:06 +08:00
import { getToken } from '@/utils/auth'
2017-06-26 13:38:24 +08:00
2019-04-19 20:41:52 +08:00
// create an axios instance
2017-06-26 13:38:24 +08:00
const service = axios.create({
2019-04-19 20:41:52 +08:00
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
withCredentials: true, // send cookies when cross-domain requests
timeout: 5000 // request timeout
2017-08-30 17:42:06 +08:00
})
2017-06-26 13:38:24 +08:00
2019-04-19 20:41:52 +08:00
// request interceptor
service.interceptors.request.use(
config => {
2019-04-19 20:41:52 +08:00
// do something before request is sent
if (store.getters.token) {
2019-04-19 20:41:52 +08:00
// let each request carry token
// ['X-Token'] is a custom headers key
// please modify it according to the actual situation
config.headers['X-Token'] = getToken()
}
return config
},
error => {
2019-04-19 20:41:52 +08:00
// do something with request error
console.log(error) // for debug
2019-04-19 20:41:52 +08:00
return Promise.reject(error)
2017-06-26 13:38:24 +08:00
}
)
2017-06-26 13:38:24 +08:00
2019-04-19 20:41:52 +08:00
// response interceptor
2017-06-26 13:38:24 +08:00
service.interceptors.response.use(
2019-04-19 20:41:52 +08:00
/**
* 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
*/
2017-06-26 15:43:11 +08:00
response => {
2017-08-30 17:42:06 +08:00
const res = response.data
2019-04-19 20:41:52 +08:00
// if the custom code is not 20000, it is judged as an error.
2017-06-26 15:43:11 +08:00
if (res.code !== 20000) {
Message({
2019-04-19 20:41:52 +08:00
message: res.message || 'error',
2017-06-26 15:43:11 +08:00
type: 'error',
duration: 5 * 1000
2017-08-30 17:42:06 +08:00
})
2017-06-28 23:02:22 +08:00
2019-04-19 20:41:52 +08:00
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
2017-06-26 15:43:11 +08:00
if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
2019-04-19 20:41:52 +08:00
// 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()
2017-08-30 17:42:06 +08:00
})
2017-06-26 15:43:11 +08:00
})
}
2019-04-19 20:41:52 +08:00
return Promise.reject(res.message || 'error')
2017-06-26 15:43:11 +08:00
} else {
2019-04-19 20:41:52 +08:00
return res
2017-06-26 15:43:11 +08:00
}
},
2017-06-26 13:38:24 +08:00
error => {
console.log('err' + error) // for debug
2017-06-26 13:38:24 +08:00
Message({
message: error.message,
type: 'error',
duration: 5 * 1000
2017-08-30 17:42:06 +08:00
})
return Promise.reject(error)
2017-06-26 13:38:24 +08:00
}
)
2017-08-30 17:42:06 +08:00
export default service