export default (url = '', baseUrl='', data = {},dataType = 'JSON', type = 'GET', requestType='FORMDATA') => {
var html = '
'
$('#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;
default:
baseUrl = "";
break;
}
}
}
url = baseUrl + url;
let dataStr = ''; //数据拼接字符串
for(var key in data){
if(typeof(data[key]) == "undefined") {
data[key] = '';
}
}
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{
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');
}
if(localStorage.token != "" && typeof(localStorage.token) !== "undefined") {
// header_content.token = localStorage.token;
xhr.setRequestHeader('token', localStorage.token);
}
if(localStorage.user_id != "" && typeof(localStorage.user_id) !== "undefined") {
// header_content.userId = localStorage.user_id;
xhr.setRequestHeader('userId', localStorage.user_id);
}
if(type == 'POST') {
xhr.send(dataStr);
}
else {
xhr.send();
}
}