From eb74466c301fb1edca6c5ae2938e60a0803ac3e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B3=89=E4=BD=B3?= <1107186916@qq.com> Date: Fri, 6 Nov 2026 17:39:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8A=BD=E5=8F=96=E9=87=91?= =?UTF-8?q?=E8=9D=B6=E9=99=84=E4=BB=B6=E6=9F=A5=E7=9C=8B=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/kingdee-attachment.js | 67 +++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/src/utils/kingdee-attachment.js b/src/utils/kingdee-attachment.js index a7ac977..ab16107 100644 --- a/src/utils/kingdee-attachment.js +++ b/src/utils/kingdee-attachment.js @@ -21,16 +21,17 @@ export const isKingdeePathAttachment = attachment => { }; /** - * 请求金蝶文件流:成功返回 Blob;业务失败为 HTTP 200 + R.fail JSON(响应通道未写流时后端即失败), - * 按 Content-Type 识别并抛出带 msg 的错误;网络级失败拦截器已提示,标记 silent 避免重复提示。 + * 请求金蝶文件流(公共核):成功返回 Blob;业务失败为 HTTP 200 + R.fail JSON + * (响应通道未写流时后端即失败),按 Content-Type 识别并抛出带 msg 的错误; + * 网络级失败拦截器已提示,标记 silent 避免重复提示。 * 供查看(弹窗内预览)/下载两类入口复用 - * @param {String} invoiceNo 发票号码 + * @param {Function} fetcher 文件流请求函数(返回 axios blob 响应) * @returns {Promise} */ -export const requestKingdeeFile = async invoiceNo => { +const fetchKingdeeBlob = async fetcher => { let res; try { - res = await downloadInvoicePoolAttachment(invoiceNo); + res = await fetcher(); } catch (error) { throw Object.assign(new Error(error?.message || '附件下载失败'), { silent: true }); } @@ -51,6 +52,45 @@ export const requestKingdeeFile = async invoiceNo => { return blob; }; +/** + * 请求金蝶文件流:票池发票按发票号回源(后端代理 A801-2) + * @param {String} invoiceNo 发票号码 + * @returns {Promise} + */ +export const requestKingdeeFile = invoiceNo => fetchKingdeeBlob(() => downloadInvoicePoolAttachment(invoiceNo)); + +/** + * 下载金蝶侧附件(经 A801-2 后端代理,公共入口),失败统一 ElMessage 提示 + * @param {Function} fetcher 文件流请求函数 + * @param {String} fileName 下载文件名 + */ +export const downloadKingdeeFile = async (fetcher, fileName) => { + try { + const blob = await fetchKingdeeBlob(fetcher); + downloadFile(blob, fileName); + } catch (error) { + if (!error?.silent) ElMessage.error(error?.message || '金蝶附件下载失败'); + } +}; + +/** + * 新窗口预览金蝶侧附件(经 A801-2 后端代理,公共入口):拉取 Blob 转 objectURL + * (PDF 可预览;OFD 等浏览器不可预览的格式由浏览器触发下载) + * @param {Function} fetcher 文件流请求函数 + * @param {String} fileName 预览文件名 + */ +export const viewKingdeeFile = async (fetcher, fileName) => { + try { + const blob = await fetchKingdeeBlob(fetcher); + const url = URL.createObjectURL(blob); + window.open(url, '_blank'); + // 延迟释放 objectURL:立即回收可能导致新窗口尚未加载完成即失效 + setTimeout(() => URL.revokeObjectURL(url), 60000); + } catch (error) { + if (!error?.silent) ElMessage.error(error?.message || '金蝶附件预览失败'); + } +}; + /** * 下载金蝶侧发票附件(经 A801-2 后端代理),失败统一 ElMessage 提示 * @param {String} invoiceNo 发票号码 @@ -61,12 +101,7 @@ export const downloadKingdeeInvoiceFile = async (invoiceNo, fileName) => { ElMessage.warning('当前发票缺少发票号码,无法下载金蝶侧附件'); return; } - try { - const blob = await requestKingdeeFile(invoiceNo); - downloadFile(blob, fileName || `${invoiceNo}.pdf`); - } catch (error) { - if (!error?.silent) ElMessage.error(error?.message || '金蝶附件下载失败'); - } + await downloadKingdeeFile(() => downloadInvoicePoolAttachment(invoiceNo), fileName || `${invoiceNo}.pdf`); }; /** @@ -79,13 +114,5 @@ export const viewKingdeeInvoiceFile = async (invoiceNo, fileName) => { ElMessage.warning('当前发票缺少发票号码,无法预览金蝶侧附件'); return; } - try { - const blob = await requestKingdeeFile(invoiceNo); - const url = URL.createObjectURL(blob); - window.open(url, '_blank'); - // 延迟释放 objectURL:立即回收可能导致新窗口尚未加载完成即失效 - setTimeout(() => URL.revokeObjectURL(url), 60000); - } catch (error) { - if (!error?.silent) ElMessage.error(error?.message || '金蝶附件预览失败'); - } + await viewKingdeeFile(() => downloadInvoicePoolAttachment(invoiceNo), fileName || `${invoiceNo}.pdf`); };