"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const crypto = require("./crypto.js");
require("../../../node_modules/axios/index.js");
const env = require("../utils/env.js");
require("../../../node_modules/element-plus/es/index.js");
;/* empty css                                                       */
;/* empty css                                                           */
;/* empty css                                                             */
;/* empty css                                                           */
;/* empty css                                                            */
;/* empty css                                                             */
;/* empty css                                                                 */
const axios = require("../../../node_modules/axios/lib/axios.js");
const index = require("../../../node_modules/element-plus/es/components/message/index.js");
const index$1 = require("../../../node_modules/element-plus/es/components/message-box/index.js");
const service = axios.default.create({
  baseURL: env.default.api_base_url,
  timeout: env.default.api_timeout
});
service.interceptors.request.use(
  (config) => {
    removePending(config);
    addPending(config);
    if (config.url != "/login") {
      config.headers.token = window.$store.getters.token;
      config.headers.refreshToken = window.$store.getters.refreshToken;
    }
    config.headers.crypto = env.default.data_crypto || false;
    config.headers.sig = env.default.data_verify_sign || false;
    return config;
  },
  (error) => {
    Promise.reject(error);
  }
);
service.interceptors.response.use(
  (response) => {
    removePending(response.config);
    const refreshToken = response.headers["refresh-token"];
    const token = response.headers["token"];
    if (token && refreshToken) {
      window.$store.commit("set_token", token);
      window.$store.commit("set_refreshToken", refreshToken);
    }
    if (response.config.responseType == "blob") {
      return response;
    }
    if (response.data && response.data.code == "500") {
      showErrorMessage(response.data.message, response.data.messageType);
      return Promise.reject(response.data);
    }
    if (response.data.message) {
      index.ElMessage({
        message: response.data.message,
        type: "success",
        duration: 1500
      });
    }
    let responseData = response.data.data;
    let isCrypto = env.default.data_crypto || false;
    if (isCrypto && responseData) {
      let decryptData = crypto.aes.de(responseData);
      let resultJson = JSON.parse(decryptData);
      let bodyData = resultJson.data;
      if (resultJson.type == "string") {
        return bodyData;
      } else {
        try {
          return JSON.parse(bodyData);
        } catch (error) {
          return bodyData;
        }
      }
    }
    return responseData;
  },
  async (error) => {
    error.config && removePending(error.config);
    if (axios.default.isCancel(error)) {
      return console.error("请求的重复请求：" + error.message);
    }
    httpErrorStatusHandle(error);
    return Promise.reject(error);
  }
);
function httpErrorStatusHandle(error) {
  let message = "";
  let goLogin = false;
  if (error && error.response) {
    switch (error.response.status) {
      case 302:
        message = "接口重定向了！";
        break;
      case 400:
        message = "参数不正确！";
        break;
      case 401:
        message = error.response.data.message || "您未登录，或者登录已经超时，请先登录！";
        goLogin = true;
        break;
      case 403:
        message = "您没有权限操作！";
        break;
      case 404:
        message = `请求地址未找到: ${error.response.config.url}`;
        break;
      case 408:
        message = "请求超时！";
        break;
      case 409:
        message = "系统已存在相同数据！";
        break;
      case 500:
        message = "服务器内部错误，请联系管理员！";
        break;
      case 501:
        message = "服务未实现！";
        break;
      case 502:
        message = `网关错误: ${error.message}`;
        break;
      case 503:
        message = "服务暂时不可用！";
        break;
      case 504:
        message = "服务暂时无法访问，请稍后再试！";
        if (error.response.statusText.includes("Timeout")) {
          message = "网络请求超时！";
        }
        break;
      case 505:
        message = "HTTP版本不受支持！";
        break;
      default:
        message = error.response.data.message || "异常问题，请联系管理员！";
        break;
    }
  }
  if (error.message.includes("timeout")) {
    message = "网络请求超时！";
  }
  if (error.message.includes("Network")) {
    message = "服务端连接异常！";
  }
  showErrorMessage(message, "1", () => {
    if (goLogin) {
      window.$router.replace("/login");
    }
  });
}
function showErrorMessage(message, type = "1", callback) {
  if (message && document.getElementsByClassName("axios-error-message-box").length === 0) {
    let messageBoxType = "error";
    let messageBoxTitle = "错误";
    if (type == "2") {
      messageBoxType = "warning";
      messageBoxTitle = "警告";
    }
    return index$1.ElMessageBox.alert(message, messageBoxTitle, {
      customClass: "axios-error-message-box",
      type: messageBoxType,
      draggable: true,
      callback: (action) => {
        if (typeof callback == "function") {
          callback();
        }
      }
    });
  }
}
function addPending(config) {
  getPendingKey(config);
}
function removePending(config) {
  getPendingKey(config);
}
function getPendingKey(config) {
  let { url, method, params, data } = config;
  if (typeof data === "string") data = JSON.parse(data);
  return [url, method, JSON.stringify(params), JSON.stringify(data)].join("&");
}
exports.default = service;
