export default (url = '', baseUrl = '', data = {}, dataType = 'JSON', type = 'GET') => { var html = '
Loading
' $('#app').append(html); type = type.toUpperCase(); if (process.env.NODE_ENV === 'development') { baseUrl = baseUrl; } else if (process.env.NODE_ENV === 'production') { if (baseUrl) { switch (baseUrl) { case '/landcrm': baseUrl = '/landcrm'; //其他环境 break; case '/rcrm': baseUrl = '/landcrm'; //报表地址 break; case '/decorationManage': baseUrl = '/decorationManage'; //收费地址 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; } else { Object.keys(data).forEach(key => { // if(data[key]) // { dataStr += key + '=' + data[key] + '&'; // } }) dataStr = dataStr.substr(0, dataStr.lastIndexOf('&')); } var xhr = new XMLHttpRequest(); xhr.open(type, url, true); xhr.responseType = 'arraybuffer'; xhr.setRequestHeader("companyId", localStorage.companyId || ""); 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(); }; xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhr.setRequestHeader("token", localStorage.token || ""); if (type == 'POST') { xhr.send(dataStr); } else { xhr.send(); } }