"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _message = _interopRequireDefault(require("antd/lib/message"));
var _PublicService = _interopRequireDefault(require("../_mapUtil/PublicService"));
var _getUrlParam2 = _interopRequireDefault(require("@vtx/utils/lib/getUrlParam"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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 _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); }
function Download(_ref) {
  var url = _ref.url,
    params = _ref.params,
    _ref$method = _ref.method,
    method = _ref$method === void 0 ? 'GET' : _ref$method,
    selToken = _ref.selToken,
    selFileName = _ref.selFileName;
  var hide = _message["default"].loading('下载中...', 0);
  return new Promise(function (resolve, reject) {
    var xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';
    xhr.onload = function () {
      resolve(xhr);
    };
    xhr.onerror = function (err) {
      reject();
    };

    // let token = PublicService.getCookie('token');
    var _getUrlParam = (0, _getUrlParam2["default"])(),
      token = _getUrlParam.token;
    token = selToken || token || _PublicService["default"].getCookie('token') || sessionStorage.getItem('token');
    if (method === 'POST') {
      xhr.open('post', url, true);
      xhr.setRequestHeader('Authorization', "Bearer ".concat(token));
      xhr.setRequestHeader('Content-Type', 'application/json');
      var data = JSON.stringify(params);
      xhr.send(data);
    } else {
      if (params) {
        xhr.open('GET', "".concat(url, "?").concat(_PublicService["default"].paramSerializer(_objectSpread({}, params))));
      } else {
        xhr.open('GET', url);
      }
      xhr.setRequestHeader('Authorization', "Bearer ".concat(token));
      xhr.setRequestHeader('Content-Type', 'charset=UTF-8');
      xhr.send();
    }
  }).then(function (xhr) {
    var a = document.createElement('a');
    a.href = window.URL.createObjectURL(xhr.response);
    var header = xhr.getResponseHeader('Content-Disposition');
    if (header) {
      var fileName = header.split("filename*=utf-8'zh_cn'")[1]; // 文件后缀
      a.download = decodeURIComponent(fileName);
    }
    a.style.display = 'none';
    document.body.appendChild(a);
    a.click();
    hide();
    return xhr;
  });
}
var _default = exports["default"] = Download;
//# sourceMappingURL=Download.js.map
