migrationSupport.js
1.18 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
export function unwrapData(response) {
if (response && Object.prototype.hasOwnProperty.call(response, "data")) {
return response.data;
}
return response || {};
}
export function normalizePage(response) {
const payload = unwrapData(response);
const nested = payload.data || payload;
const rows = nested.list || nested.rows || nested.records || nested.pageData || [];
const total = Number(nested.total || nested.totalCount || nested.count || rows.length || 0);
return { rows, total };
}
export function extractDownloadUrl(response) {
const payload = unwrapData(response);
if (typeof payload === "string") {
return payload;
}
return payload.url || payload.fileUrl || payload.downloadUrl || "";
}
export function openDownload(response) {
const url = extractDownloadUrl(response);
if (url && typeof window !== "undefined") {
window.open(url, "_blank");
}
return url;
}
export function validateUpload(file, {
maxSizeMb = 20,
types = ["image/jpeg", "image/png", "application/pdf"],
} = {}) {
if (!types.includes(file.type)) {
return "仅支持 JPG、PNG 或 PDF 文件";
}
if (file.size > maxSizeMb * 1024 * 1024) {
return `文件大小不能超过 ${maxSizeMb}MB`;
}
return "";
}