优化身份证读取逻辑,简化代码并清理不必要的注释

This commit is contained in:
yuchenglong
2025-11-28 18:01:26 +08:00
parent 66e01c3446
commit 25fdb36dc6

View File

@@ -19,16 +19,13 @@ const U1: React.FC = () => {
// 实时监听身份证读取 // 实时监听身份证读取
useEffect(() => { useEffect(() => {
// 启动后端监听
window.electronAPI.startIdCardListen().catch((e: any) => { window.electronAPI.startIdCardListen().catch((e: any) => {
console.error("start_idcard_listen failed", e); console.error("start_idcard_listen failed", e);
window.electronAPI.log("error", `start_idcard_listen failed: ${e}`); window.electronAPI.log("error", `start_idcard_listen failed: ${e}`);
}); });
// 监听数据
window.electronAPI.onIdCardData((e: any) => { window.electronAPI.onIdCardData((e: any) => {
const payload = e.payload; const payload = e.payload;
// 保存到 localStorage下一次刷卡时覆盖
try { try {
if (payload?.id_card_no) { if (payload?.id_card_no) {
localStorage.setItem("lastIdCardNo", payload.id_card_no); localStorage.setItem("lastIdCardNo", payload.id_card_no);
@@ -43,8 +40,7 @@ const U1: React.FC = () => {
`Read IDCard success: ${payload.name} ${payload.id_card_no}` `Read IDCard success: ${payload.name} ${payload.id_card_no}`
); );
// 无论是否点击了开始签到,只要读到卡跳转 // 读到卡跳转;清理倒计时
// 清理定时器
if (timerRef.current) { if (timerRef.current) {
clearTimeout(timerRef.current); clearTimeout(timerRef.current);
timerRef.current = null; timerRef.current = null;
@@ -53,22 +49,14 @@ const U1: React.FC = () => {
clearInterval(intervalRef.current); clearInterval(intervalRef.current);
intervalRef.current = null; intervalRef.current = null;
} }
// 停止监听(跳转后不再需要监听)
window.electronAPI.stopIdCardListen().catch(() => {}); window.electronAPI.stopIdCardListen().catch(() => {});
setReading(false); setReading(false);
navigate("/u2"); navigate("/u2");
}); });
// 监听错误 (可选)
window.electronAPI.onIdCardError((e: any) => { window.electronAPI.onIdCardError((e: any) => {
console.error("[idcard-error]", e.payload); console.error("[idcard-error]", e.payload);
window.electronAPI.log("error", `[idcard-error] ${e.payload}`); window.electronAPI.log("error", `[idcard-error] ${e.payload}`);
// 错误不停止监听,继续等待下一次读取,除非是严重错误?
// 这里仅记录日志,或者在 reading 状态下提示用户
if (reading) {
// 如果正在倒计时中出错,可以选择停止倒计时或者忽略
// 目前保持监听
}
}); });
return () => { return () => {
@@ -77,7 +65,7 @@ const U1: React.FC = () => {
if (timerRef.current) clearTimeout(timerRef.current); if (timerRef.current) clearTimeout(timerRef.current);
if (intervalRef.current) clearInterval(intervalRef.current); if (intervalRef.current) clearInterval(intervalRef.current);
}; };
}, [navigate, reading]); }, [navigate]);
const handleStart = () => { const handleStart = () => {
if (reading) return; // 避免重复点击 if (reading) return; // 避免重复点击