添加新中新二代证读取器支持
This commit is contained in:
@@ -19,45 +19,45 @@ const U1: React.FC = () => {
|
||||
if (reading) return; // 避免重复点击
|
||||
setReading(true);
|
||||
// 启动后端监听
|
||||
// invoke("start_idcard_listen").catch((e) => {
|
||||
// console.error("start_idcard_listen failed", e);
|
||||
// });
|
||||
window.electronAPI.startIdCardListen().catch((e: any) => {
|
||||
console.error("start_idcard_listen failed", e);
|
||||
});
|
||||
// 6 秒超时恢复
|
||||
timerRef.current = window.setTimeout(() => {
|
||||
if (reading) {
|
||||
console.warn("未在 6 秒内读取到身份证信息,恢复初始状态");
|
||||
setReading(false);
|
||||
// invoke("stop_idcard_listen").catch(() => {});
|
||||
window.electronAPI.stopIdCardListen().catch(() => {});
|
||||
}
|
||||
}, 6000);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!reading) return;
|
||||
// let unlisten: (() => void) | null = null;
|
||||
// (async () => {
|
||||
// try {
|
||||
// const off = await listen("idcard-data", (e) => {
|
||||
// const payload: any = e.payload;
|
||||
// console.log("[idcard-data]", payload);
|
||||
// // 成功:清理定时器,停止监听,跳转并传递身份证号
|
||||
// if (timerRef.current) {
|
||||
// clearTimeout(timerRef.current);
|
||||
// timerRef.current = null;
|
||||
// }
|
||||
// invoke("stop_idcard_listen").catch(() => {});
|
||||
// setReading(false);
|
||||
// navigate("/u2", {
|
||||
// state: { idCardNo: payload?.id_card_no, cardData: payload },
|
||||
// });
|
||||
// });
|
||||
// unlisten = off;
|
||||
// } catch (err) {
|
||||
// console.error("listen idcard-data failed", err);
|
||||
// }
|
||||
// })();
|
||||
|
||||
// 监听数据
|
||||
window.electronAPI.onIdCardData((e: any) => {
|
||||
const payload = e.payload;
|
||||
console.log("[idcard-data]", payload);
|
||||
// 成功:清理定时器,停止监听,跳转并传递身份证号
|
||||
if (timerRef.current) {
|
||||
clearTimeout(timerRef.current);
|
||||
timerRef.current = null;
|
||||
}
|
||||
window.electronAPI.stopIdCardListen().catch(() => {});
|
||||
setReading(false);
|
||||
navigate("/u2", {
|
||||
state: { idCardNo: payload?.id_card_no, cardData: payload },
|
||||
});
|
||||
});
|
||||
|
||||
// 监听错误 (可选)
|
||||
window.electronAPI.onIdCardError((e: any) => {
|
||||
console.error("[idcard-error]", e.payload);
|
||||
});
|
||||
|
||||
return () => {
|
||||
// if (unlisten) unlisten();
|
||||
window.electronAPI.removeIdCardListeners();
|
||||
};
|
||||
}, [reading, navigate]);
|
||||
|
||||
@@ -66,7 +66,7 @@ const U1: React.FC = () => {
|
||||
// 页面卸载时清理
|
||||
if (timerRef.current) clearTimeout(timerRef.current);
|
||||
timerRef.current = null;
|
||||
// if (reading) invoke("stop_idcard_listen").catch(() => {});
|
||||
if (reading) window.electronAPI.stopIdCardListen().catch(() => {});
|
||||
};
|
||||
}, [reading]);
|
||||
|
||||
|
||||
10
src/vite-env.d.ts
vendored
10
src/vite-env.d.ts
vendored
@@ -1 +1,11 @@
|
||||
/// <reference types="vite/client" />
|
||||
|
||||
interface Window {
|
||||
electronAPI: {
|
||||
startIdCardListen: () => Promise<any>;
|
||||
stopIdCardListen: () => Promise<any>;
|
||||
onIdCardData: (callback: (data: any) => void) => void;
|
||||
onIdCardError: (callback: (error: any) => void) => void;
|
||||
removeIdCardListeners: () => void;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user