@hzab/utils 1.1.6 → 1.1.7
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/CHANGELOG.md +11 -0
- package/LICENSE +21 -0
- package/dist/index.cjs +1532 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +741 -0
- package/dist/index.d.ts +741 -0
- package/dist/index.js +1423 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -49
- package/src/color.ts +8 -8
- package/src/file/file.ts +4 -3
- package/src/file/fileType.ts +1 -1
- package/src/formily/field.ts +142 -0
- package/src/global.d.ts +9 -0
- package/src/idCardVerify.ts +1 -1
- package/src/img-utils.ts +9 -9
- package/src/index.ts +43 -0
- package/src/nanoid.ts +19 -19
- package/src/options.ts +248 -226
- package/src/upload/OfflineUpload.ts +2 -2
- package/src/upload/OssUploadUtil.ts +1 -1
- package/src/upload/fileDataNormalization.ts +3 -3
- package/src/upload/ossUpload.ts +2 -1
- package/src/url.ts +181 -155
- package/README.md +0 -137
- package/changelog.md +0 -56
package/dist/index.js
ADDED
|
@@ -0,0 +1,1423 @@
|
|
|
1
|
+
import { customAlphabet } from 'nanoid';
|
|
2
|
+
import { cloneDeep } from 'lodash-es';
|
|
3
|
+
import dayjs2 from 'dayjs';
|
|
4
|
+
import { axios } from '@hzab/data-model';
|
|
5
|
+
|
|
6
|
+
// src/array.ts
|
|
7
|
+
function getArr(value) {
|
|
8
|
+
return Array.isArray(value) ? value : value && [value] || [];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// src/base64Str.ts
|
|
12
|
+
var isBase64Str = function(str) {
|
|
13
|
+
if (typeof str !== "string") {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
return /^data:\w+\/\w+;base64/i.test(str);
|
|
17
|
+
};
|
|
18
|
+
function getFileTypeFromBase64(base64Str) {
|
|
19
|
+
if (!isBase64Str(base64Str)) {
|
|
20
|
+
return base64Str;
|
|
21
|
+
}
|
|
22
|
+
const base64Data = base64Str.replace(/^data:.*;base64,/, "");
|
|
23
|
+
const prefixMap = {
|
|
24
|
+
JVBERi0: { type: "pdf", mimeType: "application/pdf" },
|
|
25
|
+
R0lGODdh: { type: "gif", mimeType: "image/gif" },
|
|
26
|
+
R0lGODlh: { type: "gif", mimeType: "image/gif" },
|
|
27
|
+
iVBORw0KGgo: { type: "png", mimeType: "image/png" },
|
|
28
|
+
"/9j/": { type: "jpeg", mimeType: "image/jpeg" },
|
|
29
|
+
SUQzB: { type: "mp4", mimeType: "video/mp4" },
|
|
30
|
+
AAAAGGZ0eXBpc29tAA: { type: "mpeg", mimeType: "audio/mpeg" },
|
|
31
|
+
UklGR: { type: "webp", mimeType: "image/webp" },
|
|
32
|
+
PD94bWwgdmVyc2lvbj0: { type: "xml", mimeType: "text/xml" },
|
|
33
|
+
PHN2ZyB4bWxucz0: { type: "svg", mimeType: "image/svg+xml" },
|
|
34
|
+
dGV4dCB4bWxucz0: { type: "txt", mimeType: "text/plain" }
|
|
35
|
+
};
|
|
36
|
+
for (const prefix of Object.keys(prefixMap)) {
|
|
37
|
+
if (base64Data.startsWith(prefix)) {
|
|
38
|
+
return prefixMap[prefix];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return { type: "unknown", mimeType: "application/octet-stream" };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// src/blob.ts
|
|
45
|
+
async function getFileTypeFromBlob(blob) {
|
|
46
|
+
const fileSignatures = {
|
|
47
|
+
"89504E47": "image/png",
|
|
48
|
+
FFD8FF: "image/jpeg",
|
|
49
|
+
"47494638": "image/gif",
|
|
50
|
+
"25504446": "application/pdf",
|
|
51
|
+
"504B0304": "application/zip",
|
|
52
|
+
// ZIP 或 DOCX/XLSX 等基于 ZIP 的格式
|
|
53
|
+
"3C3F786D6C": "text/xml",
|
|
54
|
+
// XML
|
|
55
|
+
"7B22": "application/json",
|
|
56
|
+
// JSON(开头可能为 { 或 {")
|
|
57
|
+
"25215053": "application/postscript",
|
|
58
|
+
// PS
|
|
59
|
+
"494433": "audio/mpeg",
|
|
60
|
+
// MP3
|
|
61
|
+
"0000001866747970": "video/mp4"
|
|
62
|
+
// MP4(部分情况)
|
|
63
|
+
};
|
|
64
|
+
const buffer = await blob.slice(0, 8).arrayBuffer();
|
|
65
|
+
const uint8Array = new Uint8Array(buffer);
|
|
66
|
+
let hex = "";
|
|
67
|
+
for (let i = 0; i < uint8Array.length; i++) {
|
|
68
|
+
const byte = uint8Array[i].toString(16).padStart(2, "0");
|
|
69
|
+
hex += byte.toUpperCase();
|
|
70
|
+
}
|
|
71
|
+
const signatures = Object.keys(fileSignatures).sort((a, b) => b.length - a.length);
|
|
72
|
+
for (const signature of signatures) {
|
|
73
|
+
if (hex.startsWith(signature)) {
|
|
74
|
+
return fileSignatures[signature];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return "unknown";
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// src/color.ts
|
|
81
|
+
function luminance(r, g, b) {
|
|
82
|
+
const a = [r, g, b].map((v) => {
|
|
83
|
+
v /= 255;
|
|
84
|
+
return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
|
|
85
|
+
});
|
|
86
|
+
return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
|
|
87
|
+
}
|
|
88
|
+
function contrast(l1, l2) {
|
|
89
|
+
const brightest = Math.max(l1, l2);
|
|
90
|
+
const darkest = Math.min(l1, l2);
|
|
91
|
+
return (brightest + 0.05) / (darkest + 0.05);
|
|
92
|
+
}
|
|
93
|
+
function contrastRatio(luminance1, luminance2) {
|
|
94
|
+
return (Math.max(luminance1, luminance2) + 0.05) / (Math.min(luminance1, luminance2) + 0.05);
|
|
95
|
+
}
|
|
96
|
+
function hexToRGB(hex) {
|
|
97
|
+
hex = hex.replace("#", "");
|
|
98
|
+
if (hex.length === 3) {
|
|
99
|
+
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
|
|
100
|
+
}
|
|
101
|
+
const r = parseInt(hex.substring(0, 2), 16);
|
|
102
|
+
const g = parseInt(hex.substring(2, 4), 16);
|
|
103
|
+
const b = parseInt(hex.substring(4, 6), 16);
|
|
104
|
+
return [r, g, b];
|
|
105
|
+
}
|
|
106
|
+
function rgbNumLimit(val) {
|
|
107
|
+
if (val > 250) {
|
|
108
|
+
return 250;
|
|
109
|
+
}
|
|
110
|
+
if (val < 0) {
|
|
111
|
+
return 0;
|
|
112
|
+
}
|
|
113
|
+
return val;
|
|
114
|
+
}
|
|
115
|
+
function rgbToHex(r, g, b) {
|
|
116
|
+
return "#" + [r, g, b].map((x) => {
|
|
117
|
+
const hex = x.toString(16);
|
|
118
|
+
return hex.length === 1 ? "0" + hex : hex;
|
|
119
|
+
}).join("");
|
|
120
|
+
}
|
|
121
|
+
function rgbToHsl(r, g, b) {
|
|
122
|
+
r /= 255;
|
|
123
|
+
g /= 255;
|
|
124
|
+
b /= 255;
|
|
125
|
+
const max = Math.max(r, g, b);
|
|
126
|
+
const min = Math.min(r, g, b);
|
|
127
|
+
let h, s, l = (max + min) / 2;
|
|
128
|
+
if (max === min) {
|
|
129
|
+
h = s = 0;
|
|
130
|
+
} else {
|
|
131
|
+
const d = max - min;
|
|
132
|
+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
133
|
+
switch (max) {
|
|
134
|
+
case r:
|
|
135
|
+
h = (g - b) / d + (g < b ? 6 : 0);
|
|
136
|
+
break;
|
|
137
|
+
case g:
|
|
138
|
+
h = (b - r) / d + 2;
|
|
139
|
+
break;
|
|
140
|
+
case b:
|
|
141
|
+
h = (r - g) / d + 4;
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
h /= 6;
|
|
145
|
+
}
|
|
146
|
+
return [h * 360, s * 100, l * 100];
|
|
147
|
+
}
|
|
148
|
+
function hslToRgb(h, s, l) {
|
|
149
|
+
h /= 360;
|
|
150
|
+
s /= 100;
|
|
151
|
+
l /= 100;
|
|
152
|
+
let r, g, b;
|
|
153
|
+
if (s === 0) {
|
|
154
|
+
r = g = b = l;
|
|
155
|
+
} else {
|
|
156
|
+
const hue2rgb = (p2, q2, t) => {
|
|
157
|
+
if (t < 0) t += 1;
|
|
158
|
+
if (t > 1) t -= 1;
|
|
159
|
+
if (t < 1 / 6) return p2 + (q2 - p2) * 6 * t;
|
|
160
|
+
if (t < 1 / 2) return q2;
|
|
161
|
+
if (t < 2 / 3) return p2 + (q2 - p2) * (2 / 3 - t) * 6;
|
|
162
|
+
return p2;
|
|
163
|
+
};
|
|
164
|
+
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
165
|
+
const p = 2 * l - q;
|
|
166
|
+
r = hue2rgb(p, q, h + 1 / 3);
|
|
167
|
+
g = hue2rgb(p, q, h);
|
|
168
|
+
b = hue2rgb(p, q, h - 1 / 3);
|
|
169
|
+
}
|
|
170
|
+
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
|
|
171
|
+
}
|
|
172
|
+
function isGray(r, g, b, opt = { tolerance: 10 }) {
|
|
173
|
+
const {
|
|
174
|
+
/**
|
|
175
|
+
* 允许的误差范围
|
|
176
|
+
*/
|
|
177
|
+
tolerance = 10
|
|
178
|
+
} = opt || {};
|
|
179
|
+
return Math.abs(r - g) <= tolerance && Math.abs(g - b) <= tolerance && Math.abs(r - b) <= tolerance;
|
|
180
|
+
}
|
|
181
|
+
function getTextColor(backgroundColor) {
|
|
182
|
+
let [r, g, b] = [0, 0, 0];
|
|
183
|
+
if (backgroundColor.startsWith("#")) {
|
|
184
|
+
[r, g, b] = hexToRGB(backgroundColor);
|
|
185
|
+
} else {
|
|
186
|
+
const div = document.createElement("div");
|
|
187
|
+
div.style.color = backgroundColor;
|
|
188
|
+
document.body.appendChild(div);
|
|
189
|
+
const cs = getComputedStyle(div);
|
|
190
|
+
const rgb = cs.color.match(/\d+/g).map(Number);
|
|
191
|
+
document.body.removeChild(div);
|
|
192
|
+
[r, g, b] = rgb;
|
|
193
|
+
}
|
|
194
|
+
const bgLuminance = luminance(r, g, b);
|
|
195
|
+
const whiteLuminance = luminance(255, 255, 255);
|
|
196
|
+
const blackLuminance = luminance(0, 0, 0);
|
|
197
|
+
const whiteContrast = contrast(bgLuminance, whiteLuminance);
|
|
198
|
+
const blackContrast = contrast(bgLuminance, blackLuminance);
|
|
199
|
+
return whiteContrast > blackContrast ? "#ffffff" : "#000000";
|
|
200
|
+
}
|
|
201
|
+
function adjustTextColor(hexColor) {
|
|
202
|
+
const [bgR, bgG, bgB] = hexToRGB(hexColor);
|
|
203
|
+
const bgLuminance = luminance(bgR, bgG, bgB);
|
|
204
|
+
let textR, textG, textB;
|
|
205
|
+
const targetContrast = 4.5;
|
|
206
|
+
if (bgR === 0 && bgG === 0 && bgB === 0) {
|
|
207
|
+
let textR2 = 255, textG2 = 255, textB2 = 255;
|
|
208
|
+
const textLuminance = luminance(textR2, textG2, textB2);
|
|
209
|
+
const ratio = contrastRatio(bgLuminance, textLuminance);
|
|
210
|
+
if (ratio < targetContrast) {
|
|
211
|
+
textR2 = 230;
|
|
212
|
+
textG2 = 230;
|
|
213
|
+
textB2 = 230;
|
|
214
|
+
}
|
|
215
|
+
return rgbToHex(textR2, textG2, textB2);
|
|
216
|
+
} else if (bgR === 255 && bgG === 255 && bgB === 255) {
|
|
217
|
+
let textR2 = 0, textG2 = 0, textB2 = 0;
|
|
218
|
+
const textLuminance = luminance(textR2, textG2, textB2);
|
|
219
|
+
const ratio = contrastRatio(bgLuminance, textLuminance);
|
|
220
|
+
if (ratio < targetContrast) {
|
|
221
|
+
textR2 = 20;
|
|
222
|
+
textG2 = 20;
|
|
223
|
+
textB2 = 20;
|
|
224
|
+
}
|
|
225
|
+
return rgbToHex(textR2, textG2, textB2);
|
|
226
|
+
} else if (isGray(bgR, bgG, bgB)) {
|
|
227
|
+
if (bgLuminance < 0.4) {
|
|
228
|
+
let textR2 = 255, textG2 = 255, textB2 = 255;
|
|
229
|
+
const textLuminance = luminance(textR2, textG2, textB2);
|
|
230
|
+
const ratio = contrastRatio(bgLuminance, textLuminance);
|
|
231
|
+
if (ratio < targetContrast) {
|
|
232
|
+
textR2 = 230;
|
|
233
|
+
textG2 = 230;
|
|
234
|
+
textB2 = 230;
|
|
235
|
+
}
|
|
236
|
+
return rgbToHex(textR2, textG2, textB2);
|
|
237
|
+
} else {
|
|
238
|
+
let textR2 = 0, textG2 = 0, textB2 = 0;
|
|
239
|
+
const textLuminance = luminance(textR2, textG2, textB2);
|
|
240
|
+
const ratio = contrastRatio(bgLuminance, textLuminance);
|
|
241
|
+
if (ratio < targetContrast) {
|
|
242
|
+
textR2 = 20;
|
|
243
|
+
textG2 = 20;
|
|
244
|
+
textB2 = 20;
|
|
245
|
+
}
|
|
246
|
+
return rgbToHex(textR2, textG2, textB2);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
const adjustNum = 150;
|
|
250
|
+
if (bgLuminance > 0.5) {
|
|
251
|
+
textR = Math.max(0, bgR - adjustNum);
|
|
252
|
+
textG = Math.max(0, bgG - adjustNum);
|
|
253
|
+
textB = Math.max(0, bgB - adjustNum);
|
|
254
|
+
} else {
|
|
255
|
+
textR = Math.min(255, bgR + adjustNum);
|
|
256
|
+
textG = Math.min(255, bgG + adjustNum);
|
|
257
|
+
textB = Math.min(255, bgB + adjustNum);
|
|
258
|
+
}
|
|
259
|
+
return rgbToHex(textR, textG, textB);
|
|
260
|
+
}
|
|
261
|
+
function adjustColor(hexColor, amount) {
|
|
262
|
+
const [r, g, b] = hexToRGB(hexColor);
|
|
263
|
+
const newR = rgbNumLimit(r + amount);
|
|
264
|
+
const newG = rgbNumLimit(g + amount);
|
|
265
|
+
const newB = rgbNumLimit(b + amount);
|
|
266
|
+
return rgbToHex(newR, newG, newB);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// src/empty-val.ts
|
|
270
|
+
function checkEmpty(val, opt = {}) {
|
|
271
|
+
const { checkEmptyArr = true } = opt || {};
|
|
272
|
+
if (val === "" || val === null || val === void 0) {
|
|
273
|
+
return true;
|
|
274
|
+
}
|
|
275
|
+
if (checkEmptyArr && Array.isArray(val) && Array.length === 0) {
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
var handleEmptyVal = (val, opt = {}) => {
|
|
281
|
+
const { emptyStr = "--" } = opt || {};
|
|
282
|
+
const emptySymbol = opt?.emptySymbol ?? emptyStr;
|
|
283
|
+
if (checkEmpty(val, opt)) {
|
|
284
|
+
return emptySymbol;
|
|
285
|
+
}
|
|
286
|
+
return val;
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
// src/globalConfig.ts
|
|
290
|
+
window.utilsGlobalServeConfig = {};
|
|
291
|
+
function getGlobalServeConfig() {
|
|
292
|
+
return window.utilsGlobalServeConfig || {};
|
|
293
|
+
}
|
|
294
|
+
function setGlobalServeConfigs(data) {
|
|
295
|
+
Object.assign(window.utilsGlobalServeConfig || {}, data);
|
|
296
|
+
return window.utilsGlobalServeConfig || {};
|
|
297
|
+
}
|
|
298
|
+
function setGlobalServeConfig(key, data) {
|
|
299
|
+
window.utilsGlobalServeConfig[key] = data;
|
|
300
|
+
return window.utilsGlobalServeConfig || {};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// src/idCardVerify.ts
|
|
304
|
+
function isValidIdCard(idCard, opt) {
|
|
305
|
+
if (!idCard) return false;
|
|
306
|
+
idCard = idCard.trim().toUpperCase();
|
|
307
|
+
const len = idCard.length;
|
|
308
|
+
if (len !== 15 && len !== 18) return false;
|
|
309
|
+
const reg15 = /^[1-9]\d{7}((0[1-9])|(1[0-2]))((0[1-9])|([1-2]\d)|(3[0-1]))\d{3}$/;
|
|
310
|
+
const reg18 = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))((0[1-9])|([1-2]\d)|(3[0-1]))\d{3}[\dX]$/;
|
|
311
|
+
if (len === 15 && !reg15.test(idCard)) return false;
|
|
312
|
+
if (len === 18 && !reg18.test(idCard)) return false;
|
|
313
|
+
let id18 = idCard;
|
|
314
|
+
if (len === 15) {
|
|
315
|
+
id18 = convert15to18(idCard);
|
|
316
|
+
}
|
|
317
|
+
if (opt?.validBirthDate && !isValidBirthDate(id18)) return false;
|
|
318
|
+
return verifyCheckCode(id18);
|
|
319
|
+
}
|
|
320
|
+
function isValidBirthDate(id18) {
|
|
321
|
+
const birthYear = parseInt(id18.substring(6, 10));
|
|
322
|
+
const birthMonth = parseInt(id18.substring(10, 12));
|
|
323
|
+
const birthDay = parseInt(id18.substring(12, 14));
|
|
324
|
+
const currentYear = (/* @__PURE__ */ new Date()).getFullYear();
|
|
325
|
+
if (birthYear < 1900 || birthYear > currentYear) {
|
|
326
|
+
return false;
|
|
327
|
+
}
|
|
328
|
+
if (birthMonth < 1 || birthMonth > 12) {
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
const daysInMonth = getDaysInMonth(birthYear, birthMonth);
|
|
332
|
+
if (birthDay < 1 || birthDay > daysInMonth) {
|
|
333
|
+
return false;
|
|
334
|
+
}
|
|
335
|
+
return true;
|
|
336
|
+
}
|
|
337
|
+
function getDaysInMonth(year, month) {
|
|
338
|
+
const isLeapYear = year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
|
|
339
|
+
const daysInMonth = [31, isLeapYear ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
340
|
+
return daysInMonth[month - 1];
|
|
341
|
+
}
|
|
342
|
+
function convert15to18(id15) {
|
|
343
|
+
const yearTwoDigits = parseInt(id15.substring(6, 8));
|
|
344
|
+
const currentYear = (/* @__PURE__ */ new Date()).getFullYear();
|
|
345
|
+
const currentYearTwoDigits = currentYear % 100;
|
|
346
|
+
let fullYear;
|
|
347
|
+
if (yearTwoDigits > currentYearTwoDigits) {
|
|
348
|
+
fullYear = 1900 + yearTwoDigits;
|
|
349
|
+
} else {
|
|
350
|
+
fullYear = 2e3 + yearTwoDigits;
|
|
351
|
+
}
|
|
352
|
+
const id17 = id15.substring(0, 6) + fullYear + id15.substring(8);
|
|
353
|
+
const checkCode = calculateCheckCode(id17);
|
|
354
|
+
return id17 + checkCode;
|
|
355
|
+
}
|
|
356
|
+
function calculateCheckCode(id17) {
|
|
357
|
+
const weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
|
|
358
|
+
const checkCodes = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"];
|
|
359
|
+
let sum = 0;
|
|
360
|
+
for (let i = 0; i < 17; i++) {
|
|
361
|
+
sum += parseInt(id17[i]) * weights[i];
|
|
362
|
+
}
|
|
363
|
+
const mod = sum % 11;
|
|
364
|
+
return checkCodes[mod];
|
|
365
|
+
}
|
|
366
|
+
function verifyCheckCode(id18) {
|
|
367
|
+
const id17 = id18.substring(0, 17);
|
|
368
|
+
const providedCode = id18[17];
|
|
369
|
+
const calculatedCode = calculateCheckCode(id17);
|
|
370
|
+
return providedCode === calculatedCode;
|
|
371
|
+
}
|
|
372
|
+
function calculateAge(year, month, day) {
|
|
373
|
+
const today = /* @__PURE__ */ new Date();
|
|
374
|
+
let age = today.getFullYear() - year;
|
|
375
|
+
const monthDiff = today.getMonth() + 1 - month;
|
|
376
|
+
if (monthDiff < 0 || monthDiff === 0 && today.getDate() < day) {
|
|
377
|
+
age--;
|
|
378
|
+
}
|
|
379
|
+
return age;
|
|
380
|
+
}
|
|
381
|
+
function getIdCardInfo(idCard) {
|
|
382
|
+
if (!isValidIdCard(idCard)) return null;
|
|
383
|
+
let id18 = idCard.trim().toUpperCase();
|
|
384
|
+
if (id18.length === 15) {
|
|
385
|
+
id18 = convert15to18(id18);
|
|
386
|
+
}
|
|
387
|
+
const provinceCode = id18.substring(0, 2);
|
|
388
|
+
const birthYear = parseInt(id18.substring(6, 10));
|
|
389
|
+
const birthMonth = parseInt(id18.substring(10, 12));
|
|
390
|
+
const birthDay = parseInt(id18.substring(12, 14));
|
|
391
|
+
const genderCode = parseInt(id18.substring(16, 17));
|
|
392
|
+
const birthDate = new Date(birthYear, birthMonth - 1, birthDay);
|
|
393
|
+
return {
|
|
394
|
+
province: provinceCode,
|
|
395
|
+
birthDate,
|
|
396
|
+
birthYear,
|
|
397
|
+
birthMonth,
|
|
398
|
+
birthDay,
|
|
399
|
+
age: calculateAge(birthYear, birthMonth, birthDay),
|
|
400
|
+
gender: genderCode % 2 === 1 ? "\u7537" : "\u5973",
|
|
401
|
+
isValid: true
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// src/img-utils.ts
|
|
406
|
+
var IMAGE_TYPES = class {
|
|
407
|
+
static JPEG = "image/jpeg";
|
|
408
|
+
static PNG = "image/png";
|
|
409
|
+
};
|
|
410
|
+
function checkEmptyImg(img) {
|
|
411
|
+
const canvas = document.createElement("canvas");
|
|
412
|
+
const ctx = canvas.getContext("2d");
|
|
413
|
+
if (!ctx) {
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
canvas.width = img.width;
|
|
417
|
+
canvas.height = img.height;
|
|
418
|
+
ctx.drawImage(img, 0, 0);
|
|
419
|
+
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
420
|
+
const data = imageData.data;
|
|
421
|
+
let topLeftToBottomRight = true;
|
|
422
|
+
const iUp = 0;
|
|
423
|
+
const redUp = data[iUp];
|
|
424
|
+
const greenUp = data[iUp + 1];
|
|
425
|
+
const blueUp = data[iUp + 2];
|
|
426
|
+
const alphaUp = data[iUp + 3];
|
|
427
|
+
for (let i = 0; i < data.length; i += 4) {
|
|
428
|
+
const red = data[i];
|
|
429
|
+
const green = data[i + 1];
|
|
430
|
+
const blue = data[i + 2];
|
|
431
|
+
const alpha = data[i + 3];
|
|
432
|
+
if (red !== redUp || green !== greenUp || blue !== blueUp || alpha !== alphaUp) {
|
|
433
|
+
topLeftToBottomRight = false;
|
|
434
|
+
break;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
let topRightToBottomLeft = true;
|
|
438
|
+
const jDown = data.length - 4;
|
|
439
|
+
const redDown = data[jDown];
|
|
440
|
+
const greenDown = data[jDown + 1];
|
|
441
|
+
const blueDown = data[jDown + 2];
|
|
442
|
+
const alphaDown = data[jDown + 3];
|
|
443
|
+
for (let j = jDown; j >= 0; j -= 4) {
|
|
444
|
+
const red = data[j];
|
|
445
|
+
const green = data[j + 1];
|
|
446
|
+
const blue = data[j + 2];
|
|
447
|
+
const alpha = data[j + 3];
|
|
448
|
+
if (red !== redDown || green !== greenDown || blue !== blueDown || alpha !== alphaDown) {
|
|
449
|
+
topRightToBottomLeft = false;
|
|
450
|
+
break;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
if (topLeftToBottomRight && topRightToBottomLeft) {
|
|
454
|
+
return false;
|
|
455
|
+
} else {
|
|
456
|
+
return true;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
function getDomWH(d) {
|
|
460
|
+
if (d instanceof Image || d instanceof HTMLCanvasElement) {
|
|
461
|
+
return {
|
|
462
|
+
width: d.width || d.offsetWidth,
|
|
463
|
+
height: d.width || d.offsetHeight
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
return {
|
|
467
|
+
width: d.offsetWidth,
|
|
468
|
+
height: d.offsetHeight
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
function setDomWH(dom, w, h) {
|
|
472
|
+
dom.setAttribute("width", `${w}`);
|
|
473
|
+
dom.setAttribute("height", `${h}`);
|
|
474
|
+
}
|
|
475
|
+
function getCanvasPic(canvas) {
|
|
476
|
+
canvas.getContext("2d", { preserveDrawingBuffer: true });
|
|
477
|
+
return dataURItoBlob(canvas.toDataURL(IMAGE_TYPES.PNG, 1));
|
|
478
|
+
}
|
|
479
|
+
function dataURItoBlob(base64) {
|
|
480
|
+
const arr = base64.split(",");
|
|
481
|
+
const mime = arr[0]?.match(/:(.*?);/)?.[1];
|
|
482
|
+
const bstr = atob(arr[1]);
|
|
483
|
+
let n = bstr.length;
|
|
484
|
+
const u8arr = new Uint8Array(n);
|
|
485
|
+
while (n--) {
|
|
486
|
+
u8arr[n] = bstr.charCodeAt(n);
|
|
487
|
+
}
|
|
488
|
+
return new Blob([u8arr], { type: mime });
|
|
489
|
+
}
|
|
490
|
+
function getImgEle(imgSrc) {
|
|
491
|
+
return new Promise((resolve) => {
|
|
492
|
+
const img = new Image();
|
|
493
|
+
img.onload = function() {
|
|
494
|
+
resolve(img);
|
|
495
|
+
};
|
|
496
|
+
img.src = imgSrc;
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
function getImgBase64(imgSrc) {
|
|
500
|
+
return getImgEle(imgSrc).then((img) => {
|
|
501
|
+
const canvas = document.createElement("canvas");
|
|
502
|
+
const { width, height } = getDomWH(img);
|
|
503
|
+
setDomWH(canvas, width, height);
|
|
504
|
+
const ctx = canvas.getContext("2d");
|
|
505
|
+
ctx && ctx.drawImage(img, 0, 0, width, height, 0, 0, width, height);
|
|
506
|
+
return canvas.toDataURL(IMAGE_TYPES.PNG, 1);
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
function getCanvas(imgSrc) {
|
|
510
|
+
return getImgEle(imgSrc).then((img) => {
|
|
511
|
+
const canvas = document.createElement("canvas");
|
|
512
|
+
const { width, height } = getDomWH(img);
|
|
513
|
+
setDomWH(canvas, width, height);
|
|
514
|
+
const ctx = canvas.getContext("2d");
|
|
515
|
+
ctx && ctx.drawImage(img, 0, 0, width, height, 0, 0, width, height);
|
|
516
|
+
return canvas;
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
function getCanvasByImg(img) {
|
|
520
|
+
const canvas = document.createElement("canvas");
|
|
521
|
+
const { width, height } = getDomWH(img);
|
|
522
|
+
setDomWH(canvas, width, height);
|
|
523
|
+
const ctx = canvas.getContext("2d");
|
|
524
|
+
ctx && ctx.drawImage(img, 0, 0, width, height, 0, 0, width, height);
|
|
525
|
+
return canvas;
|
|
526
|
+
}
|
|
527
|
+
function cropImage(img, opt) {
|
|
528
|
+
return cropImageToCanvas(img, opt).toDataURL(IMAGE_TYPES.PNG, 1);
|
|
529
|
+
}
|
|
530
|
+
function cropImageToCanvas(img, opt) {
|
|
531
|
+
const { startX, startY, width, height } = opt;
|
|
532
|
+
const canvas = document.createElement("canvas");
|
|
533
|
+
const ctx = canvas.getContext("2d");
|
|
534
|
+
canvas.width = width;
|
|
535
|
+
canvas.height = height;
|
|
536
|
+
ctx?.drawImage(img, startX, startY, width, height, 0, 0, width, height);
|
|
537
|
+
return canvas;
|
|
538
|
+
}
|
|
539
|
+
function imgToCanvas(img) {
|
|
540
|
+
const canvas = document.createElement("canvas");
|
|
541
|
+
const ctx = canvas.getContext("2d");
|
|
542
|
+
canvas.width = img.width;
|
|
543
|
+
canvas.height = img.height;
|
|
544
|
+
ctx?.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height);
|
|
545
|
+
return canvas;
|
|
546
|
+
}
|
|
547
|
+
function scaleImage(img, opt = { scaleFactor: 1 }) {
|
|
548
|
+
return scaleImageToCanvas(img, opt).toDataURL(IMAGE_TYPES.PNG, 1);
|
|
549
|
+
}
|
|
550
|
+
function scaleImageToCanvas(img, opt = { scaleFactor: 1 }) {
|
|
551
|
+
const { scaleFactor = 1 } = opt;
|
|
552
|
+
const canvas = document.createElement("canvas");
|
|
553
|
+
const ctx = canvas.getContext("2d");
|
|
554
|
+
canvas.width = img.width * scaleFactor;
|
|
555
|
+
canvas.height = img.height * scaleFactor;
|
|
556
|
+
ctx?.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height);
|
|
557
|
+
return canvas;
|
|
558
|
+
}
|
|
559
|
+
async function imgToBase64(img) {
|
|
560
|
+
if (!img || img instanceof Blob) {
|
|
561
|
+
return img;
|
|
562
|
+
}
|
|
563
|
+
let _img = img;
|
|
564
|
+
if (img instanceof Image) {
|
|
565
|
+
_img = img.src;
|
|
566
|
+
} else if (img instanceof HTMLVideoElement) {
|
|
567
|
+
const canvas = document.createElement("canvas");
|
|
568
|
+
const { width, height } = getDomWH(img);
|
|
569
|
+
setDomWH(canvas, width, height);
|
|
570
|
+
const ctx = canvas.getContext("2d");
|
|
571
|
+
ctx && ctx.drawImage(img, 0, 0, width, height, 0, 0, width, height);
|
|
572
|
+
_img = canvas.toDataURL(IMAGE_TYPES.PNG, 1);
|
|
573
|
+
} else if (img instanceof HTMLCanvasElement) {
|
|
574
|
+
_img = img.toDataURL(IMAGE_TYPES.PNG, 1);
|
|
575
|
+
} else if (typeof _img === "string" && !_img.startsWith("data:image/")) {
|
|
576
|
+
_img = await getImgBase64(_img);
|
|
577
|
+
}
|
|
578
|
+
return _img;
|
|
579
|
+
}
|
|
580
|
+
async function imgToBlob(img) {
|
|
581
|
+
if (!img) {
|
|
582
|
+
throw new Error("\u8BF7\u4F20\u5165\u56FE\u7247\u6570\u636E");
|
|
583
|
+
}
|
|
584
|
+
if (img instanceof Blob) {
|
|
585
|
+
return Promise.resolve(img);
|
|
586
|
+
}
|
|
587
|
+
const _img = await imgToBase64(img);
|
|
588
|
+
if (typeof _img !== "string") {
|
|
589
|
+
throw new Error("\u4F20\u5165\u7684\u56FE\u7247\u6570\u636E\u4E0D\u6B63\u786E");
|
|
590
|
+
}
|
|
591
|
+
return dataURItoBlob(_img);
|
|
592
|
+
}
|
|
593
|
+
async function getAllTypeImg(img) {
|
|
594
|
+
const base64 = await imgToBase64(img);
|
|
595
|
+
return {
|
|
596
|
+
base64,
|
|
597
|
+
blob: typeof base64 === "string" && dataURItoBlob(base64)
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
function drawScaleImage(img, scale) {
|
|
601
|
+
return new Promise((resolve) => {
|
|
602
|
+
const w = Math.round(img.width * scale);
|
|
603
|
+
const h = Math.round(img.height * scale);
|
|
604
|
+
const canvas = document.createElement("canvas");
|
|
605
|
+
canvas.width = w;
|
|
606
|
+
canvas.height = h;
|
|
607
|
+
const ctx = canvas.getContext("2d");
|
|
608
|
+
ctx.drawImage(img, 0, 0, w, h);
|
|
609
|
+
canvas.toBlob((blob) => resolve(blob), "image/png");
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
function compressByQuality(blob, img, maxSize) {
|
|
613
|
+
return new Promise((resolve) => {
|
|
614
|
+
const canvas = document.createElement("canvas");
|
|
615
|
+
canvas.width = img.width;
|
|
616
|
+
canvas.height = img.height;
|
|
617
|
+
const ctx = canvas.getContext("2d");
|
|
618
|
+
ctx.drawImage(img, 0, 0);
|
|
619
|
+
let quality = 0.98;
|
|
620
|
+
const minQ = 0.5;
|
|
621
|
+
function loop() {
|
|
622
|
+
canvas.toBlob(
|
|
623
|
+
(newBlob) => {
|
|
624
|
+
if (newBlob.size <= maxSize || quality <= minQ) {
|
|
625
|
+
resolve(newBlob);
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
quality -= 0.02;
|
|
629
|
+
loop();
|
|
630
|
+
},
|
|
631
|
+
blob.type,
|
|
632
|
+
quality
|
|
633
|
+
);
|
|
634
|
+
}
|
|
635
|
+
loop();
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
function smartImageCompress(blob, maxSize) {
|
|
639
|
+
if (blob.size <= maxSize) {
|
|
640
|
+
return Promise.resolve(blob);
|
|
641
|
+
}
|
|
642
|
+
return new Promise((resolve) => {
|
|
643
|
+
const isPng = blob.type === "image/png";
|
|
644
|
+
const img = new Image();
|
|
645
|
+
img.src = URL.createObjectURL(blob);
|
|
646
|
+
img.onload = async () => {
|
|
647
|
+
URL.revokeObjectURL(img.src);
|
|
648
|
+
if (!isPng) {
|
|
649
|
+
const resBlob = await compressByQuality(blob, img, maxSize);
|
|
650
|
+
return resolve(resBlob);
|
|
651
|
+
}
|
|
652
|
+
let scale = 0.95;
|
|
653
|
+
const minScale = 0.5;
|
|
654
|
+
let currentBlob = blob;
|
|
655
|
+
const loopScale = async () => {
|
|
656
|
+
currentBlob = await drawScaleImage(img, scale);
|
|
657
|
+
if (currentBlob.size <= maxSize || scale <= minScale) {
|
|
658
|
+
resolve(currentBlob);
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
scale -= 0.05;
|
|
662
|
+
loopScale();
|
|
663
|
+
};
|
|
664
|
+
loopScale();
|
|
665
|
+
};
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// src/locationUtils.ts
|
|
670
|
+
var DEFAULT_POSITION = {
|
|
671
|
+
lon: 120.168893,
|
|
672
|
+
lat: 30.225404,
|
|
673
|
+
addr: "\u6D59\u6C5F\u7701\u676D\u5DDE\u5E02\u4E0A\u57CE\u533A\u5357\u661F\u8857\u9053\u676D\u5DDE\u897F\u6E56\u98CE\u666F\u540D\u80DC\u533A"
|
|
674
|
+
};
|
|
675
|
+
function getCurrentPosition(config) {
|
|
676
|
+
return new Promise((resolve, reject) => {
|
|
677
|
+
navigator?.geolocation?.getCurrentPosition(
|
|
678
|
+
(position) => {
|
|
679
|
+
resolve(position);
|
|
680
|
+
},
|
|
681
|
+
reject,
|
|
682
|
+
{
|
|
683
|
+
enableHighAccuracy: true,
|
|
684
|
+
timeout: 5e3,
|
|
685
|
+
...config
|
|
686
|
+
}
|
|
687
|
+
);
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
var numALetters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
691
|
+
var nanoidNumALetters = customAlphabet(numALetters, 8);
|
|
692
|
+
var numALettersAUrl = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_~";
|
|
693
|
+
var nanoidUrl = customAlphabet(numALettersAUrl, 20);
|
|
694
|
+
var DEF_FIELD_NAMES = {
|
|
695
|
+
value: "value",
|
|
696
|
+
label: "label",
|
|
697
|
+
children: "children"
|
|
698
|
+
};
|
|
699
|
+
var findOptionByVal = function(val, options, opt = {
|
|
700
|
+
// 是否返回完整的路径数组
|
|
701
|
+
isPathList: false,
|
|
702
|
+
pathList: [],
|
|
703
|
+
labelList: [],
|
|
704
|
+
parent: void 0,
|
|
705
|
+
fieldNames: {
|
|
706
|
+
label: "label",
|
|
707
|
+
value: "value",
|
|
708
|
+
children: "children"
|
|
709
|
+
}
|
|
710
|
+
}) {
|
|
711
|
+
if (!options) {
|
|
712
|
+
console.warn("getDeepData \u8BF7\u4F20\u5165 options");
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
const { fieldNames, isPathList, pathList = [], labelList = [] } = opt || {};
|
|
716
|
+
const { label = "label", value = "value", children = "children" } = fieldNames || {};
|
|
717
|
+
const res = options.find((it) => it[value] === val);
|
|
718
|
+
if (res) {
|
|
719
|
+
if (isPathList) {
|
|
720
|
+
pathList.unshift(res);
|
|
721
|
+
labelList.unshift(res[label]);
|
|
722
|
+
return {
|
|
723
|
+
last: res,
|
|
724
|
+
pathList,
|
|
725
|
+
labelList
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
return res;
|
|
729
|
+
}
|
|
730
|
+
const len = options.length;
|
|
731
|
+
for (let i = 0; i < len; i++) {
|
|
732
|
+
const item = options[i];
|
|
733
|
+
const childList = item[children];
|
|
734
|
+
if (Array.isArray(childList) && childList.length > 0) {
|
|
735
|
+
const res2 = findOptionByVal(val, childList, {
|
|
736
|
+
...opt,
|
|
737
|
+
parent: item,
|
|
738
|
+
pathList,
|
|
739
|
+
labelList
|
|
740
|
+
});
|
|
741
|
+
if (res2) {
|
|
742
|
+
if (isPathList) {
|
|
743
|
+
pathList.unshift(item);
|
|
744
|
+
labelList.unshift(item[label]);
|
|
745
|
+
return {
|
|
746
|
+
last: res2.last,
|
|
747
|
+
labelList,
|
|
748
|
+
pathList
|
|
749
|
+
};
|
|
750
|
+
}
|
|
751
|
+
return res2;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
};
|
|
756
|
+
function normalizeOption(item, fieldNames) {
|
|
757
|
+
const { value = "value", label = "label", children = "children" } = fieldNames || {};
|
|
758
|
+
return {
|
|
759
|
+
...item,
|
|
760
|
+
value: item?.[value],
|
|
761
|
+
label: item?.[label],
|
|
762
|
+
children: item?.[children]
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
var getOptionLabel = function(val, options = [], opt = {}) {
|
|
766
|
+
const { fieldNames = DEF_FIELD_NAMES } = opt || {};
|
|
767
|
+
return findOptionByVal(val, options, opt)?.[fieldNames?.label || DEF_FIELD_NAMES.label];
|
|
768
|
+
};
|
|
769
|
+
function getValLabel(val, options = [], opt = {}) {
|
|
770
|
+
const { showEmpty } = opt;
|
|
771
|
+
if (showEmpty && checkEmpty(val, opt)) {
|
|
772
|
+
return handleEmptyVal(val, opt);
|
|
773
|
+
}
|
|
774
|
+
return getOptionLabel(val, options, opt);
|
|
775
|
+
}
|
|
776
|
+
function getArrayLabels(values, options, opt = {}) {
|
|
777
|
+
if (checkEmpty(values, opt)) {
|
|
778
|
+
return handleEmptyVal(values, opt);
|
|
779
|
+
}
|
|
780
|
+
const { split = "\u3001" } = opt || {};
|
|
781
|
+
let _values = values;
|
|
782
|
+
if (!Array.isArray(_values)) {
|
|
783
|
+
_values = [_values];
|
|
784
|
+
}
|
|
785
|
+
return _values?.map((val) => getOptionLabel(val, options, opt) ?? val)?.filter((it) => (it ?? it) || false)?.join(split);
|
|
786
|
+
}
|
|
787
|
+
var formatOptions = function(options, opt = {}) {
|
|
788
|
+
if (!Array.isArray(options)) {
|
|
789
|
+
console.warn("formatOptions Error: options not Array");
|
|
790
|
+
return options;
|
|
791
|
+
}
|
|
792
|
+
const {
|
|
793
|
+
fieldNames = DEF_FIELD_NAMES,
|
|
794
|
+
sourceFieldNames = DEF_FIELD_NAMES,
|
|
795
|
+
// 是否清除其他参数
|
|
796
|
+
isClear
|
|
797
|
+
} = opt || {};
|
|
798
|
+
const { label, value, children } = fieldNames;
|
|
799
|
+
const { label: sLabel, value: sValue, children: sChildren } = sourceFieldNames;
|
|
800
|
+
const _options = cloneDeep(options);
|
|
801
|
+
const resOptions = [];
|
|
802
|
+
_options.forEach((it) => {
|
|
803
|
+
const _it = isClear ? {} : { ...it };
|
|
804
|
+
_it[value] = it[sValue];
|
|
805
|
+
_it[label] = it[sLabel];
|
|
806
|
+
resOptions.push(_it);
|
|
807
|
+
const childList = it[sChildren];
|
|
808
|
+
if (childList) {
|
|
809
|
+
_it[children] = formatOptions(childList, opt);
|
|
810
|
+
}
|
|
811
|
+
});
|
|
812
|
+
return resOptions;
|
|
813
|
+
};
|
|
814
|
+
var getOptItByVal = function(val, options, opt) {
|
|
815
|
+
const { fieldNames } = opt || {};
|
|
816
|
+
const { value = "value", label = "label", children = "children" } = fieldNames || {};
|
|
817
|
+
for (let i = 0; i < options.length; i++) {
|
|
818
|
+
const it = options[i];
|
|
819
|
+
if (it?.[value] === val) {
|
|
820
|
+
return {
|
|
821
|
+
...it,
|
|
822
|
+
value: it?.[value],
|
|
823
|
+
label: it?.[label],
|
|
824
|
+
children: it?.[children]
|
|
825
|
+
};
|
|
826
|
+
}
|
|
827
|
+
const _children = it?.[children];
|
|
828
|
+
if (Array.isArray(_children) && _children.length > 0) {
|
|
829
|
+
const child = getOptItByVal(val, _children, opt);
|
|
830
|
+
if (child) {
|
|
831
|
+
return {
|
|
832
|
+
...child,
|
|
833
|
+
value: child?.[value],
|
|
834
|
+
label: child?.[label],
|
|
835
|
+
children: child?.[children]
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
|
|
842
|
+
// src/file/fileName.ts
|
|
843
|
+
function getFileExt(fileUrl) {
|
|
844
|
+
if (typeof fileUrl !== "string") {
|
|
845
|
+
return;
|
|
846
|
+
}
|
|
847
|
+
const res = fileUrl?.match(/([^\/]+?)\.([^\/\.]+)$/);
|
|
848
|
+
if (res && res.length > 2) {
|
|
849
|
+
return res[2];
|
|
850
|
+
}
|
|
851
|
+
return fileUrl;
|
|
852
|
+
}
|
|
853
|
+
function getFileName(fileUrl) {
|
|
854
|
+
if (typeof fileUrl !== "string") {
|
|
855
|
+
return;
|
|
856
|
+
}
|
|
857
|
+
const res = fileUrl?.match(/([^\/]+?)\.[^\/\.]+$/);
|
|
858
|
+
if (res && res.length > 0) {
|
|
859
|
+
return res?.[1] || res?.[0];
|
|
860
|
+
}
|
|
861
|
+
return fileUrl;
|
|
862
|
+
}
|
|
863
|
+
function getFullFileName(fileUrl) {
|
|
864
|
+
if (typeof fileUrl !== "string") {
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
867
|
+
const res = fileUrl?.match(/[^\/]+?\.[^\/\.]+$/);
|
|
868
|
+
if (res && res.length > 0) {
|
|
869
|
+
return res[0];
|
|
870
|
+
}
|
|
871
|
+
return fileUrl;
|
|
872
|
+
}
|
|
873
|
+
var mergeFileName = function(fileName, str = "") {
|
|
874
|
+
if (typeof fileName !== "string") {
|
|
875
|
+
return "";
|
|
876
|
+
}
|
|
877
|
+
const name = fileName?.match(/([^\/]+?)\.[^\/]+$/)?.[1] || "";
|
|
878
|
+
const suffix = fileName?.match(/[^\/]+?\.([^\/]+)$/)?.[1] || "";
|
|
879
|
+
return `${name}${str ?? ""}.${suffix}`;
|
|
880
|
+
};
|
|
881
|
+
var getUFileName = function(fileName) {
|
|
882
|
+
return mergeFileName(getFileName(fileName) || "up", `-~kid-${nanoidNumALetters()}~`);
|
|
883
|
+
};
|
|
884
|
+
var getFileNameExt = function(fileName) {
|
|
885
|
+
const regex = /^(.*)\.([^.]+)$/;
|
|
886
|
+
const result = fileName.match(regex);
|
|
887
|
+
if (result) {
|
|
888
|
+
const [, mainPart, ext] = result;
|
|
889
|
+
return {
|
|
890
|
+
mainPart,
|
|
891
|
+
ext
|
|
892
|
+
};
|
|
893
|
+
}
|
|
894
|
+
return {
|
|
895
|
+
mainPart: fileName,
|
|
896
|
+
ext: ""
|
|
897
|
+
};
|
|
898
|
+
};
|
|
899
|
+
|
|
900
|
+
// src/file/fileType.ts
|
|
901
|
+
var TYPE_VIDEO = "video";
|
|
902
|
+
var TYPE_IMG = "image";
|
|
903
|
+
var TYPE_AUDIO = "audio";
|
|
904
|
+
function checkUrlSuffix(url, types = [], caseSensitive = false) {
|
|
905
|
+
if (!url) {
|
|
906
|
+
return false;
|
|
907
|
+
}
|
|
908
|
+
const _url = url?.replace(/\?.+/, "");
|
|
909
|
+
const reg = new RegExp(`.(${types.join("|")})$`, caseSensitive ? void 0 : "i");
|
|
910
|
+
if (reg.test(_url)) {
|
|
911
|
+
return true;
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
function getFileType(file) {
|
|
915
|
+
if (typeof file === "object" && file?.type) {
|
|
916
|
+
return file?.type;
|
|
917
|
+
}
|
|
918
|
+
if (typeof file === "string") {
|
|
919
|
+
let type = null;
|
|
920
|
+
if (checkVideoUrl(file)) {
|
|
921
|
+
type = "video/mp4";
|
|
922
|
+
} else if (checkImageUrl(file)) {
|
|
923
|
+
type = "image/jpeg";
|
|
924
|
+
} else if (checkAudioUrl(file)) {
|
|
925
|
+
type = "audio/3gpp";
|
|
926
|
+
} else if (checkUrlSuffix(file, ["pdf"])) {
|
|
927
|
+
type = "application/pdf";
|
|
928
|
+
} else if (checkUrlSuffix(file, ["xlsx", "xls", "csv", "xlsm", "xlsb"])) {
|
|
929
|
+
type = "application/vnd.ms-excel";
|
|
930
|
+
} else if (checkUrlSuffix(file, ["doc", "docx"])) {
|
|
931
|
+
type = "application/msword";
|
|
932
|
+
} else if (checkUrlSuffix(file, ["ppt", "pptx"])) {
|
|
933
|
+
type = "application/vnd.ms-powerpoint";
|
|
934
|
+
}
|
|
935
|
+
return type;
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
function checkImageUrl(url) {
|
|
939
|
+
if (/^data:image\/\w+;base64/i.test(url)) {
|
|
940
|
+
return true;
|
|
941
|
+
}
|
|
942
|
+
const imgTypes = [
|
|
943
|
+
"apng",
|
|
944
|
+
"avif",
|
|
945
|
+
"bmp",
|
|
946
|
+
"gif",
|
|
947
|
+
"ico",
|
|
948
|
+
"cur",
|
|
949
|
+
"jpg",
|
|
950
|
+
"jpeg",
|
|
951
|
+
"jfif",
|
|
952
|
+
"pjpeg",
|
|
953
|
+
"pjp",
|
|
954
|
+
"png",
|
|
955
|
+
"svg",
|
|
956
|
+
"tif",
|
|
957
|
+
"tiff",
|
|
958
|
+
"webp"
|
|
959
|
+
];
|
|
960
|
+
return checkUrlSuffix(url, imgTypes);
|
|
961
|
+
}
|
|
962
|
+
function isValidMediaType(input) {
|
|
963
|
+
const validPrefixes = ["image/", "video/", "audio/"];
|
|
964
|
+
return validPrefixes.some((prefix) => input?.startsWith(prefix));
|
|
965
|
+
}
|
|
966
|
+
function checkVideoUrl(url) {
|
|
967
|
+
const imgTypes = ["3gp", "mpg", "mpeg", "mp4", "m4v", "m4p", "ogv", "ogg", "mov", "webm"];
|
|
968
|
+
return checkUrlSuffix(url, imgTypes);
|
|
969
|
+
}
|
|
970
|
+
function checkAudioUrl(url) {
|
|
971
|
+
const imgTypes = ["3gp", "adts", "mpeg", "mp3", "mp4", "ogg", "mov", "webm", "rtp", "amr", "wav"];
|
|
972
|
+
return checkUrlSuffix(url, imgTypes);
|
|
973
|
+
}
|
|
974
|
+
function getFileTypeStr(file) {
|
|
975
|
+
const { type, url = file?.ossUrl } = file || {};
|
|
976
|
+
let fileType = "";
|
|
977
|
+
if (url) {
|
|
978
|
+
if (url.startsWith("data:image/") || checkImageUrl(url)) {
|
|
979
|
+
fileType = TYPE_IMG;
|
|
980
|
+
} else if (checkVideoUrl(url)) {
|
|
981
|
+
fileType = TYPE_VIDEO;
|
|
982
|
+
} else if (checkAudioUrl(url)) {
|
|
983
|
+
fileType = TYPE_AUDIO;
|
|
984
|
+
}
|
|
985
|
+
} else if (type) {
|
|
986
|
+
if (type?.startsWith("image/")) {
|
|
987
|
+
fileType = TYPE_IMG;
|
|
988
|
+
}
|
|
989
|
+
if (type?.startsWith("video/")) {
|
|
990
|
+
fileType = TYPE_VIDEO;
|
|
991
|
+
}
|
|
992
|
+
if (type?.startsWith("audio/")) {
|
|
993
|
+
fileType = TYPE_AUDIO;
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
return fileType || type;
|
|
997
|
+
}
|
|
998
|
+
var getFileTypeExt = function(mimeType) {
|
|
999
|
+
const mimeToExtMap = {
|
|
1000
|
+
// 图片类型
|
|
1001
|
+
"image/jpeg": "jpg",
|
|
1002
|
+
"image/jpg": "jpg",
|
|
1003
|
+
"image/png": "png",
|
|
1004
|
+
"image/gif": "gif",
|
|
1005
|
+
"image/webp": "webp",
|
|
1006
|
+
"image/svg+xml": "svg",
|
|
1007
|
+
"image/bmp": "bmp",
|
|
1008
|
+
"image/tiff": "tiff",
|
|
1009
|
+
// 文档类型
|
|
1010
|
+
"application/pdf": "pdf",
|
|
1011
|
+
"application/msword": "doc",
|
|
1012
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
|
|
1013
|
+
"application/vnd.ms-excel": "xls",
|
|
1014
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
|
|
1015
|
+
"application/vnd.ms-powerpoint": "ppt",
|
|
1016
|
+
"application/vnd.openxmlformats-officedocument.presentationml.presentation": "pptx",
|
|
1017
|
+
"text/plain": "txt",
|
|
1018
|
+
"text/html": "html",
|
|
1019
|
+
"application/json": "json",
|
|
1020
|
+
// 音频/视频类型
|
|
1021
|
+
"audio/mpeg": "mp3",
|
|
1022
|
+
"audio/wav": "wav",
|
|
1023
|
+
"video/mp4": "mp4",
|
|
1024
|
+
"video/avi": "avi",
|
|
1025
|
+
"video/mpeg": "mpeg",
|
|
1026
|
+
// 压缩包类型
|
|
1027
|
+
"application/zip": "zip",
|
|
1028
|
+
"application/x-rar-compressed": "rar",
|
|
1029
|
+
"application/x-7z-compressed": "7z"
|
|
1030
|
+
};
|
|
1031
|
+
if (!mimeType || typeof mimeType !== "string") {
|
|
1032
|
+
return "";
|
|
1033
|
+
}
|
|
1034
|
+
const normalizedMimeType = mimeType.trim().toLowerCase();
|
|
1035
|
+
if (mimeToExtMap[normalizedMimeType]) {
|
|
1036
|
+
return mimeToExtMap[normalizedMimeType];
|
|
1037
|
+
}
|
|
1038
|
+
const mainType = normalizedMimeType.split("/")[0];
|
|
1039
|
+
switch (mainType) {
|
|
1040
|
+
case "image":
|
|
1041
|
+
return "img";
|
|
1042
|
+
case "audio":
|
|
1043
|
+
return "audio";
|
|
1044
|
+
case "video":
|
|
1045
|
+
return "video";
|
|
1046
|
+
default:
|
|
1047
|
+
return mainType?.match(/[^\/]*\/([^\/]+)$/)?.[1] || mimeType;
|
|
1048
|
+
}
|
|
1049
|
+
};
|
|
1050
|
+
|
|
1051
|
+
// src/url.ts
|
|
1052
|
+
function replaceSearch(url, search) {
|
|
1053
|
+
if (!url || typeof url !== "string") {
|
|
1054
|
+
console.warn("Warn replaceSearch: \u8BF7\u4F20\u5165\u6B63\u786E\u7684 url \u5B57\u7B26\u4E32");
|
|
1055
|
+
return url;
|
|
1056
|
+
}
|
|
1057
|
+
const _url = fixUrlQuery(url);
|
|
1058
|
+
const hashIdx = _url.indexOf("#");
|
|
1059
|
+
const queryIdx = _url.indexOf("?");
|
|
1060
|
+
let hash = "";
|
|
1061
|
+
let resUrl = _url;
|
|
1062
|
+
if (queryIdx >= 0) {
|
|
1063
|
+
resUrl = _url.slice(0, queryIdx);
|
|
1064
|
+
} else if (hashIdx >= 0) {
|
|
1065
|
+
resUrl = _url.slice(0, hashIdx);
|
|
1066
|
+
}
|
|
1067
|
+
if (hashIdx >= 0) {
|
|
1068
|
+
hash = _url.slice(hashIdx);
|
|
1069
|
+
}
|
|
1070
|
+
resUrl += search?.indexOf("?") >= 0 ? "" : "?";
|
|
1071
|
+
resUrl += search;
|
|
1072
|
+
resUrl += hash;
|
|
1073
|
+
return resUrl;
|
|
1074
|
+
}
|
|
1075
|
+
function objToSearch(query, opt) {
|
|
1076
|
+
if (!query || typeof query !== "object") {
|
|
1077
|
+
console.warn("Warn objToSearch: \u8BF7\u4F20\u5165\u6B63\u786E\u7684\u5BF9\u8C61");
|
|
1078
|
+
return;
|
|
1079
|
+
}
|
|
1080
|
+
let hasQuery = opt?.hasQuery;
|
|
1081
|
+
if (typeof opt === "boolean") {
|
|
1082
|
+
hasQuery = opt;
|
|
1083
|
+
}
|
|
1084
|
+
const { isEncode } = opt || {};
|
|
1085
|
+
let search = hasQuery ? "?" : "";
|
|
1086
|
+
Object.keys(query).forEach((key) => {
|
|
1087
|
+
if (search && search != "?") {
|
|
1088
|
+
search += "&";
|
|
1089
|
+
}
|
|
1090
|
+
const _key = isEncode ? encodeURIComponent(key) : key;
|
|
1091
|
+
search += _key;
|
|
1092
|
+
search += "=";
|
|
1093
|
+
const val = isEncode ? encodeURIComponent(query[key]) : query[key];
|
|
1094
|
+
search += val;
|
|
1095
|
+
});
|
|
1096
|
+
return search;
|
|
1097
|
+
}
|
|
1098
|
+
function fixUrlQuery(url) {
|
|
1099
|
+
if (!url || typeof url !== "string") {
|
|
1100
|
+
console.warn("Warn handleUrlQuery: \u8BF7\u4F20\u5165\u6B63\u786E\u7684 url \u5B57\u7B26\u4E32");
|
|
1101
|
+
return url;
|
|
1102
|
+
}
|
|
1103
|
+
const hashIdx = url.indexOf("#");
|
|
1104
|
+
const queryIdx = url.indexOf("?");
|
|
1105
|
+
if (queryIdx < hashIdx) {
|
|
1106
|
+
let hash = url.slice(hashIdx);
|
|
1107
|
+
const hashQueryIdx = hash.indexOf("?");
|
|
1108
|
+
if (hashQueryIdx >= 0) {
|
|
1109
|
+
const _url = url.slice(0, queryIdx);
|
|
1110
|
+
const urlQuery = url.slice(queryIdx, hashIdx)?.replace(/\/$/, "");
|
|
1111
|
+
const hashQuery = hash.slice(hashQueryIdx)?.replace("?", "")?.replace(/\/$/, "");
|
|
1112
|
+
hash = hash.slice(0, hashQueryIdx);
|
|
1113
|
+
return `${_url}${urlQuery}&${hashQuery}${hash}`;
|
|
1114
|
+
}
|
|
1115
|
+
return url;
|
|
1116
|
+
}
|
|
1117
|
+
if (queryIdx >= 0 && hashIdx >= 0 && queryIdx > hashIdx) {
|
|
1118
|
+
const hash = url.slice(hashIdx, queryIdx);
|
|
1119
|
+
const search = url.slice(queryIdx);
|
|
1120
|
+
return `${url.slice(0, hashIdx)}${search}${hash}`;
|
|
1121
|
+
}
|
|
1122
|
+
return url;
|
|
1123
|
+
}
|
|
1124
|
+
function searchToObj(url) {
|
|
1125
|
+
if (!url || typeof url !== "string") {
|
|
1126
|
+
console.warn("Warn getSearchParams: \u8BF7\u4F20\u5165\u6B63\u786E\u7684 url \u5B57\u7B26\u4E32.");
|
|
1127
|
+
return;
|
|
1128
|
+
}
|
|
1129
|
+
let queryBody;
|
|
1130
|
+
if (url.includes("?")) {
|
|
1131
|
+
const _url = fixUrlQuery(url).replace("/#", "#");
|
|
1132
|
+
const queryIdx = _url.indexOf("?");
|
|
1133
|
+
const hashIdx = _url.indexOf("#");
|
|
1134
|
+
if (queryIdx < 0) {
|
|
1135
|
+
return;
|
|
1136
|
+
}
|
|
1137
|
+
queryBody = hashIdx >= 0 ? _url.slice(queryIdx + 1, hashIdx) : _url.slice(queryIdx + 1);
|
|
1138
|
+
} else {
|
|
1139
|
+
const bare = url.replace(/^[?&]+/, "").split("#")[0];
|
|
1140
|
+
if (!bare || !bare.includes("=") || url.includes("://")) {
|
|
1141
|
+
return;
|
|
1142
|
+
}
|
|
1143
|
+
queryBody = bare;
|
|
1144
|
+
}
|
|
1145
|
+
if (!queryBody) {
|
|
1146
|
+
return;
|
|
1147
|
+
}
|
|
1148
|
+
const res = {};
|
|
1149
|
+
queryBody.split("&")?.forEach((it) => {
|
|
1150
|
+
if (!it) {
|
|
1151
|
+
return;
|
|
1152
|
+
}
|
|
1153
|
+
const eqIdx = it.indexOf("=");
|
|
1154
|
+
const rawKey = eqIdx >= 0 ? it.slice(0, eqIdx) : it;
|
|
1155
|
+
const rawVal = eqIdx >= 0 ? it.slice(eqIdx + 1) : "";
|
|
1156
|
+
try {
|
|
1157
|
+
res[decodeURIComponent(rawKey)] = decodeURIComponent(rawVal);
|
|
1158
|
+
} catch {
|
|
1159
|
+
res[rawKey] = rawVal;
|
|
1160
|
+
}
|
|
1161
|
+
});
|
|
1162
|
+
return res;
|
|
1163
|
+
}
|
|
1164
|
+
function addQuery(url, params, opt = {}) {
|
|
1165
|
+
const newObj = { ...searchToObj(url), ...params };
|
|
1166
|
+
return replaceSearch(url, objToSearch(newObj, { hasQuery: !!params, isEncode: opt?.isEncode }));
|
|
1167
|
+
}
|
|
1168
|
+
function addSearch(url, params, opt = {}) {
|
|
1169
|
+
const newObj = { ...searchToObj(url), ...params };
|
|
1170
|
+
return objToSearch(newObj, { hasQuery: !!params, isEncode: opt?.isEncode });
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
// src/upload/uploadUtils.ts
|
|
1174
|
+
var formatDirStr = function(dir) {
|
|
1175
|
+
return dir?.replace(/^\/(.*)\/*$/, "$1").replace(/(.?=*)\/*$/, "$1/");
|
|
1176
|
+
};
|
|
1177
|
+
|
|
1178
|
+
// src/upload/OssUploadUtil.ts
|
|
1179
|
+
var isPublicMap = {
|
|
1180
|
+
0: "&",
|
|
1181
|
+
1: "?"
|
|
1182
|
+
};
|
|
1183
|
+
function getSignature(opt = {}) {
|
|
1184
|
+
const { serverUrl = getGlobalServeConfig()?.serverUrl || "/api/v1/user/oss/getWebOssConfig" } = opt;
|
|
1185
|
+
if (window._$ossSignatureRes && serverUrl === window._$ossSignatureRes.serverUrl && dayjs2().valueOf() - window._$ossSignatureRes.__saveTime < window._$ossSignatureRes.expireTimeMilles - 1e4) {
|
|
1186
|
+
return Promise.resolve(window._$ossSignatureRes);
|
|
1187
|
+
}
|
|
1188
|
+
const { axios: _ax = axios, params = {}, axiosConf } = opt;
|
|
1189
|
+
params.dir = formatDirStr(params.dir);
|
|
1190
|
+
return _ax.get(serverUrl, {
|
|
1191
|
+
...axiosConf,
|
|
1192
|
+
params: {
|
|
1193
|
+
isPublic: 1,
|
|
1194
|
+
...params
|
|
1195
|
+
}
|
|
1196
|
+
}).then((res) => {
|
|
1197
|
+
window._$ossSignatureRes = res?.data?.data ?? res?.data ?? res;
|
|
1198
|
+
if (window._$ossSignatureRes) {
|
|
1199
|
+
window._$ossSignatureRes.__saveTime = dayjs2().valueOf();
|
|
1200
|
+
window._$ossSignatureRes.serverUrl = serverUrl;
|
|
1201
|
+
}
|
|
1202
|
+
return window._$ossSignatureRes;
|
|
1203
|
+
});
|
|
1204
|
+
}
|
|
1205
|
+
var OssUploadUtil = class {
|
|
1206
|
+
/** axios 实例 */
|
|
1207
|
+
axios;
|
|
1208
|
+
/** axios 配置参数 */
|
|
1209
|
+
axiosConf;
|
|
1210
|
+
/** 获取配置的接口地址 */
|
|
1211
|
+
serverUrl;
|
|
1212
|
+
/** 获取配置的接口自定义入参 */
|
|
1213
|
+
signatureParams;
|
|
1214
|
+
constructor(props = {}) {
|
|
1215
|
+
this.axios = props.axios || axios;
|
|
1216
|
+
this.axiosConf = props.axiosConf || {};
|
|
1217
|
+
this.serverUrl = props.serverUrl || getGlobalServeConfig()?.serverUrl || "/api/v1/user/oss/getWebOssConfig";
|
|
1218
|
+
this.signatureParams = props.signatureParams || {};
|
|
1219
|
+
}
|
|
1220
|
+
getSignature(serverUrl = this.serverUrl, opt) {
|
|
1221
|
+
return this._getSignature(serverUrl, opt);
|
|
1222
|
+
}
|
|
1223
|
+
upload(file, opt = {}) {
|
|
1224
|
+
return this._upload(file, { ...opt });
|
|
1225
|
+
}
|
|
1226
|
+
_getSignature(serverUrl = this.serverUrl, opt) {
|
|
1227
|
+
return getSignature({
|
|
1228
|
+
...opt,
|
|
1229
|
+
serverUrl,
|
|
1230
|
+
axios: opt?.axios || this.axios,
|
|
1231
|
+
axiosConf: { ...this.axiosConf, ...opt?.axiosConf }
|
|
1232
|
+
});
|
|
1233
|
+
}
|
|
1234
|
+
_upload(file, opt = {}) {
|
|
1235
|
+
return new Promise(async (resolve, reject) => {
|
|
1236
|
+
const ossParams = await this._getSignature(opt.serverUrl || this.serverUrl, {
|
|
1237
|
+
...opt,
|
|
1238
|
+
params: { ...this.signatureParams, ...opt.params }
|
|
1239
|
+
});
|
|
1240
|
+
const { ossParams: propOssParams, fileName } = opt || {};
|
|
1241
|
+
const formData = new FormData();
|
|
1242
|
+
const filename = fileName || file.name;
|
|
1243
|
+
const key = `${ossParams?.dir}${filename}`;
|
|
1244
|
+
formData.set("key", key);
|
|
1245
|
+
formData.set("OSSAccessKeyId", ossParams.accessid);
|
|
1246
|
+
formData.set("policy", ossParams.policy);
|
|
1247
|
+
formData.set("Signature", ossParams.signature);
|
|
1248
|
+
if (ossParams.callback) {
|
|
1249
|
+
formData.set("callback", ossParams.callback);
|
|
1250
|
+
}
|
|
1251
|
+
formData.set("success_action_status", 200);
|
|
1252
|
+
formData.set("file", file);
|
|
1253
|
+
if (propOssParams) {
|
|
1254
|
+
for (const key2 in propOssParams) {
|
|
1255
|
+
if (Object.hasOwnProperty.call(propOssParams, key2)) {
|
|
1256
|
+
formData.set(key2, propOssParams[key2]);
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
const _axios = opt?.axios || this.axios;
|
|
1261
|
+
return _axios.post(ossParams.host, formData, { ...this.axiosConf, ...opt?.axiosConf }).then((res) => {
|
|
1262
|
+
if (getGlobalServeConfig()?.showThumbnail) {
|
|
1263
|
+
res.data.data.thumbnailUrl = `${res?.data?.data?.fileUrl}${opt?.thumbnailParams ? isPublicMap[opt?.signatureParams?.isPublic] + opt?.thumbnailParams : ""}`;
|
|
1264
|
+
}
|
|
1265
|
+
resolve(res);
|
|
1266
|
+
return res;
|
|
1267
|
+
}).catch((err) => {
|
|
1268
|
+
console.error("oss upload err", err);
|
|
1269
|
+
reject(err);
|
|
1270
|
+
return Promise.reject(err);
|
|
1271
|
+
});
|
|
1272
|
+
});
|
|
1273
|
+
}
|
|
1274
|
+
};
|
|
1275
|
+
var OssUploadUtil_default = OssUploadUtil;
|
|
1276
|
+
|
|
1277
|
+
// src/scan-qr.ts
|
|
1278
|
+
function objToBlob(obj, charset = "utf-8") {
|
|
1279
|
+
const jsonStr = JSON.stringify(obj);
|
|
1280
|
+
const blob = new Blob([jsonStr], {
|
|
1281
|
+
type: `application/json;charset=${charset}`
|
|
1282
|
+
});
|
|
1283
|
+
return blob;
|
|
1284
|
+
}
|
|
1285
|
+
var getCodeInfo = async (id, opt) => {
|
|
1286
|
+
const { env, baseUrl } = opt;
|
|
1287
|
+
try {
|
|
1288
|
+
const url = ` ${baseUrl || "https://open-oss.abt.hzabjt.com"}/qr-code/${env}/${String(id)}`;
|
|
1289
|
+
const response = await fetch(url);
|
|
1290
|
+
const data = await response.json();
|
|
1291
|
+
return data;
|
|
1292
|
+
} catch (error) {
|
|
1293
|
+
throw error;
|
|
1294
|
+
}
|
|
1295
|
+
};
|
|
1296
|
+
var setCodeInfo = async (value, opt) => {
|
|
1297
|
+
const { env } = opt;
|
|
1298
|
+
try {
|
|
1299
|
+
const id = nanoidUrl();
|
|
1300
|
+
const file = objToBlob(value);
|
|
1301
|
+
const ossUploader = new OssUploadUtil_default({ signatureParams: { withoutPrefix: true } });
|
|
1302
|
+
const uploadResult = await ossUploader.upload(file, {
|
|
1303
|
+
fileName: id,
|
|
1304
|
+
params: {
|
|
1305
|
+
dir: `/qr-code/${env}`
|
|
1306
|
+
}
|
|
1307
|
+
});
|
|
1308
|
+
const fileUrl = uploadResult?.data?.data?.fileUrl;
|
|
1309
|
+
return {
|
|
1310
|
+
fileUrl,
|
|
1311
|
+
id
|
|
1312
|
+
};
|
|
1313
|
+
} catch (error) {
|
|
1314
|
+
throw error;
|
|
1315
|
+
}
|
|
1316
|
+
};
|
|
1317
|
+
|
|
1318
|
+
// src/sizeAdapter.ts
|
|
1319
|
+
function getClientStyle() {
|
|
1320
|
+
return {
|
|
1321
|
+
width: document.documentElement.clientWidth || document.body.clientWidth,
|
|
1322
|
+
height: document.documentElement.clientHeight || document.body.clientHeight
|
|
1323
|
+
};
|
|
1324
|
+
}
|
|
1325
|
+
var clientStyle = getClientStyle();
|
|
1326
|
+
window.addEventListener("resize", () => {
|
|
1327
|
+
const { width, height } = getClientStyle();
|
|
1328
|
+
clientStyle.width = width;
|
|
1329
|
+
clientStyle.height = height;
|
|
1330
|
+
});
|
|
1331
|
+
function getViewportPX() {
|
|
1332
|
+
const { width, height } = clientStyle;
|
|
1333
|
+
return {
|
|
1334
|
+
wPx: width / 100,
|
|
1335
|
+
hPx: height / 100
|
|
1336
|
+
};
|
|
1337
|
+
}
|
|
1338
|
+
var DEFAULT_SIZE = {
|
|
1339
|
+
width: 750,
|
|
1340
|
+
height: 1334
|
|
1341
|
+
};
|
|
1342
|
+
function setDefaultSize(width, height) {
|
|
1343
|
+
DEFAULT_SIZE.width = width || 750;
|
|
1344
|
+
DEFAULT_SIZE.height = height || 1334;
|
|
1345
|
+
}
|
|
1346
|
+
function getClientRate(current, defaultVal) {
|
|
1347
|
+
return parseFloat(current) / defaultVal;
|
|
1348
|
+
}
|
|
1349
|
+
function getVW(pxNum = 0) {
|
|
1350
|
+
return `${getClientRate(pxNum, DEFAULT_SIZE.width) * 100}vw`;
|
|
1351
|
+
}
|
|
1352
|
+
function getVH(pxNum = 0) {
|
|
1353
|
+
return `${getClientRate(pxNum, DEFAULT_SIZE.height) * 100}vh`;
|
|
1354
|
+
}
|
|
1355
|
+
function getWpx(pxNum = 0) {
|
|
1356
|
+
return getClientRate(pxNum, DEFAULT_SIZE.width) * clientStyle.width;
|
|
1357
|
+
}
|
|
1358
|
+
function getHpx(pxNum = 0) {
|
|
1359
|
+
return getClientRate(pxNum, DEFAULT_SIZE.height) * clientStyle.height;
|
|
1360
|
+
}
|
|
1361
|
+
function getHPxByVW(vw = 0) {
|
|
1362
|
+
let _vw = vw;
|
|
1363
|
+
if (typeof _vw === "string") {
|
|
1364
|
+
_vw = parseFloat(_vw);
|
|
1365
|
+
}
|
|
1366
|
+
return clientStyle.width * (_vw / 100);
|
|
1367
|
+
}
|
|
1368
|
+
function getWPxByVH(vh = 0) {
|
|
1369
|
+
let _vh = vh;
|
|
1370
|
+
if (typeof _vh === "string") {
|
|
1371
|
+
_vh = parseFloat(_vh);
|
|
1372
|
+
}
|
|
1373
|
+
return clientStyle.height * (_vh / 100);
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
// src/string.ts
|
|
1377
|
+
var convertToCamelCase = function(s) {
|
|
1378
|
+
return s.replace(/-(\w)/g, function(all, letter) {
|
|
1379
|
+
return letter.toUpperCase();
|
|
1380
|
+
});
|
|
1381
|
+
};
|
|
1382
|
+
var objStrToObj = function(objStr) {
|
|
1383
|
+
let _objStr = objStr;
|
|
1384
|
+
if (typeof _objStr === "object") {
|
|
1385
|
+
return _objStr;
|
|
1386
|
+
}
|
|
1387
|
+
if (typeof _objStr === "string") {
|
|
1388
|
+
_objStr = _objStr.trim();
|
|
1389
|
+
if (/^\[.*\]$|^\{.*\}$/.test(_objStr)) {
|
|
1390
|
+
try {
|
|
1391
|
+
_objStr = JSON.parse(_objStr);
|
|
1392
|
+
} catch (error) {
|
|
1393
|
+
}
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
return _objStr;
|
|
1397
|
+
};
|
|
1398
|
+
|
|
1399
|
+
// src/file/file.ts
|
|
1400
|
+
function getFileURL(file) {
|
|
1401
|
+
const _file = file?.file || file;
|
|
1402
|
+
if (!(_file instanceof File)) {
|
|
1403
|
+
return;
|
|
1404
|
+
}
|
|
1405
|
+
const url = _file.url || null;
|
|
1406
|
+
try {
|
|
1407
|
+
const win = window;
|
|
1408
|
+
if (win.createObjectURL != void 0) {
|
|
1409
|
+
return win.createObjectURL(_file);
|
|
1410
|
+
} else if (window.URL != void 0) {
|
|
1411
|
+
return window.URL.createObjectURL(_file);
|
|
1412
|
+
} else if (window.webkitURL != void 0) {
|
|
1413
|
+
return window.webkitURL.createObjectURL(_file);
|
|
1414
|
+
}
|
|
1415
|
+
} catch (error) {
|
|
1416
|
+
console.warn("getFileURL Error: ", error);
|
|
1417
|
+
}
|
|
1418
|
+
return url;
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
export { DEFAULT_POSITION, DEFAULT_SIZE, DEF_FIELD_NAMES, IMAGE_TYPES, TYPE_AUDIO, TYPE_IMG, TYPE_VIDEO, addQuery, addSearch, adjustColor, adjustTextColor, calculateAge, calculateCheckCode, checkAudioUrl, checkEmpty, checkEmptyImg, checkImageUrl, checkUrlSuffix, checkVideoUrl, clientStyle, compressByQuality, contrast, contrastRatio, convert15to18, convertToCamelCase, cropImage, cropImageToCanvas, dataURItoBlob, drawScaleImage, findOptionByVal, fixUrlQuery, formatOptions, getAllTypeImg, getArr, getArrayLabels, getCanvas, getCanvasByImg, getCanvasPic, getClientRate, getClientStyle, getCodeInfo, getCurrentPosition, getDaysInMonth, getDomWH, getFileExt, getFileName, getFileNameExt, getFileType, getFileTypeExt, getFileTypeFromBase64, getFileTypeFromBlob, getFileTypeStr, getFileURL, getFullFileName, getGlobalServeConfig, getHPxByVW, getHpx, getIdCardInfo, getImgBase64, getImgEle, getOptItByVal, getOptionLabel, getTextColor, getUFileName, getVH, getVW, getValLabel, getViewportPX, getWPxByVH, getWpx, handleEmptyVal, hexToRGB, hslToRgb, imgToBase64, imgToBlob, imgToCanvas, isBase64Str, isGray, isValidBirthDate, isValidIdCard, isValidMediaType, luminance, mergeFileName, nanoidNumALetters, nanoidUrl, normalizeOption, numALetters, numALettersAUrl, objStrToObj, objToSearch, replaceSearch, rgbNumLimit, rgbToHex, rgbToHsl, scaleImage, scaleImageToCanvas, searchToObj, setCodeInfo, setDefaultSize, setDomWH, setGlobalServeConfig, setGlobalServeConfigs, smartImageCompress, verifyCheckCode };
|
|
1422
|
+
//# sourceMappingURL=index.js.map
|
|
1423
|
+
//# sourceMappingURL=index.js.map
|