refactor: 抽取金蝶附件查看下载公共方法

This commit is contained in:
刘泉佳
2026-11-06 17:39:30 +08:00
parent 79e903bcd8
commit eb74466c30
+47 -20
View File
@@ -21,16 +21,17 @@ export const isKingdeePathAttachment = attachment => {
}; };
/** /**
* 请求金蝶文件流:成功返回 Blob;业务失败为 HTTP 200 + R.fail JSON(响应通道未写流时后端即失败), * 请求金蝶文件流(公共核):成功返回 Blob;业务失败为 HTTP 200 + R.fail JSON
* 按 Content-Type 识别并抛出带 msg 的错误;网络级失败拦截器已提示,标记 silent 避免重复提示。 * (响应通道未写流时后端即失败),按 Content-Type 识别并抛出带 msg 的错误;
* 网络级失败拦截器已提示,标记 silent 避免重复提示。
* 供查看(弹窗内预览)/下载两类入口复用 * 供查看(弹窗内预览)/下载两类入口复用
* @param {String} invoiceNo 发票号码 * @param {Function} fetcher 文件流请求函数(返回 axios blob 响应)
* @returns {Promise<Blob>} * @returns {Promise<Blob>}
*/ */
export const requestKingdeeFile = async invoiceNo => { const fetchKingdeeBlob = async fetcher => {
let res; let res;
try { try {
res = await downloadInvoicePoolAttachment(invoiceNo); res = await fetcher();
} catch (error) { } catch (error) {
throw Object.assign(new Error(error?.message || '附件下载失败'), { silent: true }); throw Object.assign(new Error(error?.message || '附件下载失败'), { silent: true });
} }
@@ -51,6 +52,45 @@ export const requestKingdeeFile = async invoiceNo => {
return blob; return blob;
}; };
/**
* 请求金蝶文件流:票池发票按发票号回源(后端代理 A801-2)
* @param {String} invoiceNo 发票号码
* @returns {Promise<Blob>}
*/
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 提示 * 下载金蝶侧发票附件(经 A801-2 后端代理),失败统一 ElMessage 提示
* @param {String} invoiceNo 发票号码 * @param {String} invoiceNo 发票号码
@@ -61,12 +101,7 @@ export const downloadKingdeeInvoiceFile = async (invoiceNo, fileName) => {
ElMessage.warning('当前发票缺少发票号码,无法下载金蝶侧附件'); ElMessage.warning('当前发票缺少发票号码,无法下载金蝶侧附件');
return; return;
} }
try { await downloadKingdeeFile(() => downloadInvoicePoolAttachment(invoiceNo), fileName || `${invoiceNo}.pdf`);
const blob = await requestKingdeeFile(invoiceNo);
downloadFile(blob, fileName || `${invoiceNo}.pdf`);
} catch (error) {
if (!error?.silent) ElMessage.error(error?.message || '金蝶附件下载失败');
}
}; };
/** /**
@@ -79,13 +114,5 @@ export const viewKingdeeInvoiceFile = async (invoiceNo, fileName) => {
ElMessage.warning('当前发票缺少发票号码,无法预览金蝶侧附件'); ElMessage.warning('当前发票缺少发票号码,无法预览金蝶侧附件');
return; return;
} }
try { await viewKingdeeFile(() => downloadInvoicePoolAttachment(invoiceNo), fileName || `${invoiceNo}.pdf`);
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 || '金蝶附件预览失败');
}
}; };