修复打印和exam_id问题

This commit is contained in:
xianyi
2025-12-03 14:33:23 +08:00
parent 408221fc66
commit 60ac086d7d
6 changed files with 420 additions and 185 deletions

View File

@@ -34,6 +34,7 @@ const UI6: React.FC = () => {
const res = await getPackagItemDetail(id_no as string);
if (res.Status === 200) {
localStorage.setItem("package_code", res.Data.packagItemInfo.package_code || "");
localStorage.setItem("selectedExamId", res.Data.packagItemInfo.physical_exam_id.toString() || "0");
// 处理数据:将 project_id 和 project_name 字符串分离为数组
const processedData = res.Data.listPackDetail.map((item: any) => {
// 将 project_id 字符串按中文顿号分割为数组

View File

@@ -291,49 +291,73 @@ const UI8: React.FC = () => {
return;
}
const primaryPdf = pdfFiles[0] || originPdfUrls[0];
if (!primaryPdf) {
if (printers.length > 0 && !selectedPrinter) {
alert("请先选择打印机");
return;
}
if (pdfFiles.length === 0 && originPdfUrls.length === 0) {
const errorMsg = "PDF 尚未加载完成,请稍候";
window.electronAPI?.log("warn", errorMsg);
alert(errorMsg);
return;
}
if (printers.length > 0 && !selectedPrinter) {
alert("请先选择打印机");
return;
}
setIsPrinting(true);
try {
const printData =
primaryPdf.startsWith("data:") && pdfFiles[0]
? primaryPdf
: originPdfUrls[0];
const dataType = printData.startsWith("data:") ? "base64数据" : "远程文件路径";
window.electronAPI.log("info", `开始打印PDF (${dataType}): ${printData.substring(0, 100)}...`);
// 优先使用已加载的 base64 数据,确保数据完整
let printData: string;
let dataType: string;
if (pdfFiles[0] && pdfFiles[0].startsWith("data:application/pdf;base64,")) {
printData = pdfFiles[0];
dataType = "base64数据";
// 验证 base64 数据长度至少应该有几KB
const base64Part = printData.replace("data:application/pdf;base64,", "");
const estimatedSize = (base64Part.length * 3) / 4;
window.electronAPI.log("info", `使用base64数据打印估算大小: ${Math.round(estimatedSize)} bytes`);
if (estimatedSize < 1024) {
window.electronAPI.log("warn", "base64数据可能不完整尝试使用原始URL");
// 如果base64数据太小回退到URL
if (originPdfUrls[0]) {
printData = originPdfUrls[0];
dataType = "远程文件路径";
}
}
} else if (originPdfUrls[0]) {
printData = originPdfUrls[0];
dataType = "远程文件路径";
} else {
throw new Error("没有可用的PDF数据");
}
window.electronAPI.log("info", `开始打印PDF (${dataType}): ${dataType === "base64数据" ? `base64长度${printData.length}` : printData.substring(0, 100)}...`);
const printResult = await window.electronAPI.printPdf(
printData,
selectedPrinter ? { printerName: selectedPrinter } : undefined
);
if (!printResult.success) {
const errorMsg = `打印失败: ${printResult.error || "未知错误"}`;
window.electronAPI.log("error", errorMsg);
alert(errorMsg);
} else {
window.electronAPI.log("info", "PDF打印请求已发送");
return;
}
window.electronAPI.log("info", "PDF打印请求已发送成功");
navigate("/UI81");
} catch (error) {
const errorMsg = `Print error: ${error}`;
const errorMsg = `打印错误: ${(error as Error).message || error}`;
console.error(errorMsg);
window.electronAPI.log("error", errorMsg);
alert("打印失败,请重试");
} finally {
setIsPrinting(false);
navigate("/UI81");
}
}, [originPdfUrls, pdfFiles, printers.length, selectedPrinter]);
}, [originPdfUrls, pdfFiles, printers.length, selectedPrinter, navigate]);
const handlePageCountUpdate = useCallback((index: number, numPages: number) => {
window.electronAPI?.log("info", `[UI8] PDF渲染成功 (index=${index}),共 ${numPages}`);