download.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. let project = host.PROJECT;
  2. export default (url = '', baseUrl = '', data = {}, dataType = 'JSON', type = 'GET', requestType = 'FORMDATA') => {
  3. 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>'
  4. $('#app').append(html);
  5. // debugger
  6. type = type.toUpperCase();
  7. if (process.env.NODE_ENV === 'development') {
  8. baseUrl = baseUrl;
  9. } else if (process.env.NODE_ENV === 'production') {
  10. if (baseUrl) {
  11. // let buildUrl = "https://test.hajwy.com/"
  12. let buildUrl = "https://pms.hajwy.com/"
  13. switch (baseUrl) {
  14. case '/landcrm':
  15. baseUrl = prodServer("landcrm");
  16. break;
  17. case '/activity': //微信运营-活动
  18. baseUrl = prodServer("activity");
  19. break;
  20. case '/steward': //微信运营-管家
  21. baseUrl = prodServer("steward");
  22. break;
  23. case '/knowledge': //微信运营-文章
  24. baseUrl = prodServer("knowledge");
  25. break;
  26. case '/manage': //微信运营-会员
  27. baseUrl = prodServer("manage");
  28. break;
  29. case '/community': //会员认证 community: 正式, communityDev: 测试,qpi:幸福基业
  30. baseUrl = prodServer("community");
  31. break;
  32. case '/points': //积分
  33. baseUrl = prodServer("points");
  34. break;
  35. case '/question': //调查问卷
  36. baseUrl = prodServer("question");
  37. break;
  38. case '/activiti': // 订餐 会议室预订
  39. baseUrl = prodServer("activiti");
  40. break;
  41. case '/member': //会员管理
  42. baseUrl = prodServer("member");
  43. break;
  44. case '/voucher': //
  45. baseUrl = prodServer("voucher");
  46. break;
  47. case '/bus': //楼巴管理
  48. baseUrl = prodServer("bus");
  49. break;
  50. case '/guesthouse': //招待管理
  51. baseUrl = prodServer("guesthouse");
  52. break;
  53. case '/apartment': //公寓管理
  54. baseUrl = prodServer("apartment");
  55. break;
  56. case '/purchase': //
  57. baseUrl = buildUrl + "purchase";
  58. // baseUrl = "https://test.hajwy.com/purchase";
  59. break;
  60. case '/hajwy': //
  61. baseUrl = buildUrl + "qpi/rest";
  62. // baseUrl = "https://test.hajwy.com/qpi/rest";
  63. break;
  64. case '/mkf': //
  65. baseUrl = buildUrl + "landcrm";
  66. // baseUrl = "https://test.hajwy.com/landcrm";
  67. break;
  68. default:
  69. baseUrl = "";
  70. break;
  71. }
  72. }
  73. }
  74. url = baseUrl + url;
  75. let dataStr = ''; //数据拼接字符串
  76. if (type == 'GET') {
  77. if (typeof (data) == "object") {
  78. Object.keys(data).forEach(key => {
  79. dataStr += key + '=' + data[key] + '&';
  80. })
  81. }
  82. if (dataStr !== '') {
  83. dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
  84. url = url + '?' + dataStr;
  85. }
  86. // url = url + '?' + dataStr;
  87. } else {
  88. if (requestType === "FORMDATA") {
  89. Object.keys(data).forEach(key => {
  90. dataStr += key + '=' + data[key] + '&';
  91. })
  92. dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
  93. } else if (requestType === "JSON") {
  94. dataStr = JSON.stringify(data)
  95. }
  96. }
  97. var xhr = new XMLHttpRequest();
  98. xhr.open(type, url, true);
  99. xhr.responseType = 'arraybuffer';
  100. xhr.onload = function () {
  101. if (this.status === 200) {
  102. //提取文件名
  103. var filename = "";
  104. var disposition = xhr.getResponseHeader('Content-disposition');
  105. if (disposition && disposition.indexOf('attachment') !== -1) {
  106. var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
  107. var matches = filenameRegex.exec(disposition);
  108. if (matches != null && matches[1]) filename = decodeURI(matches[1].replace(/['"]/g, ''));
  109. }
  110. var type2 = xhr.getResponseHeader('Content-Type');
  111. if (typeof File === 'undefined' && typeof Blob === 'undefined') {
  112. alert("当前浏览器不支持下载,请使用最新浏览器重试");
  113. return;
  114. }
  115. var blob = typeof File === 'function' ?
  116. new File([this.response], filename, {
  117. type: type2
  118. }) :
  119. new Blob([this.response], {
  120. type: type2
  121. });
  122. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  123. window.navigator.msSaveBlob(blob, filename);
  124. } else {
  125. var URL = window.URL || window.webkitURL;
  126. var downloadUrl = URL.createObjectURL(blob);
  127. if (filename) {
  128. var a = document.createElement("a");
  129. if (typeof a.download === 'undefined') {
  130. window.location = downloadUrl;
  131. } else {
  132. a.href = downloadUrl;
  133. a.download = filename;
  134. document.body.appendChild(a);
  135. a.click();
  136. }
  137. } else {
  138. window.location = downloadUrl;
  139. }
  140. setTimeout(function () {
  141. URL.revokeObjectURL(downloadUrl);
  142. }, 100);
  143. }
  144. }
  145. $('.ivu-spin-fullscreen-wrapper').remove();
  146. };
  147. if (requestType && requestType === "JSON") {
  148. xhr.setRequestHeader('Content-type', 'application/json');
  149. } else {
  150. xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  151. }
  152. xhr.setRequestHeader('token', localStorage.token || "");
  153. if (type == 'POST') {
  154. xhr.send(dataStr);
  155. } else {
  156. xhr.send();
  157. }
  158. }