2021-06-02 14:11:45 +08:00
|
|
|
|
import axios from 'axios' // 引入axios
|
|
|
|
|
|
import { Message } from 'element-ui'
|
|
|
|
|
|
import { store } from '@/store'
|
|
|
|
|
|
import context from '@/main'
|
2021-06-17 15:54:32 +08:00
|
|
|
|
import { MessageBox } from 'element-ui'
|
2021-03-04 19:48:38 +08:00
|
|
|
|
|
2019-09-11 23:13:50 +08:00
|
|
|
|
const service = axios.create({
|
2021-06-02 14:11:45 +08:00
|
|
|
|
baseURL: process.env.VUE_APP_BASE_API,
|
|
|
|
|
|
timeout: 99999
|
2019-09-11 23:13:50 +08:00
|
|
|
|
})
|
2019-12-27 16:28:41 +08:00
|
|
|
|
let acitveAxios = 0
|
|
|
|
|
|
let timer
|
|
|
|
|
|
const showLoading = () => {
|
2021-06-02 14:11:45 +08:00
|
|
|
|
acitveAxios++
|
|
|
|
|
|
if (timer) {
|
|
|
|
|
|
clearTimeout(timer)
|
|
|
|
|
|
}
|
|
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
|
|
if (acitveAxios > 0) {
|
|
|
|
|
|
context.$bus.emit('showLoading')
|
2019-12-27 16:28:41 +08:00
|
|
|
|
}
|
2021-06-02 14:11:45 +08:00
|
|
|
|
}, 400)
|
2019-12-27 16:28:41 +08:00
|
|
|
|
}
|
2019-09-11 23:13:50 +08:00
|
|
|
|
|
2019-12-27 16:28:41 +08:00
|
|
|
|
const closeLoading = () => {
|
2021-06-02 14:11:45 +08:00
|
|
|
|
acitveAxios--
|
|
|
|
|
|
if (acitveAxios <= 0) {
|
|
|
|
|
|
clearTimeout(timer)
|
|
|
|
|
|
context.$bus.emit('closeLoading')
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// http request 拦截器
|
2019-09-11 23:13:50 +08:00
|
|
|
|
service.interceptors.request.use(
|
2021-06-02 14:11:45 +08:00
|
|
|
|
config => {
|
|
|
|
|
|
if (!config.donNotShowLoading) {
|
|
|
|
|
|
showLoading()
|
2019-09-11 23:13:50 +08:00
|
|
|
|
}
|
2021-06-02 14:11:45 +08:00
|
|
|
|
const token = store.getters['user/token']
|
|
|
|
|
|
const user = store.getters['user/userInfo']
|
|
|
|
|
|
config.data = JSON.stringify(config.data)
|
|
|
|
|
|
config.headers = {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
'x-token': token,
|
|
|
|
|
|
'x-user-id': user.ID
|
|
|
|
|
|
}
|
|
|
|
|
|
return config
|
|
|
|
|
|
},
|
|
|
|
|
|
error => {
|
|
|
|
|
|
closeLoading()
|
|
|
|
|
|
Message({
|
|
|
|
|
|
showClose: true,
|
|
|
|
|
|
message: error,
|
|
|
|
|
|
type: 'error'
|
|
|
|
|
|
})
|
|
|
|
|
|
return error
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
2019-09-11 23:13:50 +08:00
|
|
|
|
|
2021-06-02 14:11:45 +08:00
|
|
|
|
// http response 拦截器
|
2019-09-11 23:13:50 +08:00
|
|
|
|
service.interceptors.response.use(
|
2021-06-02 14:11:45 +08:00
|
|
|
|
response => {
|
|
|
|
|
|
closeLoading()
|
2021-03-04 19:48:38 +08:00
|
|
|
|
|
2021-06-02 14:11:45 +08:00
|
|
|
|
if (response.headers['new-token']) {
|
|
|
|
|
|
store.commit('user/setToken', response.headers['new-token'])
|
|
|
|
|
|
}
|
|
|
|
|
|
if (response.data.code === 0 || response.headers.success === 'true') {
|
|
|
|
|
|
return response.data
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Message({
|
|
|
|
|
|
showClose: true,
|
|
|
|
|
|
message: response.data.msg || decodeURI(response.headers.msg),
|
|
|
|
|
|
type: response.headers.msgtype || 'error'
|
|
|
|
|
|
})
|
|
|
|
|
|
if (response.data.data && response.data.data.reload) {
|
|
|
|
|
|
store.commit('user/LoginOut')
|
|
|
|
|
|
}
|
|
|
|
|
|
return response.data.msg ? response.data : response
|
2019-09-11 23:13:50 +08:00
|
|
|
|
}
|
2021-06-02 14:11:45 +08:00
|
|
|
|
},
|
|
|
|
|
|
error => {
|
|
|
|
|
|
closeLoading()
|
2021-06-23 10:46:54 +08:00
|
|
|
|
MessageBox.confirm(`
|
|
|
|
|
|
<p>检测到接口错误${error}</p>
|
|
|
|
|
|
<p>错误码500:此类错误内容常见于后台panic,如果影响您正常使用可强制登出清理缓存</p>
|
|
|
|
|
|
<p>错误码404:此类错误多为接口未注册(或未重启)或者请求路径(方法)与api路径(方法)不符</p>
|
|
|
|
|
|
`, '接口报错', {
|
|
|
|
|
|
dangerouslyUseHTMLString: true,
|
2021-06-17 15:54:32 +08:00
|
|
|
|
distinguishCancelAndClose: true,
|
|
|
|
|
|
confirmButtonText: '清理缓存',
|
|
|
|
|
|
cancelButtonText: '取消'
|
2021-06-02 14:11:45 +08:00
|
|
|
|
})
|
2021-06-17 15:54:32 +08:00
|
|
|
|
.then(() => {
|
|
|
|
|
|
store.commit('user/LoginOut')
|
|
|
|
|
|
})
|
2021-06-02 14:11:45 +08:00
|
|
|
|
return error
|
|
|
|
|
|
}
|
2019-09-11 23:13:50 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2021-06-02 14:11:45 +08:00
|
|
|
|
export default service
|