import axios from 'axios'
|
import { Loading } from 'element-ui';
|
import { serialize } from '@/util/util'
|
import { getStore } from '../util/store'
|
import NProgress from 'nprogress' // progress bar
|
import errorCode from '@/const/errorCode'
|
import router from '@/router/router'
|
import { Message } from 'element-ui'
|
import 'nprogress/nprogress.css'
|
import qs from 'qs'
|
import store from '@/store' // progress bar style
|
import handleError from '../libs/message'
|
axios.defaults.timeout = 30000
|
// 返回其他状态吗
|
axios.defaults.validateStatus = function (status) {
|
return status >= 200 && status <= 500 // 默认的
|
}
|
// 跨域请求,允许保存cookie
|
axios.defaults.withCredentials = true
|
// NProgress Configuration
|
NProgress.configure({
|
showSpinner: false
|
})
|
let loadingInstance = null;
|
// HTTPrequest拦截
|
axios.interceptors.request.use(config => {
|
/*NProgress.start() // start progress bar
|
loadingInstance = Loading.service({
|
lock: true,
|
text: '',
|
spinner: 'el-icon-loading',
|
background: 'rgba(255, 0, 0, 0)'
|
});*/
|
const TENANT_ID = getStore({ name: 'tenantId' })
|
const isToken = (config.headers || {}).isToken === false
|
// const token = window.android.getAppToken()
|
const token = `8d3bcce6-c27b-4f98-92a7-12b3b9a02306`
|
if (token && !isToken) {
|
config.headers['Authorization'] = 'Bearer ' + token// token
|
}
|
if (TENANT_ID) {
|
config.headers['TENANT-ID'] = TENANT_ID // 租户ID
|
}
|
|
// headers中配置serialize为true开启序列化 ||config.data
|
if (config.method === 'post' && (config.headers.serialize)) {
|
config.data = serialize(config.data)
|
delete config.data.serialize
|
}
|
// config.headers['VERSION'] = 'GHX'
|
if (config.method === 'get' || config.params) {
|
config.paramsSerializer = function (params) {
|
let p = {};
|
for (let i in params) {
|
if (typeof params[i] == 'string' && !params[i]) {
|
|
} else {
|
p[i] = params[i]
|
}
|
}
|
return qs.stringify(p, { arrayFormat: 'repeat' })
|
}
|
}
|
|
return config
|
}, error => {
|
return Promise.reject(error)
|
})
|
|
// HTTPresponse拦截
|
axios.interceptors.response.use(res => {
|
// NProgress.done()
|
// loadingInstance.close();
|
const status = Number(res.status) || 200
|
const message = res.data.msg || errorCode[status] || errorCode['default']
|
/*if (status === 401) {
|
Message({
|
message: message,
|
type: 'error'
|
})
|
handleError(message)
|
store.dispatch('FedLogOut').then(() => {
|
router.push({ path: '/pad' })
|
})
|
return
|
}*/
|
|
/*if (status !== 200 || res.data.code === 1) {
|
Message({
|
message: message,
|
type: 'error'
|
})
|
handleError(message)
|
return handleError(message)
|
// return Promise.reject(new Error(message))
|
}*/
|
return res
|
}, error => {
|
NProgress.done()
|
return Promise.reject(new Error(error))
|
})
|
|
export default axios
|