169 lines
5.7 KiB
JavaScript
169 lines
5.7 KiB
JavaScript
import Vue from 'vue';
|
|
import store from "@/store";
|
|
import {
|
|
requestPackageAndAnonymizImage,
|
|
} from "@/api/load.js";
|
|
import streamSaver from "streamsaver";
|
|
import "streamsaver/examples/zip-stream.js"
|
|
let flag = {};
|
|
export const resetFlag = () => {
|
|
flag = {};
|
|
store.state.trials.uploadTip = null;
|
|
if (store.state.trials.timer) {
|
|
clearInterval(store.state.trials.timer);
|
|
store.state.trials.timer = null;
|
|
}
|
|
store.dispatch("trials/setUnLock", false);
|
|
}
|
|
export const downloadImage = async (id, id2, IsDicom = true) => {
|
|
// if (flag[`${id2}_${IsDicom}`]) return Vue.prototype.$message.warning(Vue.prototype.$t('trials:upload:tip:uploading'));
|
|
if (flag[`${id2}_${IsDicom}`]) return false;
|
|
flag[`${id2}_${IsDicom}`] = true
|
|
try {
|
|
let params = {
|
|
TrialId: id,
|
|
SubjectVisitId: id2,
|
|
IsDicom: IsDicom
|
|
}
|
|
store.dispatch("trials/setUnLock", true);
|
|
let res = await requestPackageAndAnonymizImage(params);
|
|
if (res.IsSuccess) {
|
|
if (!res.Result) {
|
|
flag[`${id2}_${IsDicom}`] = false;
|
|
// Vue.prototype.$message.warning(Vue.prototype.$t("trials:upload:message:not"))
|
|
let message = Vue.prototype.$t('trials:upload:tip:uploading').replace("xxx", res.OtherInfo.FileName);
|
|
store.state.trials.uploadTip = message;
|
|
if (!store.state.trials.timer) {
|
|
store.state.trials.timer = setInterval(() => {
|
|
downloadImage(id, id2, IsDicom);
|
|
}, 2000);
|
|
}
|
|
return false;
|
|
}
|
|
if (store.state.trials.timer) {
|
|
clearInterval(store.state.trials.timer);
|
|
store.state.trials.timer = null;
|
|
}
|
|
let fileName = res.Result.split("/").pop();
|
|
let href = Vue.prototype.OSSclientConfig.basePath + res.Result;
|
|
if (fileName !== res.OtherInfo.FileName) {
|
|
let message = Vue.prototype.$t('trials:upload:tip:uploading').replace("xxx", res.OtherInfo.FileName);
|
|
store.state.trials.uploadTip = message;
|
|
// Vue.prototype.$message.success(Vue.prototype.$t("trials:upload:message:startUpload"));
|
|
return download(href, res.OtherInfo.FileName, { id2, IsDicom });
|
|
|
|
}
|
|
let a = document.createElement("a");
|
|
a.download = res.OtherInfo.FileName;
|
|
a.href = href;
|
|
a.click();
|
|
URL.revokeObjectURL(href);
|
|
let timer = setTimeout(() => {
|
|
a = null;
|
|
href = null;
|
|
timer = null;
|
|
}, 500)
|
|
store.state.trials.uploadTip = null;
|
|
return true;
|
|
} else {
|
|
flag[`${id2}_${IsDicom}`] = false;
|
|
return false;
|
|
}
|
|
} catch (err) {
|
|
flag[`${id2}_${IsDicom}`] = false;
|
|
console.log(err);
|
|
}
|
|
};
|
|
export const fileDownload = (content, filename) => {
|
|
const eleLink = document.createElement("a");
|
|
eleLink.download = filename;
|
|
eleLink.style.display = "none";
|
|
const blob = new Blob([content]);
|
|
eleLink.href = URL.createObjectURL(blob);
|
|
document.body.appendChild(eleLink);
|
|
eleLink.click();
|
|
document.body.removeChild(eleLink);
|
|
};
|
|
let download = async (downloadUrl, downloadFileName, res) => {
|
|
const blob = await getBlob(downloadUrl);
|
|
flag[`${res.id2}_${res.IsDicom}`] = false;
|
|
store.state.trials.uploadTip = null;
|
|
store.dispatch("trials/setUnLock", false);
|
|
saveAsB(blob, downloadFileName);
|
|
return true;
|
|
}
|
|
|
|
let getBlob = (url) => {
|
|
return new Promise((resolve, reject) => {
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('GET', url, true);
|
|
xhr.responseType = 'blob';
|
|
xhr.onload = () => {
|
|
if (xhr.status === 200) {
|
|
resolve(xhr.response);
|
|
} else {
|
|
reject(new Error(`Request failed with status ${xhr.status}`));
|
|
}
|
|
};
|
|
xhr.onerror = () => {
|
|
reject(new Error('Request failed'));
|
|
};
|
|
|
|
xhr.send();
|
|
});
|
|
}
|
|
|
|
let saveAsB = (blob, filename) => {
|
|
const link = document.createElement('a');
|
|
const body = document.body;
|
|
|
|
link.href = window.URL.createObjectURL(blob);
|
|
link.download = filename;
|
|
|
|
// hide the link
|
|
link.style.display = 'none';
|
|
body.appendChild(link);
|
|
|
|
link.click();
|
|
body.removeChild(link);
|
|
|
|
window.URL.revokeObjectURL(link.href);
|
|
}
|
|
|
|
// 前端流式打包下载
|
|
const handleBatchDown = async (item, zip) => {
|
|
return new Promise((resolve) => {
|
|
console.log("同步下载打包开始时间:" + new Date());
|
|
// 创建压缩文件输出流
|
|
const zipFileOutputStream = streamSaver.createWriteStream(zipName);
|
|
// 创建下载文件流
|
|
const fileIterator = files.values();
|
|
const readableZipStream = new window.ZIP({
|
|
async pull(ctrl) {
|
|
const fileInfo = fileIterator.next();
|
|
if (fileInfo.done) {//迭代终止
|
|
ctrl.close();
|
|
} else {
|
|
const { name, url } = fileInfo.value;
|
|
return fetch(url).then(res => {
|
|
ctrl.enqueue({
|
|
name,
|
|
stream: () => res.body
|
|
})
|
|
})
|
|
}
|
|
}
|
|
});
|
|
if (window.WritableStream && readableZipStream.pipeTo) {
|
|
// 开始下载
|
|
readableZipStream
|
|
.pipeTo(zipFileOutputStream)
|
|
.then(() => { console.log("同步下载打包结束时间:" + new Date()); resolve(true) })
|
|
} else {
|
|
resolve(false);
|
|
}
|
|
})
|
|
|
|
};
|