ka-nfc-page.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. (function () {
  2. var reader = new ReaderClient();
  3. var pollingTimer = null;
  4. var isReading = false;
  5. var isConnecting = false;
  6. var lastUid = "";
  7. function getEl(id) {
  8. return document.getElementById(id);
  9. }
  10. function getValue(id, fallback) {
  11. var el = getEl(id);
  12. if (!el || el.value == null || el.value === "") {
  13. return fallback;
  14. }
  15. return String(el.value).trim();
  16. }
  17. function setStatus(text, color) {
  18. var statusEl = getEl("readerStatusTag");
  19. if (!statusEl) return;
  20. statusEl.textContent = text;
  21. statusEl.style.color = color || "#64748b";
  22. }
  23. function showConnectButton(show) {
  24. var button = getEl("btnConnect");
  25. if (!button) return;
  26. button.style.display = show ? "inline-flex" : "none";
  27. }
  28. function normalizeHexKey(value) {
  29. return String(value || "")
  30. .trim()
  31. .toUpperCase()
  32. .replace(/[^0-9A-F]/g, "");
  33. }
  34. function applyReaderConfig() {
  35. reader.wsUrl = getValue("wsUrlText", "ws://127.0.0.1:6689");
  36. reader.keyType = getValue("keyTypeInput", "0");
  37. reader.key = normalizeHexKey(getValue("keyInput", "FFFFFFFFFFFF"));
  38. reader.blockAddress = getValue("blockAddressInput", "1");
  39. reader.numberOfBlocksRead = getValue("numberOfBlocksReadInput", "1");
  40. reader.readDataType = getValue("readDataTypeInput", "0");
  41. reader.readSecurityStatus = getValue("readSecurityStatusInput", "0");
  42. reader.iso15693BlockAddress = getValue("iso15693BlockAddressInput", "0");
  43. reader.iso15693NumberOfBlocks = getValue("iso15693NumberOfBlocksInput", "7");
  44. }
  45. function setNfcValue(uid) {
  46. var input = getEl("nfch");
  47. if (!input) return;
  48. input.value = uid;
  49. input.dispatchEvent(new Event("input", { bubbles: true }));
  50. input.dispatchEvent(new Event("change", { bubbles: true }));
  51. }
  52. function stopPolling() {
  53. if (pollingTimer) {
  54. clearInterval(pollingTimer);
  55. pollingTimer = null;
  56. }
  57. isReading = false;
  58. }
  59. function handleDisconnect(message) {
  60. stopPolling();
  61. reader.connected = false;
  62. reader.handle = null;
  63. setStatus(message || "读卡器未连接", "#ef4444");
  64. showConnectButton(true);
  65. }
  66. async function readUidBothProtocols() {
  67. var protocols = ["ISO14443A", "ISO15693"];
  68. for (var i = 0; i < protocols.length; i++) {
  69. try {
  70. var uid = await reader.readUid(protocols[i]);
  71. if (uid) {
  72. return { uid: uid, protocol: protocols[i] };
  73. }
  74. } catch (err) {
  75. var msg = err && err.message ? err.message : String(err || "");
  76. if (
  77. msg.indexOf("未读取到 UID") === -1 &&
  78. msg.indexOf("标签已到读卡位") === -1 &&
  79. msg.indexOf("卡片已到读卡位") === -1
  80. ) {
  81. throw err;
  82. }
  83. }
  84. }
  85. return null;
  86. }
  87. async function pollOnce() {
  88. if (isReading || !reader.connected) {
  89. return;
  90. }
  91. isReading = true;
  92. try {
  93. applyReaderConfig();
  94. var result = await readUidBothProtocols();
  95. if (result && result.uid) {
  96. if (result.uid !== lastUid) {
  97. lastUid = result.uid;
  98. }
  99. setNfcValue(result.uid);
  100. }
  101. } catch (err) {
  102. var msg = err && err.message ? err.message : "读卡失败";
  103. if (msg.indexOf("连接") !== -1 || msg.indexOf("断开") !== -1 || msg.indexOf("超时") !== -1) {
  104. handleDisconnect("读卡器连接已断开");
  105. }
  106. } finally {
  107. isReading = false;
  108. }
  109. }
  110. function startPolling() {
  111. if (pollingTimer || !reader.connected) {
  112. return;
  113. }
  114. var interval = parseInt(getValue("pollingInterval", "1000"), 10);
  115. if (!interval || interval < 100) {
  116. interval = 1000;
  117. }
  118. pollOnce();
  119. pollingTimer = setInterval(pollOnce, interval);
  120. }
  121. async function connectReader(isManual) {
  122. if (isConnecting) {
  123. return;
  124. }
  125. isConnecting = true;
  126. setStatus("读卡器连接中...", "#64748b");
  127. showConnectButton(false);
  128. try {
  129. applyReaderConfig();
  130. await reader.selectDevice();
  131. setStatus("读卡器连接成功", "#16a34a");
  132. showConnectButton(false);
  133. startPolling();
  134. } catch (err) {
  135. stopPolling();
  136. var msg = err && err.message ? err.message : "读卡器连接失败";
  137. setStatus(msg, "#ef4444");
  138. showConnectButton(true);
  139. if (isManual) {
  140. alert(msg);
  141. }
  142. } finally {
  143. isConnecting = false;
  144. }
  145. }
  146. function bindEvents() {
  147. var button = getEl("btnConnect");
  148. if (button) {
  149. button.addEventListener("click", function () {
  150. connectReader(true);
  151. });
  152. }
  153. }
  154. function init() {
  155. bindEvents();
  156. setStatus("读卡器连接中...", "#64748b");
  157. showConnectButton(false);
  158. connectReader(false);
  159. }
  160. window.addEventListener("load", init);
  161. })();