@oeos-components/utils 0.0.2 → 0.0.3
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/dist/index.cjs +784 -12
- package/dist/index.d.cts +309 -1
- package/dist/index.d.mts +309 -1
- package/dist/index.d.ts +309 -1
- package/dist/index.mjs +748 -70
- package/package.json +3 -2
package/dist/index.cjs
CHANGED
|
@@ -1,16 +1,788 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
const reactivity = require('@vue/reactivity');
|
|
4
|
+
const lodashEs = require('lodash-es');
|
|
5
|
+
const consola = require('consola');
|
|
6
|
+
const elementPlus = require('element-plus');
|
|
7
|
+
|
|
8
|
+
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
9
|
+
const isString = (val) => typeof val === "string";
|
|
10
|
+
const isStringNumber = (val) => {
|
|
11
|
+
if (!isString(val)) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
return !Number.isNaN(Number(val));
|
|
15
|
+
};
|
|
16
|
+
const isNumber = (val) => typeof val === "number";
|
|
17
|
+
function $toast(message, type = "success", otherParams = {}) {
|
|
18
|
+
const map = {
|
|
19
|
+
s: "success",
|
|
20
|
+
i: "info",
|
|
21
|
+
e: "error",
|
|
22
|
+
w: "warning"
|
|
23
|
+
};
|
|
24
|
+
if (getType(message) === "object") {
|
|
25
|
+
if (message.clodeAll) {
|
|
26
|
+
elementPlus.ElMessage.closeAll();
|
|
27
|
+
}
|
|
28
|
+
elementPlus.ElMessage(message);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (getType(type) === "object") {
|
|
32
|
+
if (type.clodeAll) {
|
|
33
|
+
elementPlus.ElMessage.closeAll();
|
|
34
|
+
}
|
|
35
|
+
elementPlus.ElMessage({
|
|
36
|
+
message,
|
|
37
|
+
type: "success",
|
|
38
|
+
...type
|
|
39
|
+
});
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (otherParams.closeAll) {
|
|
43
|
+
elementPlus.ElMessage.closeAll();
|
|
44
|
+
}
|
|
45
|
+
elementPlus.ElMessage({
|
|
46
|
+
message,
|
|
47
|
+
type: map[type] || type,
|
|
48
|
+
...otherParams
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
$toast.success = (message, otherParams = {}) => $toast(message, "success", otherParams);
|
|
52
|
+
$toast.info = (message, otherParams = {}) => $toast(message, "info", otherParams);
|
|
53
|
+
$toast.error = (message, otherParams = {}) => $toast(message, "error", otherParams);
|
|
54
|
+
$toast.warning = (message, otherParams = {}) => $toast(message, "warning", otherParams);
|
|
55
|
+
function setStorage(storageName, params, isSession = false) {
|
|
56
|
+
let handleParams;
|
|
57
|
+
if (typeof params === "number" || typeof params === "string") {
|
|
58
|
+
handleParams = params;
|
|
59
|
+
} else {
|
|
60
|
+
handleParams = JSON.stringify(params);
|
|
61
|
+
}
|
|
62
|
+
if (isSession) {
|
|
63
|
+
sessionStorage.setItem(storageName, handleParams);
|
|
64
|
+
} else {
|
|
65
|
+
localStorage.setItem(storageName, handleParams);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function getStorage(data, isSession = false) {
|
|
69
|
+
let getLocalData = "";
|
|
70
|
+
let getSessionData = "";
|
|
71
|
+
if (isSession) {
|
|
72
|
+
getSessionData = sessionStorage.getItem(data);
|
|
73
|
+
} else {
|
|
74
|
+
getLocalData = localStorage.getItem(data);
|
|
75
|
+
}
|
|
76
|
+
if (getLocalData) {
|
|
77
|
+
try {
|
|
78
|
+
if (typeof JSON.parse(getLocalData) !== "number") {
|
|
79
|
+
getLocalData = JSON.parse(getLocalData);
|
|
80
|
+
}
|
|
81
|
+
} catch (e) {
|
|
82
|
+
}
|
|
83
|
+
return getLocalData;
|
|
84
|
+
} else if (getSessionData) {
|
|
85
|
+
try {
|
|
86
|
+
if (typeof JSON.parse(getSessionData) !== "number") {
|
|
87
|
+
getSessionData = JSON.parse(getSessionData);
|
|
88
|
+
}
|
|
89
|
+
} catch (e) {
|
|
90
|
+
}
|
|
91
|
+
return getSessionData;
|
|
92
|
+
}
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
function clearStorage(str = "") {
|
|
96
|
+
if (isEmpty(str)) {
|
|
97
|
+
sessionStorage.clear();
|
|
98
|
+
localStorage.clear();
|
|
99
|
+
}
|
|
100
|
+
if (notEmpty(str) && getType(str) !== "object") {
|
|
101
|
+
let strArr = Array.isArray(str) ? str : [str];
|
|
102
|
+
for (let i = 0; i < strArr.length; i++) {
|
|
103
|
+
sessionStorage.removeItem(strArr[i]);
|
|
104
|
+
localStorage.removeItem(strArr[i]);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (_isObjectWithExclude(str)) {
|
|
108
|
+
if (notEmpty(str.exclude) && getType(str) === "object") {
|
|
109
|
+
let sessionStorageObj = {};
|
|
110
|
+
let localStorageObj = {};
|
|
111
|
+
for (const key in str.exclude) {
|
|
112
|
+
if (Object.prototype.hasOwnProperty.call(str.exclude, key)) {
|
|
113
|
+
const name = str.exclude[key];
|
|
114
|
+
if (getStorage(name)) {
|
|
115
|
+
localStorageObj[name] = getStorage(name);
|
|
116
|
+
}
|
|
117
|
+
if (getStorage(name, true)) {
|
|
118
|
+
sessionStorageObj[name] = getStorage(name, true);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
sessionStorage.clear();
|
|
123
|
+
localStorage.clear();
|
|
124
|
+
for (const key in sessionStorageObj) {
|
|
125
|
+
setStorage(key, sessionStorageObj[key], true);
|
|
126
|
+
}
|
|
127
|
+
for (const key in localStorageObj) {
|
|
128
|
+
setStorage(key, localStorageObj[key]);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function _isObjectWithExclude(obj) {
|
|
134
|
+
return typeof obj === "object" && obj !== null && "exclude" in obj && typeof obj.exclude === "object";
|
|
135
|
+
}
|
|
136
|
+
function validForm(ref, { message = "\u8868\u5355\u6821\u9A8C\u9519\u8BEF, \u8BF7\u68C0\u67E5", detail = false, showMessage = true } = {}) {
|
|
137
|
+
return new Promise((resolve, reject) => {
|
|
138
|
+
reactivity.unref(ref).validate((valid, status) => {
|
|
139
|
+
if (valid) {
|
|
140
|
+
resolve(status);
|
|
141
|
+
} else {
|
|
142
|
+
if (message && showMessage) {
|
|
143
|
+
let errorText = Object.keys(status);
|
|
144
|
+
let toastMessage = message;
|
|
145
|
+
if (detail) {
|
|
146
|
+
toastMessage = message + errorText.join(",");
|
|
147
|
+
}
|
|
148
|
+
$toast(toastMessage, "e");
|
|
149
|
+
}
|
|
150
|
+
reject(status);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
function isEmpty(data) {
|
|
156
|
+
if (reactivity.isRef(data)) {
|
|
157
|
+
data = reactivity.unref(data);
|
|
158
|
+
}
|
|
159
|
+
if (data instanceof Date) {
|
|
160
|
+
return isNaN(data.getTime());
|
|
161
|
+
}
|
|
162
|
+
switch (typeof data) {
|
|
163
|
+
case "undefined":
|
|
164
|
+
return true;
|
|
165
|
+
case "string":
|
|
166
|
+
if (data.trim().length === 0) return true;
|
|
167
|
+
break;
|
|
168
|
+
case "boolean":
|
|
169
|
+
if (!data) return true;
|
|
170
|
+
break;
|
|
171
|
+
case "number":
|
|
172
|
+
if (0 === data) return true;
|
|
173
|
+
break;
|
|
174
|
+
case "object":
|
|
175
|
+
if (null === data) return true;
|
|
176
|
+
if (void 0 !== data.length && data.length === 0) return true;
|
|
177
|
+
for (var k in data) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
function notEmpty(v) {
|
|
185
|
+
return !isEmpty(v);
|
|
186
|
+
}
|
|
187
|
+
function merge(obj1, obj2) {
|
|
188
|
+
let merged = { ...obj1, ...obj2 };
|
|
189
|
+
for (let key in merged) {
|
|
190
|
+
if (!isEmpty(obj1[key]) && !isEmpty(obj2[key])) {
|
|
191
|
+
merged[key] = obj2[key];
|
|
192
|
+
} else if (isEmpty(obj1[key]) && !isEmpty(obj2[key])) {
|
|
193
|
+
merged[key] = obj2[key];
|
|
194
|
+
} else if (!isEmpty(obj1[key]) && isEmpty(obj2[key])) {
|
|
195
|
+
merged[key] = obj1[key];
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return merged;
|
|
199
|
+
}
|
|
200
|
+
function clone(data, times = 1) {
|
|
201
|
+
if (reactivity.isRef(data)) {
|
|
202
|
+
data = reactivity.unref(data);
|
|
203
|
+
}
|
|
204
|
+
if (getType(data) !== "array") {
|
|
205
|
+
return lodashEs.cloneDeep(data);
|
|
206
|
+
}
|
|
207
|
+
const clonedData = lodashEs.cloneDeep(data);
|
|
208
|
+
const result = [];
|
|
209
|
+
for (let i = 0; i < times; i++) {
|
|
210
|
+
result.push(...clonedData);
|
|
211
|
+
}
|
|
212
|
+
return result;
|
|
213
|
+
}
|
|
214
|
+
function formatTime(time, cFormat = "{y}-{m}-{d} {h}:{i}:{s}") {
|
|
215
|
+
if (!time) {
|
|
216
|
+
return time;
|
|
217
|
+
}
|
|
218
|
+
let date;
|
|
219
|
+
if (typeof time === "object") {
|
|
220
|
+
date = time;
|
|
221
|
+
} else {
|
|
222
|
+
if (("" + time).length === 10) time = parseInt(time) * 1e3;
|
|
223
|
+
date = new Date(time);
|
|
224
|
+
}
|
|
225
|
+
const formatObj = {
|
|
226
|
+
y: date.getFullYear(),
|
|
227
|
+
m: date.getMonth() + 1,
|
|
228
|
+
d: date.getDate(),
|
|
229
|
+
h: date.getHours(),
|
|
230
|
+
i: date.getMinutes(),
|
|
231
|
+
s: date.getSeconds(),
|
|
232
|
+
a: date.getDay()
|
|
233
|
+
};
|
|
234
|
+
const time_str = cFormat.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
|
235
|
+
let value = formatObj[key];
|
|
236
|
+
if (key === "a") {
|
|
237
|
+
return ["\u65E5", "\u4E00", "\u4E8C", "\u4E09", "\u56DB", "\u4E94", "\u516D"][value];
|
|
238
|
+
}
|
|
239
|
+
if (result.length > 0 && value < 10) {
|
|
240
|
+
value = "0" + value;
|
|
241
|
+
}
|
|
242
|
+
return value || 0;
|
|
243
|
+
});
|
|
244
|
+
return time_str;
|
|
245
|
+
}
|
|
246
|
+
function formatDurationTime(timestamp, cFormat = "{d} \u5929 {h} \u65F6 {i} \u5206 {s} \u79D2") {
|
|
247
|
+
const secondsPerMinute = 60;
|
|
248
|
+
const minutesPerHour = 60;
|
|
249
|
+
const hoursPerDay = 24;
|
|
250
|
+
let totalSeconds = Math.floor(timestamp / 1e3);
|
|
251
|
+
let days = 0;
|
|
252
|
+
if (cFormat.indexOf("d") !== -1) {
|
|
253
|
+
days = Math.floor(totalSeconds / (secondsPerMinute * minutesPerHour * hoursPerDay));
|
|
254
|
+
totalSeconds %= secondsPerMinute * minutesPerHour * hoursPerDay;
|
|
255
|
+
}
|
|
256
|
+
let hours = Math.floor(totalSeconds / (secondsPerMinute * minutesPerHour));
|
|
257
|
+
totalSeconds %= secondsPerMinute * minutesPerHour;
|
|
258
|
+
let minutes = Math.floor(totalSeconds / secondsPerMinute);
|
|
259
|
+
let seconds = totalSeconds % secondsPerMinute;
|
|
260
|
+
const formatObj = {
|
|
261
|
+
d: days,
|
|
262
|
+
h: hours,
|
|
263
|
+
i: minutes,
|
|
264
|
+
s: seconds
|
|
265
|
+
};
|
|
266
|
+
let parseFormat = cFormat;
|
|
267
|
+
if (days === 0) {
|
|
268
|
+
parseFormat = cFormat.match(/{h}.*/g)[0];
|
|
269
|
+
if (hours === 0) {
|
|
270
|
+
parseFormat = cFormat.match(/{i}.*/g)[0];
|
|
271
|
+
if (minutes === 0) {
|
|
272
|
+
parseFormat = cFormat.match(/{s}.*/g)[0];
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const time_str = parseFormat.replace(/{(y|m|d|h|i|s)+}/g, (result, key) => {
|
|
277
|
+
let value = formatObj[key];
|
|
278
|
+
if (result.length > 0 && value < 10 && value != 0) {
|
|
279
|
+
value = "0" + value;
|
|
280
|
+
}
|
|
281
|
+
return value || "00";
|
|
282
|
+
});
|
|
283
|
+
return time_str;
|
|
284
|
+
}
|
|
285
|
+
function uuid(type = "", length = 4, { emailStr = "@qq.com", timeStr = "{m}-{d} {h}:{i}:{s}", startStr = "", optionsIndex = null } = {}) {
|
|
286
|
+
let randomStr = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
|
|
287
|
+
let res = type;
|
|
288
|
+
if (reactivity.isRef(type)) {
|
|
289
|
+
type = reactivity.unref(type);
|
|
290
|
+
}
|
|
291
|
+
if (getType(type) === "array" && type.length > 0) {
|
|
292
|
+
let randNum = random(0, type.length - 1);
|
|
293
|
+
if (!length) {
|
|
294
|
+
return type[optionsIndex ?? randNum];
|
|
295
|
+
}
|
|
296
|
+
return type[optionsIndex ?? randNum][length === 4 ? "value" : length];
|
|
297
|
+
}
|
|
298
|
+
if (type === "phone") {
|
|
299
|
+
let prefixArray = new Array("130", "131", "132", "133", "135", "136", "137", "138", "170", "187", "189");
|
|
300
|
+
let i = parseInt(Math.random() * 10);
|
|
301
|
+
let res2 = prefixArray[i];
|
|
302
|
+
for (var j = 0; j < 8; j++) {
|
|
303
|
+
res2 += Math.floor(Math.random() * 10);
|
|
304
|
+
}
|
|
305
|
+
return res2;
|
|
306
|
+
}
|
|
307
|
+
if (type === "email") {
|
|
308
|
+
return uuid(startStr, length) + emailStr;
|
|
309
|
+
}
|
|
310
|
+
if (type === "time") {
|
|
311
|
+
return uuid(startStr, length) + " " + formatTime(/* @__PURE__ */ new Date(), timeStr);
|
|
312
|
+
}
|
|
313
|
+
if (type === "number") {
|
|
314
|
+
let randomStr2 = "123456789";
|
|
315
|
+
let res2 = "";
|
|
316
|
+
for (let i = length; i > 0; --i) {
|
|
317
|
+
res2 += randomStr2[Math.floor(Math.random() * randomStr2.length)];
|
|
318
|
+
}
|
|
319
|
+
return Number(res2);
|
|
320
|
+
}
|
|
321
|
+
if (type === "ip") {
|
|
322
|
+
let randomNum = random(1, 99);
|
|
323
|
+
return `10.0.11.` + randomNum;
|
|
324
|
+
}
|
|
325
|
+
if (type === "port") {
|
|
326
|
+
let randomNum = random(1, 65535);
|
|
327
|
+
return randomNum;
|
|
328
|
+
}
|
|
329
|
+
for (let i = length; i > 0; --i) {
|
|
330
|
+
res += randomStr[Math.floor(Math.random() * randomStr.length)];
|
|
331
|
+
}
|
|
332
|
+
return res;
|
|
333
|
+
}
|
|
334
|
+
function getType(type) {
|
|
335
|
+
if (typeof type === "object") {
|
|
336
|
+
const objType = Object.prototype.toString.call(type).slice(8, -1).toLowerCase();
|
|
337
|
+
return objType;
|
|
338
|
+
} else {
|
|
339
|
+
return typeof type;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
function sleep(delay = 0, fn) {
|
|
343
|
+
return new Promise(
|
|
344
|
+
(resolve) => setTimeout(() => {
|
|
345
|
+
fn?.();
|
|
346
|
+
resolve();
|
|
347
|
+
}, delay)
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
function validate(type = "required", rules = {}, pureValid = false) {
|
|
351
|
+
if (getType(type) === "object") {
|
|
352
|
+
pureValid = rules || false;
|
|
353
|
+
rules = type;
|
|
354
|
+
type = "required";
|
|
355
|
+
}
|
|
356
|
+
let trigger = rules.trigger || [];
|
|
357
|
+
const typeMaps = ["required", "pwd", "number", "mobile", "between", "length", "same", "ip", "port", "custom"];
|
|
358
|
+
let parseRequired = rules.required ?? true;
|
|
359
|
+
if (!typeMaps.includes(type)) {
|
|
360
|
+
return {
|
|
361
|
+
required: parseRequired,
|
|
362
|
+
message: type,
|
|
363
|
+
trigger
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
if (type === "required") {
|
|
367
|
+
return {
|
|
368
|
+
required: parseRequired,
|
|
369
|
+
message: rules.message ?? "\u8BF7\u8F93\u5165",
|
|
370
|
+
trigger
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
if (type === "password") {
|
|
374
|
+
const validateName = (rule, value, callback) => {
|
|
375
|
+
let validFlag = /^[a-zA-Z0-9_-]+$/.test(value);
|
|
376
|
+
if (!validFlag) {
|
|
377
|
+
callback(new Error(rules.message || "\u5BC6\u7801\u53EA\u80FD\u7531\u82F1\u6587\u3001\u6570\u5B57\u3001\u4E0B\u5212\u7EBF\u3001\u4E2D\u5212\u7EBF\u7EC4\u6210"));
|
|
378
|
+
} else {
|
|
379
|
+
callback();
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
return {
|
|
383
|
+
validator: validateName,
|
|
384
|
+
trigger
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
if (type === "positive" || type === "number") {
|
|
388
|
+
return _validValue(rules, "\u8BF7\u8F93\u5165\u6B63\u6574\u6570", pureValid, /^[1-9]+\d*$/);
|
|
389
|
+
}
|
|
390
|
+
if (type === "zeroPositive") {
|
|
391
|
+
return _validValue(rules, "\u8BF7\u8F93\u5165\u975E\u8D1F\u6574\u6570", pureValid, /^(0|[1-9]+\d*)$/);
|
|
392
|
+
}
|
|
393
|
+
if (type === "integer") {
|
|
394
|
+
return _validValue(rules, "\u8BF7\u8F93\u5165\u6574\u6570", pureValid, /^(0|[-]?[1-9]\d*)$/);
|
|
395
|
+
}
|
|
396
|
+
if (type === "decimal") {
|
|
397
|
+
return _validValue(rules, "\u8BF7\u8F93\u5165\u975E\u8D1F\u6570\u5B57, \u5305\u542B\u5C0F\u6570\u4E14\u6700\u591A2\u4F4D", pureValid, /(0|[1-9]\d*)(\.\d{1, 2})?|0\.\d{1,2}/);
|
|
398
|
+
}
|
|
399
|
+
if (type === "mobile") {
|
|
400
|
+
return _validValue(rules, "\u8BF7\u8F93\u5165\u6B63\u786E\u7684\u624B\u673A\u53F7", pureValid, /^[1][0-9]{10}$/);
|
|
401
|
+
}
|
|
402
|
+
if (type === "ip") {
|
|
403
|
+
return _validValue(
|
|
404
|
+
rules,
|
|
405
|
+
"\u8BF7\u8F93\u5165\u6B63\u786E\u7684ip\u5730\u5740",
|
|
406
|
+
pureValid,
|
|
407
|
+
/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
if (type === "between") {
|
|
411
|
+
let min = rules.min || 1;
|
|
412
|
+
let max = rules.max || 10;
|
|
413
|
+
const validateBetween = (rule, value, callback) => {
|
|
414
|
+
let validFlag = /^[0-9]+$/.test(value);
|
|
415
|
+
if (!validFlag) {
|
|
416
|
+
callback(new Error("\u8BF7\u8F93\u5165\u6570\u5B57"));
|
|
417
|
+
}
|
|
418
|
+
if (value < min) {
|
|
419
|
+
callback(new Error(`\u6570\u5B57\u4E0D\u80FD\u5C0F\u4E8E${min}`));
|
|
420
|
+
}
|
|
421
|
+
if (value > max) {
|
|
422
|
+
callback(new Error(`\u6570\u5B57\u4E0D\u80FD\u5927\u4E8E${max}`));
|
|
423
|
+
}
|
|
424
|
+
callback();
|
|
425
|
+
};
|
|
426
|
+
return {
|
|
427
|
+
validator: validateBetween,
|
|
428
|
+
trigger,
|
|
429
|
+
required: parseRequired
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
if (type === "length") {
|
|
433
|
+
return {
|
|
434
|
+
min: rules.min,
|
|
435
|
+
max: rules.max,
|
|
436
|
+
message: rules.message ?? `\u8BF7\u8F93\u5165${rules.min}\u5230${rules.max}\u4E2A\u5B57\u7B26`,
|
|
437
|
+
trigger,
|
|
438
|
+
required: parseRequired
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
if (type === "port") {
|
|
442
|
+
return _validValue(
|
|
443
|
+
rules,
|
|
444
|
+
"\u8BF7\u8F93\u51651-65535\u7684\u7AEF\u53E3\u53F7",
|
|
445
|
+
pureValid,
|
|
446
|
+
/^([1-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-5][0-5][0-3][0-5])$/
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
if (type === "same") {
|
|
450
|
+
const validateSame = (rule, value, callback) => {
|
|
451
|
+
let isSame = value === rules.value;
|
|
452
|
+
if (!isSame) {
|
|
453
|
+
const errMessage = rules.message || "\u5BC6\u7801\u548C\u786E\u8BA4\u5BC6\u7801\u8981\u4E00\u81F4";
|
|
454
|
+
callback(new Error(errMessage));
|
|
455
|
+
}
|
|
456
|
+
if (parseRequired && !value) {
|
|
457
|
+
callback(new Error(rules.message || "\u8BF7\u8F93\u5165"));
|
|
458
|
+
}
|
|
459
|
+
callback();
|
|
460
|
+
};
|
|
461
|
+
let res = {
|
|
462
|
+
validator: validateSame,
|
|
463
|
+
trigger,
|
|
464
|
+
required: parseRequired
|
|
465
|
+
};
|
|
466
|
+
return res;
|
|
467
|
+
}
|
|
468
|
+
if (type === "custom") {
|
|
469
|
+
if (pureValid) {
|
|
470
|
+
return _validValue(rules.value, rules.message, pureValid, rules.reg);
|
|
471
|
+
} else {
|
|
472
|
+
return _validValue(rules, rules.message, pureValid, rules.reg);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
function _validValue(rules2, msg, pureValid2, reg) {
|
|
476
|
+
if (pureValid2 === true) {
|
|
477
|
+
return reg.test(rules2);
|
|
478
|
+
}
|
|
479
|
+
const validatePhone = (rule, value, callback) => {
|
|
480
|
+
let validFlag = reg.test(value);
|
|
481
|
+
if (!validFlag) {
|
|
482
|
+
callback(new Error(rules2.message ?? msg));
|
|
483
|
+
} else {
|
|
484
|
+
callback();
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
return {
|
|
488
|
+
validator: validatePhone,
|
|
489
|
+
required: rules2.required ?? true,
|
|
490
|
+
trigger
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
async function asyncWrapper(func, ...args) {
|
|
495
|
+
try {
|
|
496
|
+
const res = await func(...args);
|
|
497
|
+
return { res };
|
|
498
|
+
} catch (err) {
|
|
499
|
+
return { err };
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
function formatImg(photoName, addPath = "", { basePath = "assets/images" } = {}) {
|
|
503
|
+
if (photoName.startsWith("http") || photoName.startsWith("https")) {
|
|
504
|
+
return photoName;
|
|
505
|
+
}
|
|
506
|
+
if (photoName.indexOf(".") === -1) {
|
|
507
|
+
photoName = photoName + ".png";
|
|
508
|
+
}
|
|
509
|
+
const addLastSlash = addPath.endsWith("/") || !addPath ? addPath : `${addPath}/`;
|
|
510
|
+
const addLastBasePathSlash = basePath.endsWith("/") || !basePath ? basePath : `${basePath}/`;
|
|
511
|
+
let mergeSrc = `${addLastSlash}${photoName}`;
|
|
512
|
+
let res = new URL(`../${addLastBasePathSlash}${mergeSrc}`, (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href))).href;
|
|
513
|
+
return res;
|
|
514
|
+
}
|
|
515
|
+
const copy = (text, toastParams = {}) => {
|
|
516
|
+
const textarea = document.createElement("textarea");
|
|
517
|
+
textarea.value = text;
|
|
518
|
+
textarea.style.position = "fixed";
|
|
519
|
+
document.body.appendChild(textarea);
|
|
520
|
+
textarea.select();
|
|
521
|
+
document.execCommand("copy");
|
|
522
|
+
document.body.removeChild(textarea);
|
|
523
|
+
if (!toastParams.hideToast) {
|
|
524
|
+
$toast(text + "\u590D\u5236\u6210\u529F", toastParams);
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
function formatThousands(number) {
|
|
528
|
+
let matches = ("" + number).match(/^([\d,]+)(\.?)(\d+)?(\D+)?$/);
|
|
529
|
+
if (!matches) {
|
|
530
|
+
return number;
|
|
531
|
+
}
|
|
532
|
+
let numericString = matches[1].replace(/\D/g, "");
|
|
533
|
+
let decimalString = matches[3] ? `.${matches[3]}` : "";
|
|
534
|
+
let unit = matches[4] || "";
|
|
535
|
+
let numberWithSeparator = numericString.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
536
|
+
return `${numberWithSeparator}${decimalString}${unit}`;
|
|
537
|
+
}
|
|
538
|
+
function log(variableStr, variable, otherInfo = "") {
|
|
539
|
+
const stack = new Error().stack.split("\n")[2].trim();
|
|
540
|
+
const matchResult = stack.match(/\((.*):(\d+):(\d+)\)/);
|
|
541
|
+
let fileInfo = "";
|
|
542
|
+
try {
|
|
543
|
+
if (matchResult && otherInfo) {
|
|
544
|
+
const lineNumber = matchResult[2];
|
|
545
|
+
fileInfo = `vscode://file${JSON.parse(otherInfo)}:${lineNumber}`;
|
|
546
|
+
}
|
|
547
|
+
} catch (error) {
|
|
548
|
+
fileInfo = otherInfo;
|
|
549
|
+
}
|
|
550
|
+
if (reactivity.isRef(variable)) {
|
|
551
|
+
let unrefVariable = reactivity.unref(variable);
|
|
552
|
+
_log(reactivity.toRaw(unrefVariable));
|
|
553
|
+
} else {
|
|
554
|
+
_log(variable);
|
|
555
|
+
}
|
|
556
|
+
function _log(consoleData) {
|
|
557
|
+
if (getType2(consoleData) === "object" || getType2(consoleData) === "array") {
|
|
558
|
+
consola.consola.log(
|
|
559
|
+
`%c${variableStr} `,
|
|
560
|
+
"background:#fff; color: blue;font-size: 0.8em",
|
|
561
|
+
JSON.stringify(consoleData, null, " "),
|
|
562
|
+
`${fileInfo}`
|
|
563
|
+
);
|
|
564
|
+
} else {
|
|
565
|
+
consola.consola.log(`%c${variableStr} `, "background:#fff; color: blue;font-size: 0.8em", consoleData, `${fileInfo}`);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
function getType2(type) {
|
|
569
|
+
if (typeof type === "object") {
|
|
570
|
+
const objType = Object.prototype.toString.call(type).slice(8, -1).toLowerCase();
|
|
571
|
+
return objType;
|
|
572
|
+
} else {
|
|
573
|
+
return typeof type;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
function random(min = 0, max = 10) {
|
|
578
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
579
|
+
}
|
|
580
|
+
function toLine(text, connect = "-") {
|
|
581
|
+
let translateText = text.replace(/([A-Z])/g, (match, p1, offset, origin) => {
|
|
582
|
+
if (offset === 0) {
|
|
583
|
+
return `${match.toLocaleLowerCase()}`;
|
|
584
|
+
} else {
|
|
585
|
+
return `${connect}${match.toLocaleLowerCase()}`;
|
|
586
|
+
}
|
|
587
|
+
}).toLocaleLowerCase();
|
|
588
|
+
return translateText;
|
|
589
|
+
}
|
|
590
|
+
function processWidth(initValue, isBase = false) {
|
|
591
|
+
let value = reactivity.unref(initValue);
|
|
592
|
+
let res = "";
|
|
593
|
+
if (!value) {
|
|
594
|
+
return isBase ? value : {};
|
|
595
|
+
} else if (typeof value === "number") {
|
|
596
|
+
value = String(value);
|
|
597
|
+
}
|
|
598
|
+
if (value === "") {
|
|
599
|
+
return isBase ? value : {};
|
|
600
|
+
} else if (typeof value === "string" && !isNaN(value)) {
|
|
601
|
+
res = value + "px";
|
|
602
|
+
} else if (typeof value === "string" && /^[0-9]+(\.[0-9]+)?(px|%|em|rem|vw|vh|ch)*$/.test(value)) {
|
|
603
|
+
res = value;
|
|
604
|
+
} else {
|
|
605
|
+
console.warn(`${value} is Invalid unit provided`);
|
|
606
|
+
return value;
|
|
607
|
+
}
|
|
608
|
+
if (isBase) {
|
|
609
|
+
return res;
|
|
610
|
+
}
|
|
611
|
+
return { width: res };
|
|
612
|
+
}
|
|
613
|
+
function toFixed(value, digits = 2) {
|
|
614
|
+
let matches = ("" + value).match(/^([\d,]+)(\.?)(\d+)?(\D+)?$/);
|
|
615
|
+
if (!matches) {
|
|
616
|
+
return value;
|
|
617
|
+
}
|
|
618
|
+
let numericString = matches[1].replace(/\D/g, "");
|
|
619
|
+
let decimalString = matches[3] ? `.${matches[3]}` : "";
|
|
620
|
+
let unit = matches[4] || "";
|
|
621
|
+
let res = numericString;
|
|
622
|
+
if (isStringNumber(numericString) || isNumber(numericString)) {
|
|
623
|
+
res = Number(numericString + decimalString).toFixed(digits);
|
|
624
|
+
}
|
|
625
|
+
return `${res}${unit}`;
|
|
626
|
+
}
|
|
627
|
+
function formatBytes(bytes, { toFixed: toFixed2 = 2, thousands = true } = {}) {
|
|
628
|
+
if (isStringNumber(bytes) || isNumber(bytes)) {
|
|
629
|
+
bytes = Number(bytes);
|
|
630
|
+
} else {
|
|
631
|
+
return bytes;
|
|
632
|
+
}
|
|
633
|
+
if (bytes <= 0) {
|
|
634
|
+
return bytes.toFixed(toFixed2) + " B";
|
|
635
|
+
}
|
|
636
|
+
const k = 1024;
|
|
637
|
+
const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
638
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
639
|
+
let res = (bytes / Math.pow(k, i)).toFixed(toFixed2) + " " + sizes[i];
|
|
640
|
+
if (thousands) {
|
|
641
|
+
res = formatThousands(res);
|
|
642
|
+
}
|
|
643
|
+
return res;
|
|
644
|
+
}
|
|
645
|
+
function formatBytesConvert(oBytes, { thounsand = false, toFixed: toFixed2 = 0 } = {}) {
|
|
646
|
+
if (isStringNumber(oBytes) || isNumber(oBytes) || getType(oBytes) !== "string") {
|
|
647
|
+
return oBytes;
|
|
648
|
+
}
|
|
649
|
+
if (!oBytes) {
|
|
650
|
+
return oBytes;
|
|
651
|
+
}
|
|
652
|
+
const regex = /^\d{1,3}(,\d{3})*(\.\d+)?[a-zA-Z ]*$/;
|
|
653
|
+
let bytes = oBytes;
|
|
654
|
+
if (regex.test(oBytes)) {
|
|
655
|
+
bytes = oBytes.replace(/,/g, "");
|
|
656
|
+
if (isStringNumber(bytes) || isNumber(bytes) || getType(bytes) !== "string") {
|
|
657
|
+
return bytes;
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
const bytesRegex = /^(\d+(?:\.\d+)?)\s*([BKMGTPEZY]?B|Byte)$/i;
|
|
661
|
+
const units = {
|
|
662
|
+
B: 1,
|
|
663
|
+
BYTE: 1,
|
|
664
|
+
KB: 1024,
|
|
665
|
+
MB: 1024 ** 2,
|
|
666
|
+
GB: 1024 ** 3,
|
|
667
|
+
TB: 1024 ** 4,
|
|
668
|
+
PB: 1024 ** 5,
|
|
669
|
+
EB: 1024 ** 6,
|
|
670
|
+
ZB: 1024 ** 7,
|
|
671
|
+
YB: 1024 ** 8
|
|
672
|
+
};
|
|
673
|
+
const match = bytes.match(bytesRegex);
|
|
674
|
+
if (!match) {
|
|
675
|
+
console.warn("Invalid bytes format. Please provide a valid bytes string, like '100GB'.");
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
const size = parseFloat(match[1]);
|
|
679
|
+
const unit = match[2].toUpperCase();
|
|
680
|
+
if (!units.hasOwnProperty(unit)) {
|
|
681
|
+
console.warn(
|
|
682
|
+
"Invalid bytes unit. Please provide a valid unit, like 'B', 'BYTE', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', or 'YB'."
|
|
683
|
+
);
|
|
684
|
+
return;
|
|
685
|
+
}
|
|
686
|
+
let finalRes = size * units[unit];
|
|
687
|
+
if (toFixed2) {
|
|
688
|
+
finalRes = parseFloat(finalRes.toFixed(toFixed2));
|
|
689
|
+
}
|
|
690
|
+
if (thounsand) {
|
|
691
|
+
finalRes = formatThousands(finalRes);
|
|
692
|
+
}
|
|
693
|
+
return finalRes;
|
|
694
|
+
}
|
|
695
|
+
function throttle(fn, delay = 1e3) {
|
|
696
|
+
let last = 0;
|
|
697
|
+
let timer = null;
|
|
698
|
+
return function() {
|
|
699
|
+
let context = this;
|
|
700
|
+
let args = arguments;
|
|
701
|
+
let now = +/* @__PURE__ */ new Date();
|
|
702
|
+
if (now - last < delay) {
|
|
703
|
+
clearTimeout(timer);
|
|
704
|
+
timer = setTimeout(function() {
|
|
705
|
+
last = now;
|
|
706
|
+
fn.apply(context, args);
|
|
707
|
+
}, delay);
|
|
708
|
+
} else {
|
|
709
|
+
last = now;
|
|
710
|
+
fn.apply(context, args);
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
function debounce(fn, delay = 1e3) {
|
|
715
|
+
let timer = null;
|
|
716
|
+
return function() {
|
|
717
|
+
if (timer) {
|
|
718
|
+
clearTimeout(timer);
|
|
11
719
|
}
|
|
720
|
+
timer = setTimeout(() => {
|
|
721
|
+
fn.apply(this, arguments);
|
|
722
|
+
timer = null;
|
|
723
|
+
}, delay);
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
function confirm(message, options) {
|
|
727
|
+
const resolvedMessage = typeof message === "function" ? message() : message;
|
|
728
|
+
const elContext = elementPlus.ElMessageBox.install?.context || elementPlus.ElMessageBox._context || document.querySelector("#app")?._vue_app?._context;
|
|
729
|
+
const mergeOptions = {
|
|
730
|
+
title: "\u63D0\u793A",
|
|
731
|
+
draggable: true,
|
|
732
|
+
showCancelButton: false,
|
|
733
|
+
confirmButtonText: "\u786E\u5B9A",
|
|
734
|
+
dangerouslyUseHTMLString: true,
|
|
735
|
+
// 允许 HTML
|
|
736
|
+
appContext: elContext,
|
|
737
|
+
// 强制注入 Element Plus 的上下文
|
|
738
|
+
...options
|
|
739
|
+
};
|
|
740
|
+
return elementPlus.ElMessageBox.confirm(resolvedMessage, mergeOptions);
|
|
741
|
+
}
|
|
742
|
+
function formatNewLines(str) {
|
|
743
|
+
if (!str || typeof str !== "string") {
|
|
744
|
+
return str;
|
|
12
745
|
}
|
|
13
|
-
|
|
746
|
+
str = str.replace(/\n/g, "<br>");
|
|
747
|
+
str = str.replace(/\t/g, " ");
|
|
748
|
+
return str;
|
|
749
|
+
}
|
|
750
|
+
function getVariable(propertyName) {
|
|
751
|
+
let res = getComputedStyle(document.documentElement).getPropertyValue(propertyName).trim();
|
|
752
|
+
return res;
|
|
753
|
+
}
|
|
14
754
|
|
|
15
|
-
|
|
16
|
-
|
|
755
|
+
exports.$toast = $toast;
|
|
756
|
+
exports.asyncWrapper = asyncWrapper;
|
|
757
|
+
exports.clearStorage = clearStorage;
|
|
758
|
+
exports.clone = clone;
|
|
759
|
+
exports.confirm = confirm;
|
|
760
|
+
exports.copy = copy;
|
|
761
|
+
exports.debounce = debounce;
|
|
762
|
+
exports.formatBytes = formatBytes;
|
|
763
|
+
exports.formatBytesConvert = formatBytesConvert;
|
|
764
|
+
exports.formatDurationTime = formatDurationTime;
|
|
765
|
+
exports.formatImg = formatImg;
|
|
766
|
+
exports.formatNewLines = formatNewLines;
|
|
767
|
+
exports.formatThousands = formatThousands;
|
|
768
|
+
exports.formatTime = formatTime;
|
|
769
|
+
exports.getStorage = getStorage;
|
|
770
|
+
exports.getType = getType;
|
|
771
|
+
exports.getVariable = getVariable;
|
|
772
|
+
exports.isEmpty = isEmpty;
|
|
773
|
+
exports.isNumber = isNumber;
|
|
774
|
+
exports.isString = isString;
|
|
775
|
+
exports.isStringNumber = isStringNumber;
|
|
776
|
+
exports.log = log;
|
|
777
|
+
exports.merge = merge;
|
|
778
|
+
exports.notEmpty = notEmpty;
|
|
779
|
+
exports.processWidth = processWidth;
|
|
780
|
+
exports.random = random;
|
|
781
|
+
exports.setStorage = setStorage;
|
|
782
|
+
exports.sleep = sleep;
|
|
783
|
+
exports.throttle = throttle;
|
|
784
|
+
exports.toFixed = toFixed;
|
|
785
|
+
exports.toLine = toLine;
|
|
786
|
+
exports.uuid = uuid;
|
|
787
|
+
exports.validForm = validForm;
|
|
788
|
+
exports.validate = validate;
|