ka-nfc-page.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. (function () {
  2. var reader = new ReaderClient();
  3. var pollingTimer = null;
  4. var isReading = false;
  5. var isConnecting = false;
  6. var lastUid = "";
  7. var seenUids = new Map();
  8. function getEl(id) {
  9. return document.getElementById(id);
  10. }
  11. function getValue(id, fallback) {
  12. var el = getEl(id);
  13. if (!el || el.value == null || el.value === "") {
  14. return fallback;
  15. }
  16. return String(el.value).trim();
  17. }
  18. function setStatus(text, color) {
  19. var statusEl = getEl("readerStatusTag");
  20. if (!statusEl) return;
  21. statusEl.textContent = text;
  22. statusEl.style.color = color || "#64748b";
  23. }
  24. function showConnectButton(show) {
  25. var button = getEl("btnConnect");
  26. if (!button) return;
  27. button.style.display = show ? "inline-flex" : "none";
  28. }
  29. function normalizeHexKey(value) {
  30. return String(value || "")
  31. .trim()
  32. .toUpperCase()
  33. .replace(/[^0-9A-F]/g, "");
  34. }
  35. function applyReaderConfig() {
  36. reader.wsUrl = getValue("wsUrlText", "ws://127.0.0.1:6689");
  37. reader.keyType = getValue("keyTypeInput", "0");
  38. reader.key = normalizeHexKey(getValue("keyInput", "FFFFFFFFFFFF"));
  39. reader.blockAddress = getValue("blockAddressInput", "1");
  40. reader.numberOfBlocksRead = getValue("numberOfBlocksReadInput", "1");
  41. reader.readDataType = getValue("readDataTypeInput", "0");
  42. reader.readSecurityStatus = getValue("readSecurityStatusInput", "0");
  43. reader.iso15693BlockAddress = getValue("iso15693BlockAddressInput", "0");
  44. reader.iso15693NumberOfBlocks = getValue("iso15693NumberOfBlocksInput", "7");
  45. }
  46. function getDedupeMode() {
  47. return getValue("dedupeMode", "session");
  48. }
  49. function getDedupeTimeMs() {
  50. var raw = getValue("dedupeTime", "5000");
  51. var timeMs = parseInt(raw, 10);
  52. if (!timeMs || timeMs < 0) {
  53. return 5000;
  54. }
  55. return timeMs;
  56. }
  57. function clearDedupeHistory() {
  58. seenUids.clear();
  59. lastUid = "";
  60. }
  61. function checkDuplicate(uid) {
  62. var dedupeMode = getDedupeMode();
  63. if (dedupeMode === "none") {
  64. return { isDup: false };
  65. }
  66. if (dedupeMode === "session") {
  67. if (seenUids.has(uid)) {
  68. return { isDup: true };
  69. }
  70. return { isDup: false };
  71. }
  72. if (dedupeMode === "time") {
  73. var info = seenUids.get(uid);
  74. var dedupeTimeMs = getDedupeTimeMs();
  75. if (info && Date.now() - info.time < dedupeTimeMs) {
  76. return { isDup: true };
  77. }
  78. return { isDup: false };
  79. }
  80. return { isDup: false };
  81. }
  82. function recordUid(uid, protocol) {
  83. seenUids.set(uid, { protocol: protocol, time: Date.now() });
  84. }
  85. // 功能说明:通过桥接 API 写入 Vue data,兼容 ss-inp 组件并触发联动查询 by xu 20260416
  86. function setNfcValue(uid) {
  87. if (typeof window.ss && typeof window.ss.dom && typeof window.ss.dom.set === "function") {
  88. window.ss.dom.set('kah', uid);
  89. }
  90. if (typeof window.selBaseInfoByKah === "function") {
  91. window.selBaseInfoByKah(uid);
  92. }
  93. }
  94. function stopPolling() {
  95. if (pollingTimer) {
  96. clearInterval(pollingTimer);
  97. pollingTimer = null;
  98. }
  99. isReading = false;
  100. }
  101. function handleDisconnect(message) {
  102. stopPolling();
  103. clearDedupeHistory();
  104. reader.connected = false;
  105. reader.handle = null;
  106. setStatus(message || "读卡器未连接", "#ef4444");
  107. showConnectButton(true);
  108. }
  109. async function readUidBothProtocols() {
  110. var protocols = ["ISO14443A", "ISO15693"];
  111. for (var i = 0; i < protocols.length; i++) {
  112. try {
  113. var uid = await reader.readUid(protocols[i]);
  114. if (uid) {
  115. return { uid: uid, protocol: protocols[i] };
  116. }
  117. } catch (err) {
  118. var msg = err && err.message ? err.message : String(err || "");
  119. if (
  120. msg.indexOf("未读取到 UID") === -1 &&
  121. msg.indexOf("标签已到读卡位") === -1 &&
  122. msg.indexOf("卡片已到读卡位") === -1
  123. ) {
  124. throw err;
  125. }
  126. }
  127. }
  128. return null;
  129. }
  130. async function pollOnce() {
  131. if (isReading || !reader.connected) {
  132. return;
  133. }
  134. isReading = true;
  135. try {
  136. applyReaderConfig();
  137. var result = await readUidBothProtocols();
  138. if (result && result.uid) {
  139. var dupCheck = checkDuplicate(result.uid);
  140. if (dupCheck.isDup) {
  141. return;
  142. }
  143. recordUid(result.uid, result.protocol);
  144. lastUid = result.uid;
  145. setNfcValue(result.uid);
  146. }
  147. } catch (err) {
  148. var msg = err && err.message ? err.message : "读卡失败";
  149. if (msg.indexOf("连接") !== -1 || msg.indexOf("断开") !== -1 || msg.indexOf("超时") !== -1) {
  150. handleDisconnect("读卡器连接已断开");
  151. }
  152. } finally {
  153. isReading = false;
  154. }
  155. }
  156. function startPolling() {
  157. if (pollingTimer || !reader.connected) {
  158. return;
  159. }
  160. var interval = parseInt(getValue("pollingInterval", "1000"), 10);
  161. if (!interval || interval < 100) {
  162. interval = 1000;
  163. }
  164. pollOnce();
  165. pollingTimer = setInterval(pollOnce, interval);
  166. }
  167. async function connectReader(isManual) {
  168. if (isConnecting) {
  169. return;
  170. }
  171. isConnecting = true;
  172. setStatus("读卡器连接中...", "#64748b");
  173. showConnectButton(false);
  174. try {
  175. applyReaderConfig();
  176. clearDedupeHistory();
  177. await reader.selectDevice();
  178. setStatus("读卡器连接成功", "#16a34a");
  179. showConnectButton(false);
  180. startPolling();
  181. } catch (err) {
  182. stopPolling();
  183. var msg = err && err.message ? err.message : "读卡器连接失败";
  184. setStatus(msg, "#ef4444");
  185. showConnectButton(true);
  186. if (isManual) {
  187. alert(msg);
  188. }
  189. } finally {
  190. isConnecting = false;
  191. }
  192. }
  193. function bindEvents() {
  194. var button = getEl("btnConnect");
  195. if (button) {
  196. button.addEventListener("click", function () {
  197. connectReader(true);
  198. });
  199. }
  200. var dedupeModeEl = getEl("dedupeMode");
  201. if (dedupeModeEl) {
  202. dedupeModeEl.addEventListener("change", function () {
  203. clearDedupeHistory();
  204. });
  205. }
  206. }
  207. function init() {
  208. bindEvents();
  209. setStatus("读卡器连接中...", "#64748b");
  210. showConnectButton(false);
  211. connectReader(false);
  212. }
  213. window.addEventListener("load", init);
  214. })();