function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
var _excluded = ["url", "data"],
  _excluded2 = ["url", "data"],
  _excluded3 = ["url", "data"],
  _excluded4 = ["url", "data"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
/* eslint-disable prettier/prettier */
import axios from 'axios';
import { message } from 'antd';
import { ServerCode, ServerCodeMap } from "./config";
var service = axios.create({
  timeout: 200000,
  // 请求超时时间
  withCredentials: true // 允许携带cookie
});

// request拦截器
service.interceptors.request.use(function (config) {
  return config;
}, function (error) {
  //请求错误时做些事
  return Promise.reject(error);
});

// response拦截器
service.interceptors.response.use(
// HTTP的状态码(只有200才走这里)
function (response) {
  var headers = response.headers;
  var res = response.data;

  // 状态码为200时，表示请求成功不走错误处理流程(根据请求头是否包含no_format字段来判断返回数据是否需要处理)
  if (+res.code === ServerCode.SUCCESS) return headers.no_format ? response : response.data;
  return Promise.reject({
    data: res,
    response: response
  });
}, function (err) {
  // 统一错误拦截
  return Promise.reject(err);
});
function isResponseError(x) {
  return x.data;
}
function isAxiosError(x) {
  return x.response;
}

// 错误处理
var errHandle = function errHandle(err) {
  // 判断上下文是“接口状态码”还是“HTTP状态码”
  var errResult = {};
  var code = -1;
  var errorMsg = '';

  // HTTP状态码出错
  if (isAxiosError(err) && err.response) {
    code = +err.response.status;
    errorMsg = ServerCodeMap[code];
  }

  // 接口状态码出错
  if (isResponseError(err)) {
    code = +err.data.code;
    errorMsg = err.data.msg || ServerCodeMap[code];
    errResult = err.data;
  }

  // 若code是400，则不会弹出message
  ![ServerCode.CONTINUE].includes(code) && message.error(errorMsg);

  // 未登录
  if (ServerCode.NO_LOGIN == code) {
    // 未登录逻辑
  }

  // 无权限
  if (ServerCode.FORBIDDEN == code) {
    // 无权限逻辑
  }
  return Promise.reject(errResult);
};
var response = function response(axiosObj) {
  return axiosObj.then(function (res) {
    return res.data;
  }).catch(function (err) {
    return errHandle(err);
  });
};

/**
 * 四种请求方式
 * @param url       接口地址
 * @param data      接口参数
 * @param config    接口所需其余配置参数
 */
export var get = function get(_ref) {
  var url = _ref.url,
    data = _ref.data,
    req = _objectWithoutProperties(_ref, _excluded);
  return response(service.get(url, _objectSpread({
    params: _objectSpread(_objectSpread({}, data), {}, {
      time: Date.now()
    })
  }, req)));
};
export var post = function post(_ref2) {
  var url = _ref2.url,
    data = _ref2.data,
    req = _objectWithoutProperties(_ref2, _excluded2);
  return response(service.post(url, data, _objectSpread({}, req)));
};
export var put = function put(_ref3) {
  var url = _ref3.url,
    data = _ref3.data,
    req = _objectWithoutProperties(_ref3, _excluded3);
  return response(service.put(url, data, _objectSpread({}, req)));
};
export var del = function del(_ref4) {
  var url = _ref4.url,
    data = _ref4.data,
    req = _objectWithoutProperties(_ref4, _excluded4);
  return response(service.delete(url, _objectSpread({
    data: data
  }, req)));
};
export default service;