leisure-core 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/le-libs/js/hrequestFile.js +151 -62
- package/package.json +1 -1
|
@@ -1,62 +1,151 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
|
-
import {
|
|
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
|
-
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { Message } from "element-ui";
|
|
3
|
+
import { getToken } from "./tokens";
|
|
4
|
+
|
|
5
|
+
const POLL_INTERVAL = 3000;
|
|
6
|
+
|
|
7
|
+
function authHeaders() {
|
|
8
|
+
return {
|
|
9
|
+
Accept: "image/webp,image/apng,image/*,*/*;q=0.8",
|
|
10
|
+
"Content-type": "application/octet-stream;charset=UTF-8",
|
|
11
|
+
authentication: getToken(),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 保存 blob 为本地文件
|
|
17
|
+
*/
|
|
18
|
+
function saveBlob(blob, fileName) {
|
|
19
|
+
let href = window.URL.createObjectURL(blob);
|
|
20
|
+
let downloadElement = document.createElement("a");
|
|
21
|
+
try {
|
|
22
|
+
downloadElement.href = href;
|
|
23
|
+
downloadElement.download = fileName + ".xlsx";
|
|
24
|
+
document.body.appendChild(downloadElement);
|
|
25
|
+
downloadElement.click();
|
|
26
|
+
} finally {
|
|
27
|
+
document.body.removeChild(downloadElement);
|
|
28
|
+
window.URL.revokeObjectURL(href);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function readBlobText(blob) {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
let reader = new FileReader();
|
|
35
|
+
reader.onload = () => resolve(reader.result);
|
|
36
|
+
reader.onerror = () => reject(reader.error);
|
|
37
|
+
reader.readAsText(blob);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 轮询导出任务状态, SUCCESS 后 resolve(taskId), FAILED 后 reject
|
|
43
|
+
*/
|
|
44
|
+
function pollTask(statusUrl, taskId, onProgress) {
|
|
45
|
+
return new Promise((resolve, reject) => {
|
|
46
|
+
let timer = setInterval(() => {
|
|
47
|
+
axios
|
|
48
|
+
.get(statusUrl, { params: { taskId }, headers: authHeaders() })
|
|
49
|
+
.then((response) => {
|
|
50
|
+
let task = response.data;
|
|
51
|
+
if (onProgress) onProgress(task);
|
|
52
|
+
if (task.status === "SUCCESS") {
|
|
53
|
+
clearInterval(timer);
|
|
54
|
+
resolve(task.taskId);
|
|
55
|
+
} else if (task.status === "FAILED") {
|
|
56
|
+
clearInterval(timer);
|
|
57
|
+
reject(new Error(task.error || "导出失败"));
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
.catch((error) => {
|
|
61
|
+
clearInterval(timer);
|
|
62
|
+
reject(error);
|
|
63
|
+
});
|
|
64
|
+
}, POLL_INTERVAL);
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 异步导出流程: 提交后拿到 taskId -> 轮询进度 -> 下载文件
|
|
70
|
+
* @param submitUrl 提交接口地址(以 /download/orders 结尾), 用于推导任务接口
|
|
71
|
+
*/
|
|
72
|
+
function handleAsyncTask(submitUrl, blob, fileName, onProgress) {
|
|
73
|
+
let baseUrl = submitUrl.substring(0, submitUrl.lastIndexOf("/") + 1);
|
|
74
|
+
return readBlobText(blob)
|
|
75
|
+
.then((text) => {
|
|
76
|
+
let info = JSON.parse(text);
|
|
77
|
+
if (!info.taskId) {
|
|
78
|
+
throw new Error(info.error || "提交导出任务失败");
|
|
79
|
+
}
|
|
80
|
+
if (onProgress) onProgress(info);
|
|
81
|
+
return pollTask(baseUrl + "task-status", info.taskId, onProgress);
|
|
82
|
+
})
|
|
83
|
+
.then((taskId) => {
|
|
84
|
+
return axios
|
|
85
|
+
.get(baseUrl + "task-file", {
|
|
86
|
+
params: { taskId },
|
|
87
|
+
headers: authHeaders(),
|
|
88
|
+
responseType: "blob",
|
|
89
|
+
})
|
|
90
|
+
.then((response) => {
|
|
91
|
+
saveBlob(new Blob([response.data]), fileName);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 下载 excel
|
|
98
|
+
* @param {*} url 接口地址
|
|
99
|
+
* @param {*} fileName 下载文件名称
|
|
100
|
+
* @param {*} data 数据
|
|
101
|
+
* @param {*} onProgress 可选, 异步导出进度回调 (task) => void
|
|
102
|
+
*
|
|
103
|
+
* 根据响应 Content-Type 自动识别:
|
|
104
|
+
* application/json -> 异步任务模式(提交/轮询/下载);
|
|
105
|
+
* 其他 -> 同步文件流直接下载(旧行为)
|
|
106
|
+
*/
|
|
107
|
+
function downLoadFile(url, fileName, data, onProgress) {
|
|
108
|
+
return axios({
|
|
109
|
+
method: "get",
|
|
110
|
+
url: url,
|
|
111
|
+
params: data,
|
|
112
|
+
headers: authHeaders(),
|
|
113
|
+
responseType: "blob",
|
|
114
|
+
})
|
|
115
|
+
.then((response) => {
|
|
116
|
+
let contentType = response.headers["content-type"] || "";
|
|
117
|
+
let blob = new Blob([response.data], { type: contentType });
|
|
118
|
+
if (contentType.indexOf("application/json") >= 0) {
|
|
119
|
+
return handleAsyncTask(url, blob, fileName, onProgress);
|
|
120
|
+
}
|
|
121
|
+
return saveBlob(blob, fileName);
|
|
122
|
+
})
|
|
123
|
+
.catch((error) => {
|
|
124
|
+
let message = "获取下载数据错误";
|
|
125
|
+
if (error && error.message) {
|
|
126
|
+
message += ":" + error.message;
|
|
127
|
+
}
|
|
128
|
+
Message.error(message);
|
|
129
|
+
return Promise.reject(error);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function exportExcelByParam(path, name, param, onProgress) {
|
|
134
|
+
let url = process.env.VUE_APP_BASE_API + path;
|
|
135
|
+
return downLoadFile(url, name, param, onProgress);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function exportMemberExcel(name, params) {
|
|
139
|
+
let url = process.env.VUE_APP_BASE_API + "/user/exportMember";
|
|
140
|
+
return downLoadFile(url, name, params);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function exportConsumeExcel(name, params) {
|
|
144
|
+
let url = process.env.VUE_APP_BASE_API + "/order/export";
|
|
145
|
+
return downLoadFile(url, name, params);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function exportGoodsExcel(name, params) {
|
|
149
|
+
let url = process.env.VUE_APP_BASE_API + "/mall-goods/export";
|
|
150
|
+
return downLoadFile(url, name, params);
|
|
151
|
+
}
|