import Axios from "axios";
import Config from "@/config/index.js";

const service = Axios.create({
  baseURL: Config.base.guijiApi,
  headers: {
    Accept: "*/*"
  },
});

service.defaults.retry = Config.requestRetry;
service.defaults.retryDelay = Config.requestRetryDelay;
// let LoadingInstance = null;

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

service.interceptors.response.use(
  res => {
    //if(LoadingInstance){LoadingInstance.clear();}
    if (res.status !== 200) {
      //Toast('数据返回出错');
      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();}
    //Toast("网络错误");
    if (/403/.test(error.toString()))
      window.parent
        ? (window.parent.location.href = "/")
        : (location.href = "/");

    return Promise.reject(error);
  }
);

export default service;