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
|
2019-05-28 11:07:36 +08:00
|
|
|
// withCredentials: true, // send cookies when cross-domain requests
|
2019-04-19 20:41:52 +08:00
|
|
|
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
|
2018-08-16 11:08:53 +08:00
|
|
|
service.interceptors.request.use(
|
|
|
|
config => {
|
2019-04-19 20:41:52 +08:00
|
|
|
// do something before request is sent
|
|
|
|
|
2018-08-16 11:08:53 +08:00
|
|
|
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()
|
2018-08-16 11:08:53 +08:00
|
|
|
}
|
|
|
|
return config
|
|
|
|
},
|
|
|
|
error => {
|
2019-04-19 20:41:52 +08:00
|
|
|
// do something with request error
|
2018-08-16 11:08:53 +08:00
|
|
|
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
|
|
|
}
|
2018-08-16 11:08:53 +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-05-27 17:01:46 +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-05-27 17:01:46 +08:00
|
|
|
return Promise.reject(new Error(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 => {
|
2018-08-16 11:08:53 +08:00
|
|
|
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
|