Files
lanecarford_front/src/utils/tools.ts

149 lines
4.4 KiB
TypeScript
Raw Normal View History

2025-10-09 09:29:36 +08:00
function getUniversalZoomLevel() {
// 现代浏览器方案
if (window.visualViewport) {
return window.visualViewport.scale;
}
// 备用方案1
if (window.devicePixelRatio) {
return window.devicePixelRatio;
}
// 备用方案2不精确
return window.outerWidth / window.innerWidth;
}
2025-10-23 15:11:24 +08:00
const getMousePosition = (e: any, bor: any) => {
2025-10-09 09:29:36 +08:00
// if(e?.stopPropagation)e.stopPropagation()
// if(e?.preventDefault)e.preventDefault();
2025-10-23 15:11:24 +08:00
let event: any
if (bor) {
2025-10-09 09:29:36 +08:00
const touch = e.changedTouches[0] as any;
event = {
2025-10-23 15:11:24 +08:00
offsetX: touch.clientX - e.target.getBoundingClientRect().left,
2025-10-09 09:29:36 +08:00
offsetY: touch.clientY - e.target.getBoundingClientRect().top,
2025-10-23 15:11:24 +08:00
clientX: touch.clientX,
clientY: touch.clientY,
screenX: touch.screenX,
screenY: touch.screenY,
target: e.target,
2025-10-09 09:29:36 +08:00
}
// if(dom){
// event.offsetX = touch.clientX - dom.getBoundingClientRect().left
// event.offsetY = touch.clientY - dom.getBoundingClientRect().top
// }
2025-10-23 15:11:24 +08:00
} else {
2025-10-09 09:29:36 +08:00
event = {
2025-10-23 15:11:24 +08:00
offsetX: e.offsetX,
offsetY: e.offsetY,
clientX: e.clientX,
clientY: e.clientY,
screenX: e.screenX,
screenY: e.screenY,
target: e.target,
2025-10-09 09:29:36 +08:00
}
}
return event
}
2025-10-28 11:33:20 +08:00
/**
* UUID v4
* @returns UUID v4字符串xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
*/
export function generateUUID(): string {
// 优先使用现代浏览器的crypto.randomUUID()方法
if (typeof crypto !== 'undefined' && crypto.randomUUID) {
return crypto.randomUUID()
}
// 备用方案手动生成UUID v4
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0
const v = c === 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
2025-10-09 09:29:36 +08:00
export {
getUniversalZoomLevel,
getMousePosition,
2025-10-21 13:41:54 +08:00
}
2025-10-23 15:11:24 +08:00
/** -
* @param value ||
* @param format 'yyyy-MM-dd HH:mm:ss'
* @returns
*/
export function FormatDate(value: Date | number | string, format: string = 'yyyy-MM-dd HH:mm:ss') {
const date = new Date(value);
const yyyy = String(date.getFullYear());
const yy = String(date.getFullYear()).slice(-2);
const MM = String(date.getMonth() + 1).padStart(2, '0');
const M = String(date.getMonth() + 1);
const dd = String(date.getDate()).padStart(2, '0');
const d = String(date.getDate());
const HH = String(date.getHours()).padStart(2, '0');
const H = String(date.getHours());
const mm = String(date.getMinutes()).padStart(2, '0');
const m = String(date.getMinutes());
const ss = String(date.getSeconds()).padStart(2, '0');
const s = String(date.getSeconds());
const str = format.replaceAll('yyyy', yyyy)
.replaceAll('yy', yy)
.replaceAll('MM', MM)
.replaceAll('M', M)
.replaceAll('dd', dd)
.replaceAll('d', d)
.replaceAll('HH', HH)
.replaceAll('H', H)
.replaceAll('mm', mm)
.replaceAll('m', m)
.replaceAll('ss', ss)
.replaceAll('s', s);
return str;
}
2025-10-21 13:41:54 +08:00
/**
*
* @param list
* @param onProgress
* @param onError
* @param onSuccess
*/
2025-10-30 17:12:52 +08:00
export async function DownloadImages(list: Array<{ url: string, name?: string }>, onProgress?: (count: number, total: number, item: any) => void, onError?: (count: number, total: number, item: any) => void, onSuccess?: (successCount: number, errCount: number) => void) {
2025-10-21 13:41:54 +08:00
const total = list.length;
2025-10-21 13:42:45 +08:00
let count = 0;
let successCount = 0;
let errCount = 0;
2025-10-21 13:41:54 +08:00
for (let i = 0; i < list.length; i++) {
await new Promise((resolve) => {
2025-10-21 13:42:45 +08:00
const xhr = new XMLHttpRequest();
2025-10-21 13:41:54 +08:00
xhr.open("GET", list[i].url);
xhr.responseType = "blob"
xhr.onload = function () {
count++;
if (this.status === 200) {
2025-10-21 13:42:45 +08:00
const blob = this.response;
2025-10-21 13:41:54 +08:00
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
2025-10-30 17:12:52 +08:00
a.download = list[i].name || list[i].url.split('/').pop().split('?').shift();
2025-10-21 13:41:54 +08:00
a.click();
successCount++;
2025-10-23 15:11:24 +08:00
typeof onProgress === "function" && onProgress(count, total, list[i]);
2025-10-21 13:41:54 +08:00
resolve(blob);
} else {
errCount++;
2025-10-23 15:11:24 +08:00
typeof onError === "function" && onError(count, total, list[i]);
2025-10-21 13:41:54 +08:00
resolve(true);
}
};
xhr.onerror = function () {
count++;
errCount++;
2025-10-23 15:11:24 +08:00
typeof onError === "function" && onError(count, total, list[i]);
2025-10-21 13:41:54 +08:00
resolve(true);
};
xhr.send();
})
}
2025-10-23 15:11:24 +08:00
typeof onSuccess === "function" && onSuccess(successCount, errCount);
2025-10-21 13:41:54 +08:00
}