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`); };