import axios from 'axios' import qs from 'qs'; let setLoading = null; export function setloadingFun (call){ setLoading = call; } let requestLength = 0; // 请求开始 function showLoading(headers) { if (!headers.noLoading) { requestLength += 1; if (requestLength >= 0) { setLoading&&setLoading(true) } } } // 请求结束 function hideLoading() { requestLength -= 1; if (requestLength <= 0) { setLoading&&setLoading(false) requestLength = 0; } } const service = axios.create({ baseURL: `${process.env.VUE_APP_BUILD_ENV? process.env.VUE_APP_BUILD_ENV : ''}`, // url = base url + request // withCredentials: true, // send cookies when cross-domain requests timeout: 50000, // request timeout withCredentials: true }) service.interceptors.request.use((config)=>{ const { headers } = config; showLoading(headers); config.paramsSerializer = (params) => qs.stringify(params, { arrayFormat: 'repeat' }); }) service.interceptors.response.use((response)=>{ hideLoading(); if (response.status === 200) { const { data } = response; // code正常请求 if (data.code === 0 || data.code === 200) { return data.data; } } return response; },(err) => { hideLoading(); return Promise.reject(err); }) export default service;