exportExcel.js
2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { CONSTANT } from '../constant.js'
import axios from 'axios'
import qs from 'qs'
import { Message } from "element-ui";
export default (url, oParam, datatype, cb) => {
/*let exportForm = document.querySelector("#exportForm");
if(exportForm){
document.body.removeChild(exportForm)
}
exportForm = document.createElement('form');
exportForm.id = 'exportForm';
exportForm.action = CONSTANT.API_URL+url;
exportForm.method = datatype?datatype:'get';
exportForm.attributes['accept-charset'] = 'utf-8';
exportForm.style.display = 'none';
document.body.appendChild(exportForm);
let _input = '';
for(let key in oParam){
_input+='<input type="hidden" name="'+key+'" id="'+key+'" value="'+(oParam[key])+'"/>';
}
exportForm.innerHTML = _input;
exportForm.submit();*/
url = CONSTANT.API_URL+url
if (datatype == 'post') {
axios.post(url, typeof oParam == 'string'?oParam:qs.stringify(oParam), {
responseType: 'blob', //指定返回数据的格式为blob
headers: {
'Content-Type': typeof oParam == 'string'?'application/json':'application/x-www-form-urlencoded; charset=UTF-8'
}
})
.then(response => {
if (response) {
successCb(response, cb)
}
})
.catch(err => {
console.log(`接口调用失败`);
console.log(err);
});
} else {
axios.get(url, /*qs.stringify(oParam),*/ {
responseType: 'blob', //指定返回数据的格式为blob
params: oParam,
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
})
.then(response => {
if (response) {
successCb(response)
}
})
.catch(err => {
console.log(`接口调用失败`);
console.log(err);
});
}
function successCb(response, cb) {
if (response.headers && response.headers.sessionstatus == 'timeout') {
window.location.href = '../ots/login';
return;
}
if (response.headers && response.headers['content-disposition']) {
let filename = decodeURIComponent(response.headers['content-disposition'].replace('attachment;filename=', ''));
if (window.navigator.appVersion.toString().indexOf('.NET') > 0) {
window.navigator.msSaveBlob(response.data, filename);
} else {
let url = window.URL.createObjectURL(response.data);
console.log(url)
var a = document.createElement("a");
document.body.appendChild(a);
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
}
} else {
let fr = new FileReader();
fr.onload = function() {
const res = JSON.parse(this.result);
Message({
message: res.message,
type: 'error'
});
};
fr.readAsText(response.data);
}
cb && cb()
}
}