| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- (function () {
- var reader = new ReaderClient();
- var pollingTimer = null;
- var isReading = false;
- var isConnecting = false;
- var lastUid = "";
- function getEl(id) {
- return document.getElementById(id);
- }
- function getValue(id, fallback) {
- var el = getEl(id);
- if (!el || el.value == null || el.value === "") {
- return fallback;
- }
- return String(el.value).trim();
- }
- function setStatus(text, color) {
- var statusEl = getEl("readerStatusTag");
- if (!statusEl) return;
- statusEl.textContent = text;
- statusEl.style.color = color || "#64748b";
- }
- function showConnectButton(show) {
- var button = getEl("btnConnect");
- if (!button) return;
- button.style.display = show ? "inline-flex" : "none";
- }
- function normalizeHexKey(value) {
- return String(value || "")
- .trim()
- .toUpperCase()
- .replace(/[^0-9A-F]/g, "");
- }
- function applyReaderConfig() {
- reader.wsUrl = getValue("wsUrlText", "ws://127.0.0.1:6689");
- reader.keyType = getValue("keyTypeInput", "0");
- reader.key = normalizeHexKey(getValue("keyInput", "FFFFFFFFFFFF"));
- reader.blockAddress = getValue("blockAddressInput", "1");
- reader.numberOfBlocksRead = getValue("numberOfBlocksReadInput", "1");
- reader.readDataType = getValue("readDataTypeInput", "0");
- reader.readSecurityStatus = getValue("readSecurityStatusInput", "0");
- reader.iso15693BlockAddress = getValue("iso15693BlockAddressInput", "0");
- reader.iso15693NumberOfBlocks = getValue("iso15693NumberOfBlocksInput", "7");
- }
- function setNfcValue(uid) {
- var input = getEl("nfch");
- if (!input) return;
- input.value = uid;
- input.dispatchEvent(new Event("input", { bubbles: true }));
- input.dispatchEvent(new Event("change", { bubbles: true }));
- }
- function stopPolling() {
- if (pollingTimer) {
- clearInterval(pollingTimer);
- pollingTimer = null;
- }
- isReading = false;
- }
- function handleDisconnect(message) {
- stopPolling();
- reader.connected = false;
- reader.handle = null;
- setStatus(message || "读卡器未连接", "#ef4444");
- showConnectButton(true);
- }
- async function readUidBothProtocols() {
- var protocols = ["ISO14443A", "ISO15693"];
- for (var i = 0; i < protocols.length; i++) {
- try {
- var uid = await reader.readUid(protocols[i]);
- if (uid) {
- return { uid: uid, protocol: protocols[i] };
- }
- } catch (err) {
- var msg = err && err.message ? err.message : String(err || "");
- if (
- msg.indexOf("未读取到 UID") === -1 &&
- msg.indexOf("标签已到读卡位") === -1 &&
- msg.indexOf("卡片已到读卡位") === -1
- ) {
- throw err;
- }
- }
- }
- return null;
- }
- async function pollOnce() {
- if (isReading || !reader.connected) {
- return;
- }
- isReading = true;
- try {
- applyReaderConfig();
- var result = await readUidBothProtocols();
- if (result && result.uid) {
- if (result.uid !== lastUid) {
- lastUid = result.uid;
- }
- setNfcValue(result.uid);
- }
- } catch (err) {
- var msg = err && err.message ? err.message : "读卡失败";
- if (msg.indexOf("连接") !== -1 || msg.indexOf("断开") !== -1 || msg.indexOf("超时") !== -1) {
- handleDisconnect("读卡器连接已断开");
- }
- } finally {
- isReading = false;
- }
- }
- function startPolling() {
- if (pollingTimer || !reader.connected) {
- return;
- }
- var interval = parseInt(getValue("pollingInterval", "1000"), 10);
- if (!interval || interval < 100) {
- interval = 1000;
- }
- pollOnce();
- pollingTimer = setInterval(pollOnce, interval);
- }
- async function connectReader(isManual) {
- if (isConnecting) {
- return;
- }
- isConnecting = true;
- setStatus("读卡器连接中...", "#64748b");
- showConnectButton(false);
- try {
- applyReaderConfig();
- await reader.selectDevice();
- setStatus("读卡器连接成功", "#16a34a");
- showConnectButton(false);
- startPolling();
- } catch (err) {
- stopPolling();
- var msg = err && err.message ? err.message : "读卡器连接失败";
- setStatus(msg, "#ef4444");
- showConnectButton(true);
- if (isManual) {
- alert(msg);
- }
- } finally {
- isConnecting = false;
- }
- }
- function bindEvents() {
- var button = getEl("btnConnect");
- if (button) {
- button.addEventListener("click", function () {
- connectReader(true);
- });
- }
- }
- function init() {
- bindEvents();
- setStatus("读卡器连接中...", "#64748b");
- showConnectButton(false);
- connectReader(false);
- }
- window.addEventListener("load", init);
- })();
|