download.js 5.4 KB

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