123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- export default (url = '', baseUrl = '', data = {}, dataType = 'JSON', type = 'GET') => {
- 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>'
- $('#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();
- }
- }
|