This commit is contained in:
oneao committed 2026-03-30 17:27:19 +08:00
1 parent f9eaacf02a
commit 69eda8b8ad
27 files changed
+2797 -616

No files matched your search

+77 -1
View File
@@ -203,7 +203,8 @@ export function getFileExtension(fileName) {
return fileExt;
}
export function downloadZip(files, fileName = "文件合集", callback) {
export function downloadOssZip(files, fileName = "文件合集", callback) {
const zip = new JSZip();
const addedFiles = new Set();
@@ -415,3 +416,78 @@ export function getAliyunFileBlob(url) {
}
// ============================================ 阿里云上传和下载 ============================================
// ============================================ 本地文件上传和下载 ==========================================
export function buildPublicUrl(filePath) {
const baseUrl = import.meta.env.PUBLIC_API_BASE_URL || "";
return joinUrl(baseUrl, "/static/file", filePath);
}
export function getFileBlob(path) {
const fullUrl = buildPublicUrl(path)
return fetch(fullUrl).then((response) => {
if (!response.ok) {
throw new Error(`获取文件失败,状态码:${response.status}`);
}
return response.blob();
});
}
export function downloadZip(files, fileName = "文件合集", callback) {
const zip = new JSZip();
const addedFiles = new Set();
const tasks = files.map((item) => {
return getFileBlob(item.mx_mapfilename)
.then((res) => {
const uniqueFileName = getUniqueFileName(item.mx_filename, addedFiles);
zip.file(uniqueFileName, res, { binary: true });
addedFiles.add(uniqueFileName);
})
.catch((err) => {
// 捕获单个文件下载失败的错误,不影响其他文件下载
console.error(`Failed to download file: ${item.mx_filename}`, err);
});
});
// 关键:使用 Promise.all 确保所有任务完成
return Promise.all(tasks)
.then(() => zip.generateAsync({ type: "blob" }))
.then((res) => {
saveAs(res, `${fileName}.zip`);
callback && callback();
})
.catch((err) => {
callback && callback();
throw err;
});
}
// ============================================ 本地文件上传和下载 ==========================================
export function joinUrl(...parts) {
if (!parts || parts.length === 0) return "";
const sb = [];
for (let part of parts) {
if (!part) continue;
part = String(part).trim();
if (!part) continue;
// 替换 Windows 路径分隔符
part = part.replace(/\\/g, "/");
if (sb.length === 0) {
// 第一段:保留协议(http:// https://),去掉末尾 /
sb.push(part.replace(/\/+$/, ""));
} else {
// 后续段:去掉前后 /
sb.push(part.replace(/^\/+|\/+$/g, ""));
}
}
return sb.join("/");
}