From 22e9b74173e65aa95d2b066ad9c07fb3f493372e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=20=E5=85=83=E5=B1=B1?= Date: Mon, 26 May 2025 15:30:58 +0800 Subject: [PATCH] fix quarterlyresults --- .../quarterlyresults/size375/index.vue | 61 +++++++++++++------ .../quarterlyresults/size768/index.vue | 61 +++++++++++++------ 2 files changed, 84 insertions(+), 38 deletions(-) diff --git a/src/views/financialinformation/quarterlyresults/size375/index.vue b/src/views/financialinformation/quarterlyresults/size375/index.vue index 7dbed1d..dde03f0 100644 --- a/src/views/financialinformation/quarterlyresults/size375/index.vue +++ b/src/views/financialinformation/quarterlyresults/size375/index.vue @@ -345,29 +345,52 @@ const handleSearch = () => { console.log("搜索:", searchQuery.value); }; -const downloadPdf = async (pdfResource, filename = "") => { - try { - // 获取PDF文件 - const response = await fetch(pdfResource); - const blob = await response.blob(); +const downloadPdf = (url) => { + // 创建一个隐藏的a元素 + const link = document.createElement("a"); + link.href = url; - // 创建Blob URL - const blobUrl = URL.createObjectURL(blob); + // 修复文件名提取逻辑 + let fileName = url.split("/").pop(); + // 移除可能存在的查询参数 + if (fileName.includes("?") || fileName.includes("_t=")) { + fileName = fileName.split(/[?_]/)[0]; + } + link.download = fileName; + link.target = "_blank"; - // 创建下载链接 - const a = document.createElement("a"); - a.href = blobUrl; - a.download = filename || pdfResource.split("/").pop() || "download.pdf"; + // 对于移动设备,我们需要特殊处理 + const isMobile = + /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test( + navigator.userAgent + ); - // 触发下载 - document.body.appendChild(a); - a.click(); - document.body.removeChild(a); + if (isMobile) { + // 在移动设备上,可能需要使用fetch下载文件并创建blob + fetch(url) + .then((response) => response.blob()) + .then((blob) => { + const objectUrl = URL.createObjectURL(blob); + link.href = objectUrl; + document.body.appendChild(link); + link.click(); - // 释放Blob URL - URL.revokeObjectURL(blobUrl); - } catch (error) { - console.error("下载PDF文件失败:", error); + // 清理 + setTimeout(() => { + document.body.removeChild(link); + URL.revokeObjectURL(objectUrl); + }, 100); + }) + .catch((error) => { + console.error("下载文件时出错:", error); + // 如果fetch失败,回退到window.open + window.open(url, "_blank"); + }); + } else { + // 桌面设备上直接点击链接 + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); } }; diff --git a/src/views/financialinformation/quarterlyresults/size768/index.vue b/src/views/financialinformation/quarterlyresults/size768/index.vue index 2d3e01c..8a512fa 100644 --- a/src/views/financialinformation/quarterlyresults/size768/index.vue +++ b/src/views/financialinformation/quarterlyresults/size768/index.vue @@ -345,29 +345,52 @@ const handleSearch = () => { console.log("搜索:", searchQuery.value); }; -const downloadPdf = async (pdfResource, filename = "") => { - try { - // 获取PDF文件 - const response = await fetch(pdfResource); - const blob = await response.blob(); +const downloadPdf = (url) => { + // 创建一个隐藏的a元素 + const link = document.createElement("a"); + link.href = url; - // 创建Blob URL - const blobUrl = URL.createObjectURL(blob); + // 修复文件名提取逻辑 + let fileName = url.split("/").pop(); + // 移除可能存在的查询参数 + if (fileName.includes("?") || fileName.includes("_t=")) { + fileName = fileName.split(/[?_]/)[0]; + } + link.download = fileName; + link.target = "_blank"; - // 创建下载链接 - const a = document.createElement("a"); - a.href = blobUrl; - a.download = filename || pdfResource.split("/").pop() || "download.pdf"; + // 对于移动设备,我们需要特殊处理 + const isMobile = + /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test( + navigator.userAgent + ); - // 触发下载 - document.body.appendChild(a); - a.click(); - document.body.removeChild(a); + if (isMobile) { + // 在移动设备上,可能需要使用fetch下载文件并创建blob + fetch(url) + .then((response) => response.blob()) + .then((blob) => { + const objectUrl = URL.createObjectURL(blob); + link.href = objectUrl; + document.body.appendChild(link); + link.click(); - // 释放Blob URL - URL.revokeObjectURL(blobUrl); - } catch (error) { - console.error("下载PDF文件失败:", error); + // 清理 + setTimeout(() => { + document.body.removeChild(link); + URL.revokeObjectURL(objectUrl); + }, 100); + }) + .catch((error) => { + console.error("下载文件时出错:", error); + // 如果fetch失败,回退到window.open + window.open(url, "_blank"); + }); + } else { + // 桌面设备上直接点击链接 + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); } };