text: import axios from "axios";
  import { MessageBox, Message } from "element-ui";
  import store from "@/store";
  import { getToken } from "@/utils/auth";
  
  // create an axios instance
  const service = axios.create({
    baseURL: "", // url = base url + request url
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 5000 // request timeout
  });
  
  let pending = []; //声明一个数组用于存储每个ajax请求的取消函数和ajax标识
  let cancelToken = axios.CancelToken;
  let removePending = config => {
    for (let p in pending) {
      if (pending[p].u === config.url + "&" + config.method) {
        //当当前请求在数组中存在时执行函数体
        pending[p].f(); //执行取消操作
        pending.splice(p, 1); //把这条记录从数组中移除
      }
    }
  };
  
  // request interceptor
  service.interceptors.request.use(
    config => {
      // do something before request is sent
  
      removePending(config); //在一个ajax发送前执行一下取消操作
      config.cancelToken = new cancelToken(c => {
        // 这里的ajax标识我是用请求地址&请求方式拼接的字符串，当然你可以选择其他的一些方式
        pending.push({ u: config.url + "&" + config.method, f: c });
      });
  
      if (store.getters.token) {
        // let each request carry token
        // ['X-Token'] is a custom headers key
        // please modify it according to the actual situation
        config.headers["X-Token"] = getToken();
      }
      if (config.url === "/promote/tools/uploadImage") {
        config.headers["Content-Type"] = "multipart/form-data";
      }
  
      return config;
    },
    error => {
      // do something with request error
      console.log(error); // for debug
      return Promise.reject(error);
    }
  );
  
  // response interceptor
  service.interceptors.response.use(
    /**
     * If you want to get http information such as headers or status
     * Please return  response => response
     */
  
    /**
     * Determine the request status by custom code
     * Here is just an example
     * You can also judge the status by HTTP Status Code
     */
    response => {
      const res = response.data;
  
      removePending(response.config); //在一个ajax响应后再执行一下取消操作，把已经完成的请求从pending中移除
  
      // if the custom code is not 20000, it is judged as an error.
      if (res.code !== 200) {
        Message({
          message: res.msg || res.message || "Error",
          type: "error",
          duration: 5 * 1000
        });
  
        // 登录过期
        if (res.code === 403) {
          // to re-login
          MessageBox.confirm("请重新登录", "温馨提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "error"
          }).then(() => {
            store.dispatch("user/resetToken").then(() => {
              location.reload();
            });
          });
        }
        return Promise.reject(new Error(res.msg || "Error"));
      } else {
        return res;
      }
    },
    error => {
      console.log("err" + error); // for debug
      Message({
        message: error.msg || error.message || "Error",
        type: "error",
        duration: 5 * 1000
      });
      // return Promise.reject(error);
      return { data: {} };
    }
  );
  
  export default service;
  