优化签字判断&日志

This commit is contained in:
xianyi
2025-12-29 11:03:35 +08:00
parent abb71c7d46
commit c42cb6dd50

View File

@@ -142,19 +142,22 @@ const UI7: React.FC = () => {
hasFetchedRef.current = true;
const fetchPdfs = async () => {
try {
const res = await getTongyishuPdf(
Number(localStorage.getItem("selectedExamId"))
);
if (res.Status !== 200) {
// alert(`获取PDF数据失败: ${res.Message}`);
window.electronAPI?.log("error", `获取PDF数据失败: ${res.Message}`);
const examId = Number(localStorage.getItem("selectedExamId"));
window.electronAPI?.log("info", `[UI7] 开始获取知情同意书PDF列表exam_id=${examId}`);
try {
const res = await getTongyishuPdf(examId);
window.electronAPI?.log("info", `[UI7] 获取PDF列表响应: Status=${res.Status}, Message=${res.Message || "null"}`);
if (res.Status !== 200) {
window.electronAPI?.log("error", `[UI7] 获取PDF数据失败: ${res.Message}`);
setLoadMessage(res.Message || "获取PDF数据失败");
return;
}
const rawList = res.Data?.list_pdf_url || [];
window.electronAPI?.log("info", `[UI7] 原始PDF列表数量: ${rawList.length}`);
const normalizedList = rawList
.map((item, idx): PdfMeta => {
if (typeof item === "string") {
@@ -189,6 +192,14 @@ const UI7: React.FC = () => {
typeof item.pdf_url === "string" && item.pdf_url.length > 0
);
window.electronAPI?.log("info", `[UI7] 规范化后PDF列表数量: ${normalizedList.length}`);
normalizedList.forEach((item, idx) => {
window.electronAPI?.log(
"info",
`[UI7] PDF[${idx}]: name="${item.pdf_name}", combination_code=${item.combination_code || "null"}, url=${item.pdf_url.substring(0, 80)}...`
);
});
if (!normalizedList.length) {
throw new Error("未获取到任何 PDF 链接");
}
@@ -197,23 +208,33 @@ const UI7: React.FC = () => {
setError("");
setPdfFiles([]);
setPdfInfoList([]);
window.electronAPI?.log("info", "[UI7] 开始获取 PDF 文件");
window.electronAPI?.log("info", `[UI7] 开始下载 ${normalizedList.length} PDF 文件`);
const files: Uint8Array[] = [];
for (let idx = 0; idx < normalizedList.length; idx++) {
const { pdf_url: url } = normalizedList[idx];
const { pdf_url: url, pdf_name: name } = normalizedList[idx];
window.electronAPI?.log(
"info",
`[UI7] 下载第 ${idx + 1} 份 PDF: ${url}`
`[UI7] 下载第 ${idx + 1}/${normalizedList.length} 份 PDF: ${name}`
);
const startTime = Date.now();
const pdfBytes = await fetchPdfBytes(url);
const duration = Date.now() - startTime;
files.push(pdfBytes);
window.electronAPI?.log(
"info",
`[UI7] 第 ${idx + 1} 份 PDF 下载完成,大小: ${pdfBytes.length} bytes耗时: ${duration}ms`
);
}
setPdfFiles(files);
setPdfInfoList(normalizedList);
setLoading(false);
setError("");
window.electronAPI?.log(
"info",
`[UI7] PDF 获取完成,共 ${normalizedList.length} 份,总签名步骤: ${normalizedList.length + 1} (导检单 + ${normalizedList.length} 份知情同意书)`
);
} catch (err) {
const errorMsg = `PDF获取失败: ${err}`;
console.error(errorMsg);
@@ -227,8 +248,10 @@ const UI7: React.FC = () => {
}, []);
useEffect(() => {
window.electronAPI?.log("info", "[UI7] 初始化:清理旧的签名缓存数据");
localStorage.removeItem("consentSignatureList");
localStorage.removeItem("tongyishuSignedPdfUrls");
window.electronAPI?.log("info", "[UI7] 初始化完成:签名缓存已清理");
}, []);
useEffect(() => {
@@ -244,10 +267,26 @@ const UI7: React.FC = () => {
useEffect(() => {
setCurrentPdfPageCount(0);
}, [currentStep, currentPdfFile]);
if (currentStep > 0) {
const meta = pdfInfoList[currentStep - 1];
window.electronAPI?.log(
"info",
`[UI7] 步骤切换: currentStep=${currentStep}, 当前PDF: ${meta?.pdf_name || "未知"}`
);
} else {
window.electronAPI?.log(
"info",
`[UI7] 步骤切换: currentStep=${currentStep} (导检单说明页)`
);
}
}, [currentStep, currentPdfFile, pdfInfoList]);
useEffect(() => {
setHasSignature(false);
window.electronAPI?.log(
"info",
`[UI7] 步骤 ${currentStep} 签名状态已重置`
);
}, [currentStep]);
// 检测标题文本是否超过一行,如果超过则应用缩小样式
@@ -446,27 +485,46 @@ const UI7: React.FC = () => {
const handleConfirm = useCallback(() => {
const execute = async () => {
if (isSubmitting) {
window.electronAPI?.log("warn", "[UI7] 检测到重复提交,已忽略");
return;
}
const canvas = canvasRef.current;
if (!canvas) {
window.electronAPI?.log("error", "[UI7] 画布初始化失败");
alert("画布初始化失败,请刷新页面");
return;
}
if (isCanvasBlank()) {
window.electronAPI?.log("warn", "[UI7] 画布为空,无法提交");
alert("请先完成签名后再继续");
return;
}
const examIdStr = localStorage.getItem("selectedExamId");
const examId = Number(examIdStr);
if (!examIdStr || Number.isNaN(examId)) {
window.electronAPI?.log("error", `[UI7] 体检ID无效: ${examIdStr}`);
alert("未找到体检ID请返回重试");
return;
}
const totalSteps = pdfInfoList.length + 1;
const currentStepNum = currentStep + 1;
window.electronAPI?.log(
"info",
`[UI7] ========== 开始提交签名 ==========`
);
window.electronAPI?.log(
"info",
`[UI7] 当前步骤: ${currentStepNum}/${totalSteps}, currentStep=${currentStep}, pdfInfoList.length=${pdfInfoList.length}`
);
setIsSubmitting(true);
try {
const blob = await canvasToBlob(canvas);
window.electronAPI?.log(
"info",
`[UI7] 签名图片生成成功,大小: ${blob.size} bytes`
);
// 本地缓存成列表,方便 UI8 或调试使用
try {
@@ -475,6 +533,10 @@ const UI7: React.FC = () => {
const storedList: string[] = storedRaw ? JSON.parse(storedRaw) : [];
storedList[currentStep] = dataUrl;
localStorage.setItem("consentSignatureList", JSON.stringify(storedList));
window.electronAPI?.log(
"info",
`[UI7] 签名已缓存到 localStorage[${currentStep}]`
);
} catch (cacheErr) {
window.electronAPI?.log(
"warn",
@@ -485,30 +547,68 @@ const UI7: React.FC = () => {
if (currentStep === 0) {
window.electronAPI?.log(
"info",
`[UI7] 提交导检单签名(第 1 次exam_id=${examId}`
`[UI7] >>> 提交导检单签名(步骤 ${currentStepNum}/${totalSteps}exam_id=${examId}`
);
const startTime = Date.now();
const daojiandanRes = await submitDaojiandanSign(examId, blob);
const duration = Date.now() - startTime;
window.electronAPI?.log(
"info",
`[UI7] 导检单签名响应: Status=${daojiandanRes.Status}, Message=${daojiandanRes.Message || "null"}, 耗时: ${duration}ms`
);
if (daojiandanRes.Status !== 200) {
throw new Error(daojiandanRes.Message || "提交导检单签名失败");
}
localStorage.setItem("consentSignature", daojiandanRes.Data.pdf_url || "");
} else {
const meta = pdfInfoList[currentStep - 1];
if (!meta || meta.combination_code == null) {
throw new Error("当前知情同意书缺少组合代码,无法提交签名");
}
const pdfUrl = daojiandanRes.Data.pdf_url || "";
localStorage.setItem("consentSignature", pdfUrl);
window.electronAPI?.log(
"info",
`[UI7] 提交知情同意书签名exam_id=${examId}, combination_code=${meta.combination_code}`
`[UI7] 导检单签名成功PDF URL已保存: ${pdfUrl.substring(0, 80)}...`
);
} else {
const meta = pdfInfoList[currentStep - 1];
if (!meta) {
window.electronAPI?.log(
"error",
`[UI7] 无法找到当前PDF元信息currentStep=${currentStep}, pdfInfoList.length=${pdfInfoList.length}`
);
throw new Error("当前PDF信息缺失无法提交签名");
}
if (meta.combination_code == null) {
window.electronAPI?.log(
"error",
`[UI7] 当前知情同意书缺少组合代码: pdf_name="${meta.pdf_name}", pdf_url=${meta.pdf_url.substring(0, 80)}...`
);
throw new Error("当前知情同意书缺少组合代码,无法提交签名");
}
window.electronAPI?.log(
"info",
`[UI7] >>> 提交知情同意书签名(步骤 ${currentStepNum}/${totalSteps}`
);
window.electronAPI?.log(
"info",
`[UI7] PDF信息: name="${meta.pdf_name}", combination_code=${meta.combination_code}, exam_id=${examId}`
);
const startTime = Date.now();
const tongyishuRes = await submitTongyishuSign(
examId,
meta.combination_code,
blob
);
const duration = Date.now() - startTime;
window.electronAPI?.log(
"info",
`[UI7] 知情同意书签名响应: Status=${tongyishuRes.Status}, Message=${tongyishuRes.Message || "null"}, 耗时: ${duration}ms`
);
if (tongyishuRes.Status !== 200) {
throw new Error(tongyishuRes.Message || "提交知情同意书签名失败");
}
// 保存签名后的知情同意书 PDF URL可能返回多个
try {
const returnedList = Array.isArray(
@@ -516,51 +616,96 @@ const UI7: React.FC = () => {
)
? tongyishuRes.Data.list_pdf_url
: [];
window.electronAPI?.log(
"info",
`[UI7] 返回的PDF URL列表数量: ${returnedList.length}`
);
const urls = returnedList
.map((item) =>
typeof item === "string" ? item : item?.pdf_url || ""
)
.filter((url): url is string => Boolean(url));
urls.forEach((url, idx) => {
window.electronAPI?.log(
"info",
`[UI7] 返回的PDF URL[${idx}]: ${url.substring(0, 80)}...`
);
});
const storedRaw = localStorage.getItem("tongyishuSignedPdfUrls");
const storedList: string[] = storedRaw ? JSON.parse(storedRaw) : [];
window.electronAPI?.log(
"info",
`[UI7] 原有缓存的PDF URL数量: ${storedList.length}`
);
const merged = [...storedList];
let addedCount = 0;
urls.forEach((url) => {
if (!merged.includes(url)) {
merged.push(url);
addedCount++;
}
});
localStorage.setItem(
"tongyishuSignedPdfUrls",
JSON.stringify(merged)
);
window.electronAPI?.log(
"info",
`[UI7] 知情同意书PDF URL已保存新增: ${addedCount}, 总计: ${merged.length}`
);
} catch (cacheErr) {
window.electronAPI?.log(
"warn",
`[UI7] 知情同意书 PDF URL 缓存失败: ${(cacheErr as Error).message}`
);
}
}
if (currentStep >= pdfInfoList.length) {
window.electronAPI?.log(
"info",
`[UI7] 检查是否完成所有签名: currentStep=${currentStep}, pdfInfoList.length=${pdfInfoList.length}, 条件: currentStep > 0 && currentStep >= pdfInfoList.length`
);
if (currentStep > 0 && currentStep >= pdfInfoList.length) {
window.electronAPI?.log(
"info",
`[UI7] ✓ 所有签名已完成准备跳转到UI8`
);
window.electronAPI?.log(
"info",
`[UI7] 已签名: 导检单(1份) + 知情同意书(${pdfInfoList.length}份) = 共${totalSteps}`
);
sign();
navigate("/UI8");
return;
}
const nextStep = currentStep + 1;
window.electronAPI?.log(
"info",
`[UI7] 继续下一步: 从步骤 ${currentStepNum} 进入步骤 ${nextStep + 1}/${totalSteps}`
);
clearCanvas();
setCurrentStep((prev) => prev + 1);
resetCountdown();
window.electronAPI?.log(
"info",
`[UI7] 完成第 ${currentStep + 1} 次签名,进入下一份 PDF`
`[UI7] ========== 步骤 ${currentStepNum} 完成,进入步骤 ${nextStep + 1} ==========`
);
} catch (err) {
const msg = (err as Error).message || "签名提交失败,请稍后重试";
window.electronAPI?.log("error", `[UI7] ${msg}`);
window.electronAPI?.log("error", `[UI7] 签名提交失败: ${msg}`);
window.electronAPI?.log("error", `[UI7] 错误堆栈: ${(err as Error).stack || "无"}`);
alert(msg);
} finally {
setIsSubmitting(false);
window.electronAPI?.log("info", `[UI7] 提交流程结束isSubmitting=false`);
}
};
@@ -584,19 +729,36 @@ const UI7: React.FC = () => {
const sign = async () => {
const physical_exam_id = localStorage.getItem("selectedExamId");
if (!physical_exam_id) {
window.electronAPI?.log("error", "[UI7] 签到失败体检ID不存在");
alert("体检ID不存在");
return;
}
window.electronAPI?.log(
"info",
`[UI7] 开始执行签到exam_id=${physical_exam_id}`
);
const startTime = Date.now();
const res = await signIn(Number(physical_exam_id));
const duration = Date.now() - startTime;
window.electronAPI?.log(
"info",
`[UI7] 签到响应: Status=${res.Status}, Message=${res.Message || "null"}, is_success=${res.Data?.is_success}, 耗时: ${duration}ms`
);
if (res.Status === 200) {
if (res.Data.is_success === 0) {
window.electronAPI?.log("info", "[UI7] 签到成功");
return;
} else {
window.electronAPI?.log("warn", `[UI7] 签到返回异常: ${res.Message}`);
console.log(res.Data);
alert(res.Message);
}
} else {
window.electronAPI?.log("error", `[UI7] 签到失败: ${res.Message}`);
alert(res.Message);
}
};