"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getUrlPara = exports.getFieldRender = exports["default"] = void 0;
exports.secondToFormatTime = secondToFormatTime;
exports.transformRgb = void 0;
var _axios = _interopRequireDefault(require("axios"));
var _qs = _interopRequireDefault(require("qs"));
var _isObject = _interopRequireDefault(require("lodash/isObject"));
var _pickBy = _interopRequireDefault(require("lodash/pickBy"));
var _antd = require("antd");
var _utils = require("@vtx/utils");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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 _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : 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
// 安装axios的同时会同步安装qs包 https://www.npmjs.com/package/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);
  }
  // 初始化
  return _createClass(Http, [{
    key: "_createAxios",
    value: function _createAxios(options) {
      var token = (0, _utils.getVtxToken)('token');
      // 创建实例
      var instance = _axios["default"].create(Object.assign(Object.assign({
        // `timeout` 指定请求超时的毫秒数(0 表示无超时时间)
        // 如果请求话费了超过 `timeout` 的时间，请求将被中断
        timeout: MILL_SECOND * SECOND
      }, options), {
        headers: Object.assign({
          'Cache-Control': 'no-cache',
          Authorization: token ? "Bearer ".concat(token) : '',
          tenantId: (0, _utils.getVtxToken)('tenantId'),
          userId: (0, _utils.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 = (0, _utils.getVtxToken)('token');
      // 过滤掉值为 '' || undefined || null 的对象属性
      var postData = (0, _isObject["default"])(body) ? (0, _pickBy["default"])((0, _utils.trim)(body), function (value) {
        return value !== '' && value !== undefined && value !== null;
      }) : {};
      return this.instance.get(_url, {
        params: Object.assign({}, postData),
        headers: Object.assign({
          Authorization: token ? "Bearer ".concat(token) : '',
          tenantId: (0, _utils.getVtxToken)('tenantId'),
          userId: (0, _utils.getVtxToken)('userId')
        }, (extraHeader === null || extraHeader === void 0 ? void 0 : extraHeader.systemCode) ? {
          systemCode: extraHeader.systemCode || ''
        } : {})
      });
    }
    /**
     * get 请求
     * @param {*} url
     * @param {*} options {body}
     */
  }, {
    key: "getForTenantId",
    value: function getForTenantId(url) {
      var _this = this;
      var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      var _a;
      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 = (0, _utils.getVtxToken)('token');
      // 过滤掉值为 '' || undefined || null 的对象属性
      var postData = (0, _isObject["default"])(body) ? (0, _pickBy["default"])((0, _utils.trim)(body), function (value) {
        return value !== '' && value !== undefined && value !== null;
      }) : {};
      var tenantId = (_a = Object.keys(body)) === null || _a === void 0 ? void 0 : _a.find(function (item) {
        return item == 'tenantId';
      });
      if (tenantId && (0, _utils.getVtxToken)('token')) {
        return this.get('/cloud/management/api/v101/userAuth/getUserInfo', {
          data: {
            token: (0, _utils.getVtxToken)('token')
          }
        }).then(function (res) {
          var _a, _b, _c, _d;
          if (res.result == 0 && ((_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.tenantId)) {
            return _this.instance.get(_url, {
              params: Object.assign(Object.assign({}, postData), {
                tenantId: (_b = res === null || res === void 0 ? void 0 : res.data) === null || _b === void 0 ? void 0 : _b.tenantId
              }),
              headers: Object.assign({
                Authorization: token ? "Bearer ".concat(token) : '',
                tenantId: ((_c = res === null || res === void 0 ? void 0 : res.data) === null || _c === void 0 ? void 0 : _c.tenantId) || (0, _utils.getVtxToken)('tenantId'),
                userId: ((_d = res === null || res === void 0 ? void 0 : res.data) === null || _d === void 0 ? void 0 : _d.userId) || (0, _utils.getVtxToken)('userId')
              }, (extraHeader === null || extraHeader === void 0 ? void 0 : extraHeader.systemCode) ? {
                systemCode: extraHeader.systemCode || ''
              } : {})
            });
          }
        });
      } else {
        return this.instance.get(_url, {
          params: Object.assign({}, postData),
          headers: Object.assign({
            Authorization: token ? "Bearer ".concat(token) : '',
            tenantId: (0, _utils.getVtxToken)('tenantId'),
            userId: (0, _utils.getVtxToken)('userId')
          }, (extraHeader === null || extraHeader === void 0 ? 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 = (0, _isObject["default"])(body) ? _qs["default"].stringify((0, _utils.trim)(body)) : (0, _utils.trim)(JSON.parse(body));
      var headers = {};
      if (extraHeader && extraHeader.msg) {
        headers.operation = encodeURIComponent(extraHeader.msg);
      }
      if (extraHeader && (extraHeader === null || extraHeader === void 0 ? 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 = (0, _isObject["default"])(body) ? body : (0, _utils.trim)(JSON.parse(body));
      var headers = {};
      if (extraHeader && extraHeader.msg) {
        headers.operation = encodeURIComponent(extraHeader.msg);
      }
      if (extraHeader && (extraHeader === null || extraHeader === void 0 ? 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: (0, _utils.getVtxToken)('tenantId'),
        'Content-Type': 'application/json;charset=UTF-8'
      };
      if (extraHeader) {
        headers['operation'] = extraHeader.msg;
      }
      return this.instance.post(_url, Object.assign(Object.assign({}, body), {
        parameters: (body === null || body === void 0 ? void 0 : body.parameters) ? Object.assign(Object.assign({}, body.parameters), {
          tenantId: (0, _utils.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 {
          _antd.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) {
        var msg;
        switch (error.response.status) {
          case 404:
            msg = '接口请求不存在！错误码【404】。';
            break;
          case 500:
            msg = '服务端应用接口异常！错误码【500】。';
            break;
          default:
            msg = '请求错误，请检查或刷新重试！';
            break;
        }
        _antd.message.error(msg);
      } else {
        _antd.message.error('服务不可用');
      }
    }
    // 获取实例
  }, {
    key: "getInstance",
    value: function getInstance() {
      return this.instance;
    }
  }]);
}();
var http = new Http({});
var _default = exports["default"] = http; // 秒转化为时间的格式化字符串
function secondToFormatTime(times) {
  if (times <= 0) return '--';
  var timeStr = '';
  var remain = times;
  var days = Math.floor(remain / (24 * 60 * 60));
  if (days) timeStr += "".concat(days, "\u5929");
  remain = remain % (24 * 60 * 60);
  var hours = Math.floor(remain / (60 * 60));
  if (hours) timeStr += "".concat(hours, "h");
  remain = remain % (60 * 60);
  var minutes = Math.floor(remain / 60);
  if (minutes) timeStr += "".concat(minutes, "min");
  remain = remain % 60;
  var seconds = Math.floor(remain);
  if (seconds) timeStr += "".concat(seconds, "s");
  return timeStr;
}
var getUrlPara = exports.getUrlPara = function getUrlPara(key) {
  return (0, _utils.getVtxToken)(key);
};
var getFieldRender = exports.getFieldRender = function getFieldRender(fields, info) {
  var list = [];
  fields.forEach(function (item) {
    if (((info === null || info === void 0 ? void 0 : info[item.showKey]) || item.showKey == 'tenderName') && !['address', 'divisionName', 'accUnitName', 'manageUnitName', 'description', 'panoramaLinkAddress'].includes(item.showKey)) {
      list.push({
        label: item.name,
        key: item.showKey,
        value: info === null || info === void 0 ? void 0 : info[item.showKey],
        componentType: item.component.type
      });
    }
  });
  fields.forEach(function (item) {
    var _a, _b;
    if (['address', 'divisionName', 'accUnitName', 'manageUnitName', 'description', 'panoramaLinkAddress'].includes(item.showKey)) {
      list.push({
        label: item.name,
        key: item.showKey,
        value: info === null || info === void 0 ? void 0 : info[item.showKey],
        componentType: item.component.type
      });
    } else if ((_a = info === null || info === void 0 ? void 0 : info.dataJson) === null || _a === void 0 ? void 0 : _a[item.showKey]) {
      list.push({
        label: item.name,
        key: item.showKey,
        value: (_b = info === null || info === void 0 ? void 0 : info.dataJson) === null || _b === void 0 ? void 0 : _b[item.showKey],
        componentType: item.component.type
      });
    }
  });
  return list;
};
var transformRgb = exports.transformRgb = function transformRgb(color) {
  var n = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  // 16进制颜色值的正则
  var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  // 把颜色值变成小写
  var newColor = color.toLowerCase();
  if (reg.test(newColor)) {
    // 如果只有三位的值，需变成六位，如：#fff => #ffffff
    if (newColor.length === 4) {
      var colorNew = '#';
      for (var i = 1; i < 4; i += 1) {
        colorNew += newColor.slice(i, i + 1).concat(newColor.slice(i, i + 1));
      }
      newColor = colorNew;
    }
    // 处理六位的颜色值，转为RGB
    var colorChange = [];
    for (var _i = 1; _i < 7; _i += 2) {
      colorChange.push(parseInt("0x".concat(newColor.slice(_i, _i + 2))));
    }
    var _rgb = "rgba(".concat(colorChange.join(','), ",").concat(n, ")");
    return _rgb;
  } else {
    return newColor;
  }
};
//# sourceMappingURL=index.js.map
