import Axios from 'axios';
import { Message } from "view-ui-plus";
import { base_url, request_retry, request_retry_delay } from '../config/index';
const service = Axios.create({
  baseURL: base_url,
  headers: {
    Accept: '*/*',
  },
});

service.defaults.retry = request_retry;
service.defaults.retryDelay = request_retry_delay;
// let LoadingInstance = null;

service.interceptors.request.use(
  config => {
    config.headers['Authorization'] = localStorage.getItem('neican_token');
    return config;
  },
  error => {
    Promise.reject(error);
  }
);

service.interceptors.response.use(
  res => {
    //if(LoadingInstance){LoadingInstance.clear();}
    if (res.status !== 200) {
      Message.info('数据返回出错');
      return Promise.reject('响应非200！');
    } else {
      if (res.data.code != 0) {
        if (res.config.closeInterceptors) {
          return Promise.reject(res.data); //自己处理错误
        }
        //统一处理错误
        return Promise.reject('error');
      }
      return res.data.data;
    }
  },
  error => {
    //if(LoadingInstance){LoadingInstance.clear();}
    Message.info('网络错误');
    if (/403/.test(error.toString()))
      window.parent
        ? (window.parent.location.href = '/')
        : (location.href = '/');

    return Promise.reject(error);
  }
);

export default service;
