zipdownload.js
8.24 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import axios from "axios";
import store from "@/store";
import { getToken } from "@/utils/auth";
import { getDownLoadFileStatus } from "@/utils/iClearExp.js";
import { alertWarningMsg } from "@/utils/index.js";
const mimeMap = {
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
xlsm: "application/vnd.ms-excel.sheet.macroEnabled.12",
xls: "application/vnd.ms-excel",
zip: "application/zip",
pdf: "application/pdf",
png: "image/png",
jpg: "image/jpeg",
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
};
const apiUrl = {
//exl
declareDataModel: "/bus-customer/declareData/downloadTemplate",
// 清单查询下载
listExport: "/bus-customer/exportList/listExport",
};
const baseUrl = process.env.VUE_APP_BASE_API;
export function downLoadZip(urlStatus, str, filename, type, method, data) {
return new Promise((resolve, reject) => {
var url = "";
if (urlStatus == "api") {
url = baseUrl + apiUrl[str];
} else if (urlStatus == "res") {
url = str;
}
console.log(url);
if (method == "get" && data) {
url = url + "?";
for (const propName of Object.keys(data)) {
const value = data[propName];
var part = encodeURIComponent(propName) + "=";
if (value !== null && typeof value !== "undefined") {
if (typeof value === "object") {
for (const key of Object.keys(value)) {
let params = propName + "[" + key + "]";
var subPart = encodeURIComponent(params) + "=";
url += subPart + encodeURIComponent(value[key]) + "&";
}
} else {
url += part + encodeURIComponent(value) + "&";
}
}
}
url = url.slice(0, -1);
}
axios({
method: method,
url: url,
data: data,
responseType: "blob",
headers: {
Authorization: "Bearer " + getToken(),
Ver: process.env.VUE_APP_APIVERSION,
},
})
.then((res) => {
if (filename == null) {
if (res.headers["content-disposition"]) {
var patt = new RegExp("filename=([^;]+\\.[^\\.;]+);*");
var contentDisposition = decodeURI(
res.headers["content-disposition"]
);
var result = patt.exec(contentDisposition);
filename = result[0].split("=")[1];
} else {
filename = "error";
}
}
resolveBlob(res, mimeMap[type], filename);
resolve();
})
.catch(() => {
reject();
});
});
}
/**
* 解析blob响应内容并下载
* @param {*} res blob响应内容
* @param {String} mimeType MIME类型
*/
export function resolveBlob(res, mimeType, filename) {
const aLink = document.createElement("a");
var blob = new Blob([res.data], { type: mimeType });
if (blob.size < 500) {
var reader = new FileReader();
reader.readAsText(blob, "utf-8");
reader.onloadend = () => {
// 获取文本内容并将其转换为JSON格式
if (reader.result && reader.result != null && reader.result != "") {
// try {
// JSON.parse(reader.result)
// } catch (error) {
// }
if (JSON.parse(reader.result)) {
var jsonData = JSON.parse(reader.result);
alertWarningMsg(jsonData.message);
} else {
alertWarningMsg("文件无法下载,请重新生成后再试!");
}
} else {
alertWarningMsg("文件无法下载,请重新生成后再试!");
}
};
} else {
// //从response的headers中获取filename, 后端response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 设置的文件名;
// var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
// var contentDisposition = decodeURI(res.headers['content-disposition'])
// var result = patt.exec(contentDisposition)
var fileName = filename;
fileName = fileName.replace(/\"/g, "");
aLink.href = URL.createObjectURL(blob);
aLink.setAttribute("download", fileName); // 设置下载文件名称
document.body.appendChild(aLink);
aLink.click();
document.body.removeChild(aLink);
}
}
export function downLoadFile(url, filename, type) {
const aLink = document.createElement("a");
const fileName = filename;
aLink.href = URL.createObjectURL(url);
aLink.setAttribute("download", fileName); // 设置下载文件名称
document.body.appendChild(aLink);
aLink.click();
document.body.removeChild(aLink);
}
export function formDataDownload(
str,
filename,
type,
method,
data,
exportSign
) {
store.dispatch("SetLoadingType", {
type: true,
text: "正在导出中,请稍候!",
});
return new Promise((resolve, reject) => {
let iframe = document.createElement("iframe");
iframe.style.display = "none";
iframe.name = "formTarget";
document.getElementsByTagName("body")[0].appendChild(iframe);
var form = document.createElement("form");
form.target = "formTarget";
form.action = baseUrl + apiUrl.customDownloadXls;
form.method = method;
var tokenInput = document.createElement("input");
tokenInput.type = "hidden";
tokenInput.name = "Param_Authorization";
tokenInput.value = getToken();
var belongBizTypeInput = document.createElement("input");
belongBizTypeInput.type = "hidden";
belongBizTypeInput.name = "downloadSreachData";
belongBizTypeInput.value = JSON.stringify(data);
var verInput = document.createElement("input");
verInput.type = "hidden";
verInput.name = "Param_Ver";
verInput.value = process.env.VUE_APP_APIVERSION;
form.appendChild(tokenInput);
form.appendChild(belongBizTypeInput);
form.appendChild(verInput);
document.getElementsByTagName("body")[0].appendChild(form);
form.submit();
document.getElementsByTagName("body")[0].removeChild(form);
// 查询导出结果
// getType = setInterval(() => {
getDownLoadFileStatus(exportSign)
.then(() => {
resolve();
})
.catch(() => {
reject();
});
// getdownloadExlFileType(exportSign).then((res) => {
// if (res.code == '200') {
// if (res.data.extra == 'true') {
// clearInterval(getType)
// store.dispatch('SetLoadingType', { type: false, text: '' })
// resolve()
// }
// } else {
// clearInterval(getType)
// store.dispatch('SetLoadingType', { type: false, text: '' })
// alertWarningMsg(res.message)
// reject()
// }
// }).catch(() => {
// clearInterval(getType)
// store.dispatch('SetLoadingType', { type: false, text: '' })
// reject()
// })
// }, 1000);
});
}
//form.submit()导出xls/xlsx
export function formSubmitDownload(url, method, data, exportSign = null) {
return new Promise((resolve, reject) => {
let iframe = document.createElement("iframe");
iframe.style.display = "none";
iframe.name = "formTarget";
document.getElementsByTagName("body")[0].appendChild(iframe);
var form = document.createElement("form");
form.target = "formTarget";
form.action = baseUrl + apiUrl[url];
form.method = method;
if (data) {
let domObj = {};
data.forEach((item, i) => {
domObj[`input${i}`] = document.createElement("input");
domObj[`input${i}`].type = "hidden";
domObj[`input${i}`].name = item.key;
domObj[`input${i}`].value = item.value;
form.appendChild(domObj[`input${i}`]);
});
var tokenInput = document.createElement("input");
tokenInput.type = "hidden";
tokenInput.name = "Param_Authorization";
tokenInput.value = getToken();
form.appendChild(tokenInput);
var verInput = document.createElement("input");
verInput.type = "hidden";
verInput.name = "Param_Ver";
verInput.value = process.env.VUE_APP_APIVERSION;
form.appendChild(verInput);
}
document.getElementsByTagName("body")[0].appendChild(form);
form.submit();
document.getElementsByTagName("body")[0].removeChild(form);
if (exportSign != null) {
getDownLoadFileStatus(exportSign)
.then(() => {
resolve();
})
.catch(() => {
reject();
});
} else {
resolve();
}
});
}