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;
|
|
|
|
|
|
}
|
|
|
|
|
|
const getMousePosition = (e:any,bor:any) => {
|
|
|
|
|
|
// if(e?.stopPropagation)e.stopPropagation()
|
|
|
|
|
|
// if(e?.preventDefault)e.preventDefault();
|
|
|
|
|
|
let event:any
|
|
|
|
|
|
if(bor){
|
|
|
|
|
|
const touch = e.changedTouches[0] as any;
|
|
|
|
|
|
event = {
|
|
|
|
|
|
offsetX:touch.clientX - e.target.getBoundingClientRect().left,
|
|
|
|
|
|
offsetY: touch.clientY - e.target.getBoundingClientRect().top,
|
|
|
|
|
|
clientX:touch.clientX,
|
|
|
|
|
|
clientY:touch.clientY,
|
|
|
|
|
|
screenX:touch.screenX,
|
|
|
|
|
|
screenY:touch.screenY,
|
|
|
|
|
|
target:e.target,
|
|
|
|
|
|
}
|
|
|
|
|
|
// if(dom){
|
|
|
|
|
|
// event.offsetX = touch.clientX - dom.getBoundingClientRect().left
|
|
|
|
|
|
// event.offsetY = touch.clientY - dom.getBoundingClientRect().top
|
|
|
|
|
|
// }
|
|
|
|
|
|
}else{
|
|
|
|
|
|
event = {
|
|
|
|
|
|
offsetX:e.offsetX,
|
|
|
|
|
|
offsetY:e.offsetY,
|
|
|
|
|
|
clientX:e.clientX,
|
|
|
|
|
|
clientY:e.clientY,
|
|
|
|
|
|
screenX:e.screenX,
|
|
|
|
|
|
screenY:e.screenY,
|
|
|
|
|
|
target:e.target,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return event
|
|
|
|
|
|
}
|
|
|
|
|
|
export {
|
|
|
|
|
|
getUniversalZoomLevel,
|
|
|
|
|
|
getMousePosition,
|
2025-10-21 13:41:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 下载图片
|
|
|
|
|
|
* @param list 图片列表
|
|
|
|
|
|
* @param onProgress 下载进度回调
|
|
|
|
|
|
* @param onError 下载错误回调
|
|
|
|
|
|
* @param onSuccess 下载成功回调
|
|
|
|
|
|
*/
|
|
|
|
|
|
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) {
|
|
|
|
|
|
const total = list.length;
|
|
|
|
|
|
var count = 0;
|
|
|
|
|
|
var successCount = 0;
|
|
|
|
|
|
var errCount = 0;
|
|
|
|
|
|
for (let i = 0; i < list.length; i++) {
|
|
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
|
|
let xhr = new XMLHttpRequest();
|
|
|
|
|
|
xhr.open("GET", list[i].url);
|
|
|
|
|
|
xhr.responseType = "blob"
|
|
|
|
|
|
xhr.onload = function () {
|
|
|
|
|
|
count++;
|
|
|
|
|
|
if (this.status === 200) {
|
|
|
|
|
|
let blob = this.response;
|
|
|
|
|
|
const a = document.createElement('a');
|
|
|
|
|
|
a.href = URL.createObjectURL(blob);
|
|
|
|
|
|
a.download = list[i].name || list[i].url;
|
|
|
|
|
|
a.click();
|
|
|
|
|
|
successCount++;
|
|
|
|
|
|
typeof onProgress === "function" && onProgress(count,total,list[i]);
|
|
|
|
|
|
resolve(blob);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
errCount++;
|
|
|
|
|
|
typeof onError === "function" && onError(count,total,list[i]);
|
|
|
|
|
|
resolve(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
xhr.onerror = function () {
|
|
|
|
|
|
count++;
|
|
|
|
|
|
errCount++;
|
|
|
|
|
|
typeof onError === "function" && onError(count,total,list[i]);
|
|
|
|
|
|
resolve(true);
|
|
|
|
|
|
};
|
|
|
|
|
|
xhr.send();
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
typeof onSuccess === "function" && onSuccess(successCount,errCount);
|
|
|
|
|
|
}
|