feat: 初始化 NFC 读卡器项目
- 添加 Electron 主进程和渲染进程 - 实现读卡器连接、断开和读取卡片 ID 功能 - 添加自定义窗口标题栏和窗口控制 - 实现简洁美观的用户界面 - 添加项目文档 README.md
This commit is contained in:
120
lib/cardReader.es.js
Normal file
120
lib/cardReader.es.js
Normal file
@@ -0,0 +1,120 @@
|
||||
var v = {
|
||||
OBJ: function() {
|
||||
var r = {}, e = null, t = null;
|
||||
r.onResult = function(s) {
|
||||
t.addEvent("Result", s);
|
||||
};
|
||||
var o = function() {
|
||||
}, l = function(s) {
|
||||
var i = "";
|
||||
i = s.data;
|
||||
var w = {
|
||||
type: "Result",
|
||||
strStatus: i.substr(4, 2),
|
||||
strData: i.substr(6),
|
||||
strCmdCode: i.substr(0, 4)
|
||||
};
|
||||
t != null && t.fireEvent(w);
|
||||
}, d = function() {
|
||||
}, h = function() {
|
||||
alert("Server not running !");
|
||||
};
|
||||
return r.createSocket = function() {
|
||||
try {
|
||||
if ("WebSocket" in window)
|
||||
e = new WebSocket("ws://localhost:39002/RFID Reader Service");
|
||||
else if ("MozWebSocket" in window)
|
||||
e = new MozWebSocket("ws://localhost:39002/RFID Reader Service");
|
||||
else
|
||||
return alert("None"), !1;
|
||||
return e.onopen = o, e.onmessage = l, e.onclose = d, e.onerror = h, t = new u(), !0;
|
||||
} catch {
|
||||
return !1;
|
||||
}
|
||||
}, r.Disconnect = function() {
|
||||
e != null && e.close();
|
||||
}, r.send = function(s) {
|
||||
e.send(s);
|
||||
}, r;
|
||||
}
|
||||
};
|
||||
function u() {
|
||||
this.handlers = {};
|
||||
}
|
||||
u.prototype = {
|
||||
constructor: u,
|
||||
addEvent: function(r, e) {
|
||||
typeof this.handlers[r] > "u" && (this.handlers[r] = []), this.handlers[r].push(e);
|
||||
},
|
||||
fireEvent: function(r) {
|
||||
if (r.target || (r.target = this), this.handlers[r.type] instanceof Array)
|
||||
for (var e = this.handlers[r.type], t = 0; t < e.length; t++)
|
||||
e[t](r);
|
||||
},
|
||||
removeEvent: function(r, e) {
|
||||
if (this.handlers[r] instanceof Array) {
|
||||
for (var t = this.handlers[r], o = 0; o < t.length && t[o] != e; o++)
|
||||
;
|
||||
t.splice(o, 1);
|
||||
}
|
||||
}
|
||||
};
|
||||
try {
|
||||
var f = v.OBJ();
|
||||
} catch {
|
||||
}
|
||||
f.createSocket();
|
||||
let n = "00", c = !1;
|
||||
function a(r) {
|
||||
return new Promise((e, t) => {
|
||||
f.onResult(function(o) {
|
||||
o.strStatus === "00" ? e(o) : t(o.strStatus);
|
||||
}), f.send(r);
|
||||
});
|
||||
}
|
||||
async function y() {
|
||||
await a(n + "010702");
|
||||
}
|
||||
async function E() {
|
||||
await a(n + "010701");
|
||||
}
|
||||
async function S() {
|
||||
if (c)
|
||||
try {
|
||||
await a(n + "0009");
|
||||
} catch (r) {
|
||||
throw new Error(`关闭读卡器失败: EerorCode 0x${r}`);
|
||||
}
|
||||
try {
|
||||
await a(n + "000700"), await a(n + "010941"), await a(n + "010801"), y(), await a(n + "010610"), c = !0;
|
||||
} catch (r) {
|
||||
throw new Error(`连接读卡器失败: EerorCode 0x${r}`);
|
||||
}
|
||||
}
|
||||
async function R() {
|
||||
if (!c)
|
||||
throw new Error("读卡器未连接,请先连接读卡器!");
|
||||
try {
|
||||
await a(n + "100152");
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return (await a(n + "1002")).strData;
|
||||
} catch (r) {
|
||||
throw new Error(`读取ID出错: EerorCode 0x${r}`);
|
||||
}
|
||||
}
|
||||
async function g() {
|
||||
if (c)
|
||||
try {
|
||||
await a(n + "0009"), E(), await a(n + "010610"), c = !1;
|
||||
} catch (r) {
|
||||
throw new Error(`关闭读卡器失败: EerorCode 0x${r}`);
|
||||
}
|
||||
}
|
||||
export {
|
||||
g as closeReader,
|
||||
S as connectReader,
|
||||
R as readId
|
||||
};
|
||||
Reference in New Issue
Block a user