ka-nfc-page.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. function getNfcInput() {
  86. return document.querySelector('input[name="kah"]');
  87. }
  88. // 功能说明:同时兼容原生 input 和 ss-inp 组件的卡号回填 by xu 20260416
  89. function setNfcValue(uid) {
  90. // 方式1:直接赋值 input(兼容普通 input)
  91. var input = getNfcInput();
  92. if (input) {
  93. input.value = uid;
  94. }
  95. // 方式2:通过桥接 API 写入 Vue data(兼容 ss-inp 组件)
  96. if (typeof window.ss && typeof window.ss.dom && typeof window.ss.dom.set === "function") {
  97. window.ss.dom.set('kah', uid);
  98. }
  99. // 触发联动查询
  100. if (typeof window.selBaseInfoByKah === "function") {
  101. window.selBaseInfoByKah(uid);
  102. }
  103. }
  104. function stopPolling() {
  105. if (pollingTimer) {
  106. clearInterval(pollingTimer);
  107. pollingTimer = null;
  108. }
  109. isReading = false;
  110. }
  111. function handleDisconnect(message) {
  112. stopPolling();
  113. clearDedupeHistory();
  114. reader.connected = false;
  115. reader.handle = null;
  116. setStatus(message || "读卡器未连接", "#ef4444");
  117. showConnectButton(true);
  118. }
  119. async function readUidBothProtocols() {
  120. var protocols = ["ISO14443A", "ISO15693"];
  121. for (var i = 0; i < protocols.length; i++) {
  122. try {
  123. var uid = await reader.readUid(protocols[i]);
  124. if (uid) {
  125. return { uid: uid, protocol: protocols[i] };
  126. }
  127. } catch (err) {
  128. var msg = err && err.message ? err.message : String(err || "");
  129. if (
  130. msg.indexOf("未读取到 UID") === -1 &&
  131. msg.indexOf("标签已到读卡位") === -1 &&
  132. msg.indexOf("卡片已到读卡位") === -1
  133. ) {
  134. throw err;
  135. }
  136. }
  137. }
  138. return null;
  139. }
  140. async function pollOnce() {
  141. if (isReading || !reader.connected) {
  142. return;
  143. }
  144. isReading = true;
  145. try {
  146. applyReaderConfig();
  147. var result = await readUidBothProtocols();
  148. if (result && result.uid) {
  149. var dupCheck = checkDuplicate(result.uid);
  150. if (dupCheck.isDup) {
  151. return;
  152. }
  153. recordUid(result.uid, result.protocol);
  154. lastUid = result.uid;
  155. setNfcValue(result.uid);
  156. }
  157. } catch (err) {
  158. var msg = err && err.message ? err.message : "读卡失败";
  159. if (msg.indexOf("连接") !== -1 || msg.indexOf("断开") !== -1 || msg.indexOf("超时") !== -1) {
  160. handleDisconnect("读卡器连接已断开");
  161. }
  162. } finally {
  163. isReading = false;
  164. }
  165. }
  166. function startPolling() {
  167. if (pollingTimer || !reader.connected) {
  168. return;
  169. }
  170. var interval = parseInt(getValue("pollingInterval", "1000"), 10);
  171. if (!interval || interval < 100) {
  172. interval = 1000;
  173. }
  174. pollOnce();
  175. pollingTimer = setInterval(pollOnce, interval);
  176. }
  177. async function connectReader(isManual) {
  178. if (isConnecting) {
  179. return;
  180. }
  181. isConnecting = true;
  182. setStatus("读卡器连接中...", "#64748b");
  183. showConnectButton(false);
  184. try {
  185. applyReaderConfig();
  186. clearDedupeHistory();
  187. await reader.selectDevice();
  188. setStatus("读卡器连接成功", "#16a34a");
  189. showConnectButton(false);
  190. startPolling();
  191. } catch (err) {
  192. stopPolling();
  193. var msg = err && err.message ? err.message : "读卡器连接失败";
  194. setStatus(msg, "#ef4444");
  195. showConnectButton(true);
  196. if (isManual) {
  197. alert(msg);
  198. }
  199. } finally {
  200. isConnecting = false;
  201. }
  202. }
  203. function bindEvents() {
  204. var button = getEl("btnConnect");
  205. if (button) {
  206. button.addEventListener("click", function () {
  207. connectReader(true);
  208. });
  209. }
  210. var dedupeModeEl = getEl("dedupeMode");
  211. if (dedupeModeEl) {
  212. dedupeModeEl.addEventListener("change", function () {
  213. clearDedupeHistory();
  214. });
  215. }
  216. }
  217. function init() {
  218. bindEvents();
  219. setStatus("读卡器连接中...", "#64748b");
  220. showConnectButton(false);
  221. connectReader(false);
  222. }
  223. window.addEventListener("load", init);
  224. })();