let project = host.PROJECT; export default (url = '', baseUrl = '', data = {}, dataType = 'JSON', type = 'GET', requestType = 'FORMDATA') => { var html = '
Loading
' $('#app').append(html); // debugger type = type.toUpperCase(); if (process.env.NODE_ENV === 'development') { baseUrl = baseUrl; } else if (process.env.NODE_ENV === 'production') { if (baseUrl) { // let buildUrl = "https://test.hajwy.com/" let buildUrl = "https://pms.hajwy.com/" switch (baseUrl) { case '/landcrm': baseUrl = prodServer("landcrm"); break; case '/activity': //微信运营-活动 baseUrl = prodServer("activity"); break; case '/steward': //微信运营-管家 baseUrl = prodServer("steward"); break; case '/knowledge': //微信运营-文章 baseUrl = prodServer("knowledge"); break; case '/manage': //微信运营-会员 baseUrl = prodServer("manage"); break; case '/community': //会员认证 community: 正式, communityDev: 测试,qpi:幸福基业 baseUrl = prodServer("community"); break; case '/points': //积分 baseUrl = prodServer("points"); break; case '/question': //调查问卷 baseUrl = prodServer("question"); break; case '/activiti': // 订餐 会议室预订 baseUrl = prodServer("activiti"); break; case '/member': //会员管理 baseUrl = prodServer("member"); break; case '/voucher': // baseUrl = prodServer("voucher"); break; case '/bus': //楼巴管理 baseUrl = prodServer("bus"); break; case '/guesthouse': //招待管理 baseUrl = prodServer("guesthouse"); break; case '/apartment': //公寓管理 baseUrl = prodServer("apartment"); break; case '/purchase': // baseUrl = buildUrl + "purchase"; // baseUrl = "https://test.hajwy.com/purchase"; break; case '/hajwy': // baseUrl = buildUrl + "qpi/rest"; // baseUrl = "https://test.hajwy.com/qpi/rest"; break; case '/mkf': // baseUrl = buildUrl + "landcrm"; // baseUrl = "https://test.hajwy.com/landcrm"; break; default: baseUrl = ""; break; } } } url = baseUrl + url; let dataStr = ''; //数据拼接字符串 if (type == 'GET') { if (typeof (data) == "object") { Object.keys(data).forEach(key => { dataStr += key + '=' + data[key] + '&'; }) } if (dataStr !== '') { dataStr = dataStr.substr(0, dataStr.lastIndexOf('&')); url = url + '?' + dataStr; } // url = url + '?' + dataStr; } else { if (requestType === "FORMDATA") { Object.keys(data).forEach(key => { dataStr += key + '=' + data[key] + '&'; }) dataStr = dataStr.substr(0, dataStr.lastIndexOf('&')); } else if (requestType === "JSON") { dataStr = JSON.stringify(data) } } var xhr = new XMLHttpRequest(); xhr.open(type, url, true); xhr.responseType = 'arraybuffer'; xhr.onload = function () { if (this.status === 200) { //提取文件名 var filename = ""; var disposition = xhr.getResponseHeader('Content-disposition'); if (disposition && disposition.indexOf('attachment') !== -1) { var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/; var matches = filenameRegex.exec(disposition); if (matches != null && matches[1]) filename = decodeURI(matches[1].replace(/['"]/g, '')); } var type2 = xhr.getResponseHeader('Content-Type'); if (typeof File === 'undefined' && typeof Blob === 'undefined') { alert("当前浏览器不支持下载,请使用最新浏览器重试"); return; } var blob = typeof File === 'function' ? new File([this.response], filename, { type: type2 }) : new Blob([this.response], { type: type2 }); if (typeof window.navigator.msSaveBlob !== 'undefined') { window.navigator.msSaveBlob(blob, filename); } else { var URL = window.URL || window.webkitURL; var downloadUrl = URL.createObjectURL(blob); if (filename) { var a = document.createElement("a"); if (typeof a.download === 'undefined') { window.location = downloadUrl; } else { a.href = downloadUrl; a.download = filename; document.body.appendChild(a); a.click(); } } else { window.location = downloadUrl; } setTimeout(function () { URL.revokeObjectURL(downloadUrl); }, 100); } } $('.ivu-spin-fullscreen-wrapper').remove(); }; if (requestType && requestType === "JSON") { xhr.setRequestHeader('Content-type', 'application/json'); } else { xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); } xhr.setRequestHeader('token', localStorage.token || ""); if (type == 'POST') { xhr.send(dataStr); } else { xhr.send(); } }