UNPKG

fcc-core

Version:

Fusion communication center.

73 lines (67 loc) 1.94 kB
import axios from 'axios' import qs from 'querystring' import BaseException from '../../errors/base-exception' import { serverUrl } from '../../config' // 获取接口根路径 const isProd = process.env.NODE_ENV === 'production' const BASE_PATH = isProd ? serverUrl.production : serverUrl.development const instance = axios.create({ // baseURL: 再配, headers: { 'X-Requested-With': 'XMLHttpRequest' }, timeout: 60000 // 设置默认超时时长 }) // 添加请求拦截器 instance.interceptors.request.use( options => { let url = options.url // 简化类型设置 const headers = (options.headers = options.headers || {}) if (options.json) { headers['Content-Type'] = 'application/json; charset=UTF-8' delete options.json } if (options.formUpload) { headers['Content-Type'] = 'multipart/form-data; charset=UTF-8' delete options.formUpload } if (options.text) { headers['Content-Type'] = 'text/xml; charset=utf-8' headers['Data-Type'] = 'text' delete options.text } // 校验post数据格式 const contentType = headers['Content-Type'] if ( typeof options.data === 'object' && contentType && contentType.indexOf('application/x-www-form-urlencoded') > -1 ) { options.data = qs.stringify(options.data) } options.url = (BASE_PATH[options.apiType || ''] || '') + url return options }, error => { return Promise.reject(error) } ) // 响应拦截器 instance.interceptors.response.use( response => { return response }, error => { if (error.response) { // 融合通讯的服务端报错 throw new BaseException(502) } else { if (axios.isCancel(error)) { throw new BaseException(409) } else { throw new BaseException(408) } } } ) export default instance