leisure-core 1.0.3 → 1.0.5

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.
@@ -1,62 +1,151 @@
1
- import axios from "axios";
2
- import { getToken } from "./tokens";
3
-
4
- /**
5
- * 下载 excel
6
- * @param {*} url 接口地址
7
- * @param {*} fileName 下载文件名称
8
- * @param {*} data 数据
9
- * @param {*} callback 回调函数
10
- */
11
- function downLoadFile(url, fileName, data) {
12
- axios({
13
- method: "get",
14
- url: url,
15
- params: data,
16
- headers: {
17
- Accept: "image/webp,image/apng,image/*,*/*;q=0.8",
18
- "Content-type": "application/octet-stream;charset=UTF-8",
19
- authentication: getToken(),
20
- },
21
- responseType: "blob",
22
- })
23
- .then((response) => {
24
- let blob = new Blob([response.data]);
25
- let href = window.URL.createObjectURL(blob);
26
- let downloadElement = document.createElement("a");
27
- try {
28
- downloadElement.href = href;
29
- downloadElement.download = fileName + ".xlsx";
30
- document.body.appendChild(downloadElement);
31
- downloadElement.click();
32
- } catch (error) {
33
- return Promise.reject("下载错误:" + error);
34
- } finally {
35
- document.body.removeChild(downloadElement);
36
- window.URL.revokeObjectURL(href);
37
- }
38
- })
39
- .catch((error) => {
40
- return Promise.reject("获取下载数据错误:" + error);
41
- });
42
- }
43
-
44
- export function exportExcelByParam(path, name, param) {
45
- let url = process.env.VUE_APP_BASE_API + path;
46
- downLoadFile(url, name, param);
47
- }
48
-
49
- export function exportMemberExcel(name, params) {
50
- let url = process.env.VUE_APP_BASE_API + "/user/exportMember";
51
- downLoadFile(url, name, params);
52
- }
53
-
54
- export function exportConsumeExcel(name, params) {
55
- let url = process.env.VUE_APP_BASE_API + "/order/export";
56
- downLoadFile(url, name, params);
57
- }
58
-
59
- export function exportGoodsExcel(name, params) {
60
- let url = process.env.VUE_APP_BASE_API + "/mall-goods/export";
61
- downLoadFile(url, name, params);
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
+ }
@@ -1622,6 +1622,7 @@ h2 {
1622
1622
  .query-param-area {
1623
1623
  display: -webkit-flex;
1624
1624
  display: flex;
1625
+ flex-wrap: wrap;
1625
1626
  }
1626
1627
 
1627
1628
  .vxe-loading-mask {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leisure-core",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "leisure-core是京心数据基于vue2.x开发的一套后台管理系统桌面端组件库,封装了大量实用的UI控件模板,非常方便开发者快速搭建前端应用",
5
5
  "private": false,
6
6
  "author": "北方乐逍遥(zcx7878)",