From 9bf6c1fd675395bc128171aa2043431e6ea8954f Mon Sep 17 00:00:00 2001 From: caishi <1149225589@qq.com> Date: Fri, 18 Oct 2024 11:38:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E5=93=81=E6=8F=90=E4=BA=A4=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Detail/components/WorkSubmit/index.tsx | 99 +++++++++++-------- src/utils/fetch.ts | 4 +- 2 files changed, 60 insertions(+), 43 deletions(-) diff --git a/src/pages/Competitions/Detail/components/WorkSubmit/index.tsx b/src/pages/Competitions/Detail/components/WorkSubmit/index.tsx index a96ef9cc0..9b3ca11b0 100644 --- a/src/pages/Competitions/Detail/components/WorkSubmit/index.tsx +++ b/src/pages/Competitions/Detail/components/WorkSubmit/index.tsx @@ -1,5 +1,5 @@ -import React, { useEffect, useRef, useState } from 'react' +import React,{ useEffect, useRef, useState } from 'react'; import type { FC } from 'react'; import { connect, ConnectProps, Dispatch, useParams @@ -52,7 +52,6 @@ const WorkSubmit: FC = ({ StaffDetail, getTabResults = () => { } }) => { - const [form] = Form.useForm(); const [formButLoading, setFormButLoading] = useState(false); // 开启/关闭 编辑 @@ -88,18 +87,18 @@ const WorkSubmit: FC = ({ // 分页,搜索 const [urlData, setUrlData] = useState({ page: 1, - per_page: 20, + limit: 20, keyword: "", }); // MD内容 const [mdContent, setMdContent] = useState(""); - // 下载所有文件时打包成zip文件 - const [zipAll, setZipAll] = useState(new JSZip()); + let zipAll = useRef(new JSZip()); + useEffect(() => { setIdentity(userinfo?.admin || userinfo?.business || Editable) }, [userinfo?.admin, userinfo?.business, Editable]) useEffect(() => { - ItemData?.only_file ? setModelType(2) : setModelType(1); + if (ItemData?.only_file) setModelType(2); }, [ItemData]) useEffect(() => { if (TabResults.stages && TabResults.stages?.length > 0) { @@ -112,7 +111,7 @@ const WorkSubmit: FC = ({ getResults(); // getTeamList() getTeam() - }, [gameItem?.id, urlData.page]) + }, [gameItem?.id, urlData.page, urlData.limit]) async function getTeamList() { setisloading(true) let res = await Fetch(`${ENV.API_SERVER}/api/competitions/${identifier}/my_teams`, { @@ -121,6 +120,8 @@ const WorkSubmit: FC = ({ setisloading(false) setIsSubmitModel(true) if (res?.status === 0) { + + setTeamlist(res?.data) form.setFieldsValue({ name: '', @@ -274,13 +275,12 @@ const WorkSubmit: FC = ({ } // 作品提交或者编辑 const handleFormFinish = async (values: any) => { - - setFormButLoading(true); - let res: any; if (modelType == 2 && fileList.length == 0) { message.error("请选择文件"); return; } + setFormButLoading(true); + let res: any; const Data: any = { login: userinfo?.login, container_type: "Competition", @@ -303,9 +303,7 @@ const WorkSubmit: FC = ({ } } ) - setFormButLoading(false); - - if (res?.status != 0) return + if (res?.status != 0) { setFormButLoading(false); return } Data["name"] = values.name; Data["url"] = values.url; } @@ -344,10 +342,9 @@ const WorkSubmit: FC = ({ identifier: identifier, stage_id: gameItem?.id, page: 1, - per_page: 9999999, // TODO :查询所有列表数据 + limit: 9999999, // TODO :查询所有列表数据 keyword: "", module_type: 'worksubmit', - } }) const DownloadList = res?.results?.filter((item: any) => item?.result_url && item.file_name) @@ -361,23 +358,37 @@ const WorkSubmit: FC = ({ } }); const nameLsit: string[] = []; + // 下载所有文件时打包成zip文件 + // let zipAll = new JSZip(); + let maxSize = 800 * 1024 * 1024; // 900MB的最大限制 + let currentSize = 0; // 当前压缩包的大小 + let currentBatch = 0; const addzip = async (data: any, progress: number) => { try { const zip = new JSZip(); const response = await fetch(data.result_url, { method: "get" }); const blob = await response.blob(); zip.file(data.file_name, blob); - await zip.generateAsync({ type: "blob" }).then((blob) => { - const getNameFile = (name: string, index: number = 0) => { - if ((index == 0 && nameLsit.includes(name)) || nameLsit.includes(`${name}(${index})`)) { - getNameFile(name, index + 1); - } else { - nameLsit.push(index == 0 ? name : `${name}(${index})`) - zipAll.file(index == 0 ? name + ".zip" : `${name}(${index})` + ".zip", blob); - } + const fileBlob = await zip.generateAsync({ type: "blob" }) + const fileSize = blob.size; + if (currentSize + fileSize > maxSize) { + await zipAll.current.generateAsync({ type: "blob" }).then((blob) => { + downLoadLink(`${HeaderDetail.name}-batch${currentBatch + 1}`, window.URL.createObjectURL(blob)); + }); + zipAll.current = new JSZip(); + currentSize = 0; + currentBatch++; + } + const getNameFile = (name: string, index: number = 0) => { + if ((index == 0 && nameLsit.includes(name)) || nameLsit.includes(`${name}(${index})`)) { + getNameFile(name, index + 1); + } else { + nameLsit.push(index == 0 ? name : `${name}(${index})`) + zipAll.current.file(index == 0 ? name + ".zip" : `${name}(${index})` + ".zip", fileBlob); + currentSize += fileSize; // 更新当前批次的大小 } - getNameFile(`${data?.team_name}-${data?.user_name}`); - }) + } + getNameFile(`${data?.team_name}-${data?.user_name}`); modal.update({ content:
当前进度: {progress}/{DownloadList.length}
, }) @@ -385,16 +396,24 @@ const WorkSubmit: FC = ({ message.error(`${data.file_name}下载失败`); } } - if (DownloadList.length > 0) { + const processBatch = async () => { let i = 0; - for (let item of DownloadList) { + for (const item of DownloadList) { await addzip(item, ++i); } - zipAll.generateAsync({ type: "blob" }).then((blob) => { - downLoadLink(HeaderDetail.name, window.URL.createObjectURL(blob)); - }) + if (currentSize > 0) { + await zipAll.current.generateAsync({ type: "blob" }).then((blob) => { + downLoadLink(`${HeaderDetail.name}-batch${currentBatch + 1}`, window.URL.createObjectURL(blob)); + }); + } + + modal.destroy(); // 关闭弹窗 + }; + console.log("DownloadList===", DownloadList); + + if (DownloadList.length > 0) { + processBatch(); } - modal.destroy() } const uploadProps = { maxCount: 1, @@ -419,8 +438,7 @@ const WorkSubmit: FC = ({ return (
- {gameItem ? - {!identity && StaffDetail.enrolled && } @@ -445,8 +463,7 @@ const WorkSubmit: FC = ({ {item.children?.map((ChildItem: any) => )} } )} - : -
+ :
{(StaffDetail.enrolled || identity) && {!identity && StaffDetail.enrolled &&