import axios from 'axios' import store from '@/store' import Qs from 'qs' import { getToken } from './auth' import constv from '@/config/constv' import { Modal, message as Message } from 'ant-design-vue/es' const service = axios.create({ baseURL: `${constv.MIDDLE_OFFICE_PROXY_URL}admin/index/transfer`, timeout: 20000 // transformRequest: params => Qs.stringify(params) // 参数格式化 qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' }) => 'a=b&a=c' // paramsSerializer: params => Qs.stringify(params, { arrayFormat: 'repeat' }) // 参数格式化 qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' }) => 'a=b&a=c' }) const err = (error) => { return Promise.reject(error) } service.interceptors.request.use(config => { const formData = Qs.parse(config.data) const proxyUrl = { 'admin_center|proxy_url': '/secure' + config.url } if (config.method === 'get') { config.params = { ...config.params, ...proxyUrl, 'admin_center|extract_query': 1 // 以下两个参数用于下载文件流用到 // 'admin_center|stream_resp': 1, // 'admin_center|resp_header_override': 1 } } if (config.method === 'post') { config.data = Qs.stringify({ ...formData, ...proxyUrl }) } const token = getToken() if (token) { config.headers['AuthValue'] = 'xxxxxxx:' } config.headers.common['token'] = token config.headers.common['way'] = constv.PROXY_WAY config.url = '' return config }, err) service.interceptors.response.use(response => { const result = response.data const message = result.message const code = result.errCode || result.code if (result.status === 'error') { Message.error(message.message) return Promise.reject(message.message) } if (code === 10200) { Modal.warning({ title: '授权信息已过期,请重新登录', okText: '确定', onOk () { store.dispatch('Logout').then(() => { window.location.reload() }) } }) } return result }, err) export { service, axios }