function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
//@ts-nocheck
import axios from 'axios';
// 安装axios的同时会同步安装qs包 https://www.npmjs.com/package/qs
import { getVtxToken, trim } from '@vtx/utils';
import { message } from 'antd';
import isObject from 'lodash/isObject';
import pickBy from 'lodash/pickBy';
import qs from 'qs';
var SECOND = 30;
var MILL_SECOND = 1000;
var baseUrlPrefix = process.env.PREFIX ? "/".concat(process.env.PREFIX) : '';
var Http = /*#__PURE__*/function () {
  function Http(options) {
    _classCallCheck(this, Http);
    // 创建axios实例
    this.instance = this._createAxios(options);
  }

  // 初始化
  _createClass(Http, [{
    key: "_createAxios",
    value: function _createAxios(options) {
      var token = getVtxToken('token');
      // 创建实例
      var instance = axios.create(_objectSpread(_objectSpread({
        // `timeout` 指定请求超时的毫秒数(0 表示无超时时间)
        // 如果请求话费了超过 `timeout` 的时间，请求将被中断
        timeout: MILL_SECOND * SECOND
      }, options), {}, {
        headers: _objectSpread({
          'Cache-Control': 'no-cache',
          Authorization: token ? "Bearer ".concat(token) : '',
          tenantId: getVtxToken('tenantId'),
          userId: getVtxToken('userId')
        }, options === null || options === void 0 ? void 0 : options.headers)
      }));
      instance.interceptors.response.use(this._responseHandler, this._errorHandler);
      return instance;
    }

    /**
     * get 请求
     * @param {*} url
     * @param {*} options {body}
     */
  }, {
    key: "get",
    value: function get(url) {
      var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      var _options$body = options.body,
        body = _options$body === void 0 ? {} : _options$body,
        _options$extraHeader = options.extraHeader,
        extraHeader = _options$extraHeader === void 0 ? {} : _options$extraHeader;
      var _url = baseUrlPrefix + url;
      var token = getVtxToken('token');
      // 过滤掉值为 '' || undefined || null 的对象属性
      var postData = isObject(body) ? pickBy(trim(body), function (value) {
        return value !== '' && value !== undefined && value !== null;
      }) : {};
      return this.instance.get(_url, {
        params: _objectSpread({}, postData),
        headers: _objectSpread({
          Authorization: token ? "Bearer ".concat(token) : '',
          tenantId: getVtxToken('tenantId'),
          userId: getVtxToken('userId')
        }, extraHeader !== null && extraHeader !== void 0 && extraHeader.systemCode ? {
          systemCode: extraHeader.systemCode || ''
        } : {})
      });
    }

    /**
     * 判断接口是否存在
     * @param {*} url
     */
  }, {
    key: "exists",
    value: function exists(url) {
      var _url = baseUrlPrefix + url;
      var token = getVtxToken('token');
      return axios.head(_url, {
        validateStatus: function validateStatus() {
          return true;
        },
        headers: {
          Authorization: token ? "Bearer ".concat(token) : '',
          tenantId: getVtxToken('tenantId'),
          userId: getVtxToken('userId')
        }
      }).then(function (res) {
        return res.status !== 404;
      }).catch(function (error) {
        var _error$response;
        return (error === null || error === void 0 || (_error$response = error.response) === null || _error$response === void 0 ? void 0 : _error$response.status) !== 404;
      });
    }
    /**
     * get 请求
     * @param {*} url
     * @param {*} options {body}
     */
  }, {
    key: "getForTenantId",
    value: function getForTenantId(url) {
      var _Object$keys,
        _this = this;
      var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      var _options$body2 = options.body,
        body = _options$body2 === void 0 ? {} : _options$body2,
        _options$extraHeader2 = options.extraHeader,
        extraHeader = _options$extraHeader2 === void 0 ? {} : _options$extraHeader2;
      var _url = baseUrlPrefix + url;
      var token = getVtxToken('token');
      // 过滤掉值为 '' || undefined || null 的对象属性
      var postData = isObject(body) ? pickBy(trim(body), function (value) {
        return value !== '' && value !== undefined && value !== null;
      }) : {};
      var tenantId = (_Object$keys = Object.keys(body)) === null || _Object$keys === void 0 ? void 0 : _Object$keys.find(function (item) {
        return item === 'tenantId';
      });
      if (tenantId && getVtxToken('token')) {
        return this.get('/cloud/management/api/v101/userAuth/getUserInfo', {
          data: {
            token: getVtxToken('token')
          }
        }).then(function (res) {
          var _res$data;
          if (res.result === 0 && res !== null && res !== void 0 && (_res$data = res.data) !== null && _res$data !== void 0 && _res$data.tenantId) {
            var _res$data2, _res$data3, _res$data4;
            return _this.instance.get(_url, {
              params: _objectSpread(_objectSpread({}, postData), {}, {
                tenantId: res === null || res === void 0 || (_res$data2 = res.data) === null || _res$data2 === void 0 ? void 0 : _res$data2.tenantId
              }),
              headers: _objectSpread({
                Authorization: token ? "Bearer ".concat(token) : '',
                tenantId: (res === null || res === void 0 || (_res$data3 = res.data) === null || _res$data3 === void 0 ? void 0 : _res$data3.tenantId) || getVtxToken('tenantId'),
                userId: (res === null || res === void 0 || (_res$data4 = res.data) === null || _res$data4 === void 0 ? void 0 : _res$data4.userId) || getVtxToken('userId')
              }, extraHeader !== null && extraHeader !== void 0 && extraHeader.systemCode ? {
                systemCode: extraHeader.systemCode || ''
              } : {})
            });
          }
        });
      } else {
        return this.instance.get(_url, {
          params: _objectSpread({}, postData),
          headers: _objectSpread({
            Authorization: token ? "Bearer ".concat(token) : '',
            tenantId: getVtxToken('tenantId'),
            userId: getVtxToken('userId')
          }, extraHeader !== null && extraHeader !== void 0 && extraHeader.systemCode ? {
            systemCode: extraHeader.systemCode || ''
          } : {})
        });
      }
    }
    /**
     * post 请求
     * @param {*} url
     * @param {*} options
     */
  }, {
    key: "post",
    value: function post(url) {
      var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      var _url = baseUrlPrefix + url;
      var _options$body3 = options.body,
        body = _options$body3 === void 0 ? {} : _options$body3,
        _options$extraHeader3 = options.extraHeader,
        extraHeader = _options$extraHeader3 === void 0 ? {} : _options$extraHeader3;
      var data = isObject(body) ? qs.stringify(trim(body)) : trim(JSON.parse(body));
      var headers = {};
      if (extraHeader && extraHeader.msg) {
        headers.operation = encodeURIComponent(extraHeader.msg);
      }
      if (extraHeader && extraHeader !== null && extraHeader !== void 0 && extraHeader.systemCode) {
        headers.systemCode = extraHeader.systemCode;
      }
      return this.instance.post(_url, data, {
        headers: headers
      });
    }
  }, {
    key: "post1",
    value: function post1(url) {
      var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      var _url = baseUrlPrefix + url;
      var _options$body4 = options.body,
        body = _options$body4 === void 0 ? {} : _options$body4,
        _options$extraHeader4 = options.extraHeader,
        extraHeader = _options$extraHeader4 === void 0 ? {} : _options$extraHeader4;
      var data = isObject(body) ? body : trim(JSON.parse(body));
      var headers = {};
      if (extraHeader && extraHeader.msg) {
        headers.operation = encodeURIComponent(extraHeader.msg);
      }
      if (extraHeader && extraHeader !== null && extraHeader !== void 0 && extraHeader.systemCode) {
        headers.systemCode = extraHeader.systemCode;
      }
      return this.instance.post(_url, data, {
        headers: headers
      });
    }

    /**
     * post 请求数据中台
     * @param {*} url
     * @param {*} options
     */
  }, {
    key: "postDataCenter",
    value: function postDataCenter() {
      var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var _url = "".concat(baseUrlPrefix, "/cloud/sjzt-reborn/api/v101/dataSet/execute");
      var _options$body5 = options.body,
        body = _options$body5 === void 0 ? {} : _options$body5,
        _options$extraHeader5 = options.extraHeader,
        extraHeader = _options$extraHeader5 === void 0 ? {} : _options$extraHeader5;
      var headers = {
        tenantId: getVtxToken('tenantId'),
        'Content-Type': 'application/json;charset=UTF-8'
      };
      if (extraHeader) {
        headers['operation'] = extraHeader.msg;
      }
      return this.instance.post(_url, _objectSpread(_objectSpread({}, body), {}, {
        parameters: body !== null && body !== void 0 && body.parameters ? _objectSpread(_objectSpread({}, body.parameters), {}, {
          tenantId: getVtxToken('tenantId')
        }) : {}
      }), {
        headers: headers
      });
    }

    /**
     * 响应数据的处理
     * @param {*} response
     */
  }, {
    key: "_responseHandler",
    value: function _responseHandler(response) {
      var status = response.status,
        data = response.data,
        statusText = response.statusText;
      if (status >= 200 && status < 300) {
        if (data.result === 0) {
          return data;
        } else {
          if (config.url.indexOf('zdjg/jg/data/getJgDataStatistics') > -1 || config.url.indexOf('/cloud/zd/base/api/cp/commonFactory') > -1) {
            return;
          }
          message.error(data.msg || '请求信息错误');
          return;
        }
      }
      var error = new Error(statusText);
      error.response = response;
      throw error;
    }

    /**
     * 请求错误处理
     * @param {*} error
     */
  }, {
    key: "_errorHandler",
    value: function _errorHandler(error) {
      if (error.response) {
        if (error.response.data.path.indexOf('zdjg/jg/data/getJgDataStatistics') > -1 || error.response.data.path.indexOf('/cloud/zd/base/api/cp/commonFactory') > -1) {
          return;
        }
        var msg;
        switch (error.response.status) {
          case 404:
            msg = '接口请求不存在！错误码【404】。';
            break;
          case 500:
            msg = '服务端应用接口异常！错误码【500】。';
            break;
          default:
            msg = '请求错误，请检查或刷新重试！';
            break;
        }
        message.error(msg);
      } else {
        message.error('服务不可用');
      }
    }

    // 获取实例
  }, {
    key: "getInstance",
    value: function getInstance() {
      return this.instance;
    }
  }]);
  return Http;
}();
var http = new Http({});
export default http;