console
const wsUri = 'ws://127.0.0.1:22003'
const matchServerUrl = 'http://localhost:8001/openapi/v1/match'
function FingerPrint() {
let self = this
this.websockt = null
this.onOpen = function (evt) {
message.innerHTML += '设备连接成功<br>'
self.opendev()
}
this.onClose = function (evt) {
alert('close')
}
this.onMessage = function (evt) {
var jsondata = JSON.parse(evt.data);
if (jsondata.function === 'open') {
message.innerHTML += '设备已打开,可以开始识别<br>'
}
if (jsondata.datatype === "template") {
let templateBase64 = jsondata.data.template
self.match(templateBase64)
}
}
this.onError = function (evt) {
alert('error')
}
this.getinfo = function () {
self.doSend("{\"module\":\"common\",\"function\":\"info\",\"msgid\":\"123456789\",\"parameter\":\"\"}");
}
this.opendev = function () {
var str = "{\"module\":\"fingerprint\",\"function\":\"open\",\"msgid\":\"123456789\",\"parameter\":\"\"}"
self.doSend(str);
}
this.match = function (templateBase64) {
fetch(matchServerUrl, {
"headers": {
"accept": "application/json, text/plain, */*",
"accept-language": "zh-CN,zh;q=0.9",
"content-type": "application/json"
},
"body": JSON.stringify({ templateBase64 }),
"method": "POST"
}).then(r => {
r.json().then(r => {
console.log(r)
alert(r.userId)
if (r.userId == '') {
message.innerHTML += ('未识别的指纹<br>')
return
}
message.innerHTML += ('用户 ' + r.userId + ' 登录成功<br>')
})
})
}
this.init = function () {
let websocket = new WebSocket(wsUri);
websocket.onopen = function (evt) {
self.onOpen(evt)
};
websocket.onclose = function (evt) {
self.onClose(evt)
};
websocket.onmessage = function (evt) {
self.onMessage(evt)
};
websocket.onerror = function (evt) {
self.onError(evt)
};
self.websocket = websocket
}
this.doSend = function (message) {
if (!this.websocket) {
return
}
this.websocket.send(message);
}
return this
}
function test() {
var a = FingerPrint()
a.init()
}
<div>
<button onclick="test()">指纹登录</button>
<div id="message"></div>
</div>