import axios from "axios";
// import qs from 'qs'
import Vue from "vue";
import router from "../router";

const vue = new Vue();

var isProduction = process.env.NODE_ENV === "production";
let loading = "";

const serverName = isProduction ? "/api/" : "http://192.168.20.55:8035/api/";

const serverLocation = window.location.hostname;

axios.defaults.baseURL = serverName;
axios.defaults.headers["X-Requested-with"] = "XMLHttpRequest";

axios.interceptors.request.use(
  function(config) {
    config.headers["token"] = window.localStorage.getItem("token");
    loading = vue.$loading({
      lock: true,
      text: "加载中",
      spinner: "el-icon-loading",
      background: "rgba(0, 0, 0, 0.7)",
    });

    if(config.headers["Content-Type"]=='multipart/form-data'){
      return config;
    }
   
    if (config.method === "post") {
      config.data = {
        ...config.data,
        rnd: Math.random(),
      };
      // config.data = qs.stringify(config.data)
    } else if (config.method === "get") {
      config.params = {
        rnd: Math.random(),
        ...config.params,
      } 
      // config.params = qs.stringify(config.params)
    }

   
    return config;
  },
  function(error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

// 配置response拦截器
axios.interceptors.response.use(
  (response) => {
    let data = response.data;
    let code = Number(response.data.code);

    loading.close();
    switch (code) {
      case -1:
        break;
      case 5001:
        if (window.localStorage.getItem("token")) {
          window.localStorage.setItem("token", "");
          vue.$alert("登录状态失效，请重新登录", "提示", {
            confirmButtonText: "确定",
            callback: function() {
              window.localStorage.setItem("userInfo", "");
              router.push("/login");
            },
          });
        }
        return
      case 5002:
        if (window.localStorage.getItem("token")) {
          window.localStorage.setItem("token", "");
          vue.$alert("登录状态失效，请重新登录", "提示", {
            confirmButtonText: "确定",
            callback: function() {
              window.localStorage.setItem("userInfo", "");
              router.push("/login");
            },
          });
        }
        return
      case 500:
        return vue.$alert(data.msg || "服务器错误", "提示", {
          confirmButtonText: "确定",
          callback: function() {},
        });
    }

    if (code!==0) {
      return vue.$alert(data.msg || "服务器错误", "提示", {
        confirmButtonText: "确定",
        callback: function() {},
      });
    }
    return data;
  },
  (error) => {
    loading.close();
    window.localStorage.setItem("userInfo", "");
    if (error.response) {
      let data = error.response.data;
      vue.$alert(data.msg || "服务器错误，请稍后再试", "提示", {
        confirmButtonText: "确定",
        callback: function() {
          window.localStorage.setItem("userInfo", "");
          router.push("/login");
        },
      });
    }
    return Promise.reject(error);
  }
);
export { serverName, axios, serverLocation };
