This commit is contained in:
xianyi
2025-11-28 18:17:36 +08:00

View File

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