import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import "./assets/bootstrap/css/bootstrap.min.css";
import "./assets/css/common.css";
import "./assets/css/index.css";

import axios from "axios";
const axiosRetry = require("axios-retry");

Vue.config.productionTip = false;
// Vue.use(VueResource);
Vue.use(ElementUI);

const qmInstance = axios.create({});
let wk_token;
window.requestInProcessingTotal = 0;
qmInstance.interceptors.request.use(
  config => {
    if (config.url.includes("/login") === false) {
      if (!wk_token) {
        wk_token = window.localStorage.getItem("user_token");
      }
      if (wk_token && config.headers.common["user_token"] == null) {
        config.headers.common["user_token"] = wk_token;
      }
    } else {
      wk_token = null;
    }

    window.requestInProcessingTotal++;
    // console.log("后台并发峰值：" + window.requestInProcessingTotal);

    return config;
  },
  error => {
    console.log(error);
    return Promise.resolve(error);
  }
);

//响应拦截
qmInstance.interceptors.response.use(
  response => {
    window.requestInProcessingTotal--;
    return response;
  },
  error => {
    window.requestInProcessingTotal--;
    // 这里是返回状态码不为200时候的错误处理
    const status = error.response.status;
    if (status != 200) {
      console.log(error);
    }

    // 判断reponse的内容，如果是no login就退出
    if (
      error.response.data.code === "403" &&
      error.response.data.desc === "no login."
    ) {
      wk_token = null;
      localStorage.removeItem("rootOrgId");
      localStorage.removeItem("userName");
      localStorage.removeItem("user_token");
      if (Date.now() - (window.logAlertDate || 0) > 10000) {
        window.logAlertDate = Date.now();
        alert("登录失效 跳转登录页面");
      }
      router.push({
        name: "login"
      });
    }

    // 登录失效 跳转登录页面 //facepp可能返回403
    // if (status == 403 || status == 401) {
    //   wk_token = null;
    //   localStorage.removeItem("rootOrgId");
    //   localStorage.removeItem("userName");
    //   localStorage.removeItem("user_token");
    //   if (Date.now() - (window.logAlertDate || 0) > 10000) {
    //     window.logAlertDate = Date.now();
    //     alert("登录失效 跳转登录页面");
    //   }
    //   router.push({
    //     name: "login"
    //   });
    // }
    return Promise.reject(error);
  }
);

qmInstance.defaults.withCredentials = true; //允许跨域携带cookie
qmInstance.defaults.timeout = 10000; //超时时间
qmInstance.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest"; //标识这是一个 ajax 请求

axiosRetry(qmInstance, {
  retries: 3,
  retryCondition: err => {
    // debugger;
    console.log(`error status code: ${err.response.status} and retry ...`);
    console.log(err);
    return true;
  }
});

Vue.prototype.$http = qmInstance;

// var errorNum = 0; //403次数
// Vue.http.interceptors.push(function(request, next) {
//   var user_token = localStorage.getItem("user_token");
//   if (user_token) {
//     request.headers.set("user_token", localStorage.getItem("user_token"));
//   }
//   next(function(response) {
//     if (response.status == 403 && response.body && response.body.desc) {
//       clearInterval(global_.pageTimer["uploadTimer"]); //停止定时器
//       localStorage.removeItem("rootOrgId");
//       localStorage.removeItem("userName");
//       localStorage.removeItem("user_token");
//       if (errorNum == 0) {
//         errorNum++;
//         this.$alert("登录失效,请重新登录！", "提示", {
//           showClose: false,
//           confirmButtonText: "确定",
//           callback: action => {
//             this.$router.push({ path: "/login" });
//           }
//         });
//       }
//     } else if (response.status != 200) {
//       console.log(response);
//     }
//   });
// });

new Vue({
  router,
  render: h => h(App)
}).$mount("#app");
