download.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. export default (url = '', baseUrl='', data = {},dataType = 'JSON', type = 'GET', requestType='FORMDATA') => {
  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. }
  8. else if(process.env.NODE_ENV === 'production')
  9. {
  10. if(baseUrl) {
  11. switch (baseUrl) {
  12. case "/landcrm":
  13. baseUrl = '/landcrm';//云环境 基础服务
  14. break;
  15. default:
  16. baseUrl = "";
  17. break;
  18. }
  19. }
  20. }
  21. url = baseUrl + url;
  22. let dataStr = ''; //数据拼接字符串
  23. for(var key in data){
  24. if(typeof(data[key]) == "undefined") {
  25. data[key] = '';
  26. }
  27. }
  28. if (type == 'GET') {
  29. if(typeof(data)=="object"){
  30. Object.keys(data).forEach(key => {
  31. dataStr += key + '=' + data[key] + '&';
  32. })
  33. }
  34. if (dataStr !== '') {
  35. dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
  36. }
  37. url = url + '?' + dataStr;
  38. }
  39. else{
  40. if(requestType === "FORMDATA")
  41. {
  42. Object.keys(data).forEach(key => {
  43. dataStr += key + '=' + data[key] + '&';
  44. })
  45. dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
  46. }
  47. else if(requestType === "JSON")
  48. {
  49. dataStr = JSON.stringify(data)
  50. }
  51. }
  52. var xhr = new XMLHttpRequest();
  53. xhr.open(type, url, true);
  54. xhr.responseType = 'arraybuffer';
  55. xhr.onload = function () {
  56. if (this.status === 200) {
  57. //提取文件名
  58. var filename = "";
  59. var disposition = xhr.getResponseHeader('Content-disposition');
  60. if (disposition && disposition.indexOf('attachment') !== -1) {
  61. var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
  62. var matches = filenameRegex.exec(disposition);
  63. if (matches != null && matches[1]) filename = decodeURI(matches[1].replace(/['"]/g, ''));
  64. }
  65. var type2 = xhr.getResponseHeader('Content-Type');
  66. if(typeof File === 'undefined' && typeof Blob === 'undefined')
  67. {
  68. alert("当前浏览器不支持下载,请使用最新浏览器重试");
  69. return;
  70. }
  71. var blob = typeof File === 'function'
  72. ? new File([this.response], filename, { type: type2 })
  73. : new Blob([this.response], { type: type2 });
  74. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  75. window.navigator.msSaveBlob(blob, filename);
  76. } else {
  77. var URL = window.URL || window.webkitURL;
  78. var downloadUrl = URL.createObjectURL(blob);
  79. if (filename) {
  80. var a = document.createElement("a");
  81. if (typeof a.download === 'undefined') {
  82. window.location = downloadUrl;
  83. } else {
  84. a.href = downloadUrl;
  85. a.download = filename;
  86. document.body.appendChild(a);
  87. a.click();
  88. }
  89. } else {
  90. window.location = downloadUrl;
  91. }
  92. setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100);
  93. }
  94. }
  95. $('.ivu-spin-fullscreen-wrapper').remove();
  96. };
  97. if(requestType && requestType === "JSON") {
  98. xhr.setRequestHeader('Content-type', 'application/json');
  99. }
  100. else {
  101. xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  102. }
  103. if(localStorage.token != "" && typeof(localStorage.token) !== "undefined") {
  104. // header_content.token = localStorage.token;
  105. xhr.setRequestHeader('token', localStorage.token);
  106. }
  107. if(localStorage.user_id != "" && typeof(localStorage.user_id) !== "undefined") {
  108. // header_content.userId = localStorage.user_id;
  109. xhr.setRequestHeader('userId', localStorage.user_id);
  110. }
  111. if(type == 'POST') {
  112. xhr.send(dataStr);
  113. }
  114. else {
  115. xhr.send();
  116. }
  117. }