download.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. export default (url = '', baseUrl = '', data = {}, dataType = 'JSON', type = 'GET') => {
  2. var html = '<div class="ivu-spin-fullscreen ivu-spin-fullscreen-wrapper"><div class="ivu-spin ivu-spin-fix ivu-spin-show-text ivu-spin-fullscreen"><div class="ivu-spin-main"><span class="ivu-spin-dot"></span> <div class="ivu-spin-text"><div><i class="ivu-icon ivu-icon-load-c demo-spin-icon-load" style="font-size: 18px;"></i><div>Loading</div></div></div></div></div></div>'
  3. $('#app').append(html);
  4. type = type.toUpperCase();
  5. if (process.env.NODE_ENV === 'development') {
  6. baseUrl = baseUrl;
  7. } else if (process.env.NODE_ENV === 'production') {
  8. if (baseUrl) {
  9. switch (baseUrl) {
  10. case '/landcrm':
  11. baseUrl = '/landcrm'; //其他环境
  12. break;
  13. case '/rcrm':
  14. baseUrl = '/landcrm'; //报表地址
  15. break;
  16. case '/decorationManage':
  17. baseUrl = '/decorationManage'; //收费地址
  18. break;
  19. default:
  20. baseUrl = "";
  21. break;
  22. }
  23. }
  24. }
  25. url = baseUrl + url;
  26. let dataStr = ''; //数据拼接字符串
  27. if (type == 'GET') {
  28. if (typeof (data) == "object") {
  29. Object.keys(data).forEach(key => {
  30. dataStr += key + '=' + data[key] + '&';
  31. })
  32. }
  33. if (dataStr !== '') {
  34. dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
  35. }
  36. url = url + '?' + dataStr;
  37. } else {
  38. Object.keys(data).forEach(key => {
  39. // if(data[key])
  40. // {
  41. dataStr += key + '=' + data[key] + '&';
  42. // }
  43. })
  44. dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
  45. }
  46. var xhr = new XMLHttpRequest();
  47. xhr.open(type, url, true);
  48. xhr.responseType = 'arraybuffer';
  49. xhr.setRequestHeader("companyId", localStorage.companyId || "");
  50. xhr.onload = function () {
  51. if (this.status === 200) {
  52. //提取文件名
  53. var filename = "";
  54. var disposition = xhr.getResponseHeader('Content-disposition');
  55. if (disposition && disposition.indexOf('attachment') !== -1) {
  56. var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
  57. var matches = filenameRegex.exec(disposition);
  58. if (matches != null && matches[1]) filename = decodeURI(matches[1].replace(/['"]/g, ''));
  59. }
  60. var type2 = xhr.getResponseHeader('Content-Type');
  61. if (typeof File === 'undefined' && typeof Blob === 'undefined') {
  62. alert("当前浏览器不支持下载,请使用最新浏览器重试");
  63. return;
  64. }
  65. var blob = typeof File === 'function' ?
  66. new File([this.response], filename, {
  67. type: type2
  68. }) :
  69. new Blob([this.response], {
  70. type: type2
  71. });
  72. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  73. window.navigator.msSaveBlob(blob, filename);
  74. } else {
  75. var URL = window.URL || window.webkitURL;
  76. var downloadUrl = URL.createObjectURL(blob);
  77. if (filename) {
  78. var a = document.createElement("a");
  79. if (typeof a.download === 'undefined') {
  80. window.location = downloadUrl;
  81. } else {
  82. a.href = downloadUrl;
  83. a.download = filename;
  84. document.body.appendChild(a);
  85. a.click();
  86. }
  87. } else {
  88. window.location = downloadUrl;
  89. }
  90. setTimeout(function () {
  91. URL.revokeObjectURL(downloadUrl);
  92. }, 100);
  93. }
  94. }
  95. $('.ivu-spin-fullscreen-wrapper').remove();
  96. };
  97. xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  98. xhr.setRequestHeader("token", localStorage.token || "");
  99. if (type == 'POST') {
  100. xhr.send(dataStr);
  101. } else {
  102. xhr.send();
  103. }
  104. }