emis-frontend/src/utils/TanexPrinterSdk.js

243 lines
5.9 KiB
JavaScript
Raw Normal View History

2024-05-14 23:37:18 +00:00
'use strict'
/*
TanexPrinterSdk
getPrinterList: 获取打印机列表
getPrinterTplList: 获取打印模版列表
batchPrint:发送打印
getPrinterVer:获取打印组件版本信息
printerIsRead:打印机已经OK的信号
2024-05-15 05:41:09 +00:00
updatePrintStatus: 打印状态更新
2024-05-14 23:37:18 +00:00
*/
import { getToken } from '@/utils/auth'
class TanexPrinterSdk {
VERSION="1.0.0"
MAX_SENT_RETRY_COUNT = 9;
2024-05-15 05:41:09 +00:00
WS_URL = "ws://127.0.0.1:17080";
// WS_URL = "ws://192.168.88.100:17080";
2024-05-14 23:37:18 +00:00
AUTH_TOKEN=null;
WS_INIT = 0;
WS_OPEN = 1;
WS_CLOSING = 2;
WS_CLOSE = 3;
// 和服务端连接的socket对象
ws = null;
// WS 回调函数
onOpen =null;
onClose = null;
onError =null;
onMessage = null;
// 自定义 回调函数
callbacks ={}
// 标识:是否连接成功 , 记录重试的次数,重新连接尝试的次数
connected = false;
printerIsRead = false;
sendRetryCount = 0;
connectRetryCount = 0;
// static instance = null;
// static getInstance() {
// if (!this.instance) {
// this.instance = new TanexPrinterSdk()
// }
// return this.instance
// }
initSdk(callbacks){
this.token=getToken();
this.callbacks=callbacks;
this.onOpen=callbacks['onOpen'];
this.onClose=callbacks['onClose'];
this.onError=callbacks['onError'];
this.connect();
this.start();
}
sendToken(){
// {"CMD":"setToken","status":"1","Data":"token"}
var printCmd={};
printCmd['CMD']="setToken";
printCmd['Status']="1";
printCmd['Data']=getToken();
console.log(JSON.stringify(printCmd));
this.send(JSON.stringify(printCmd),null);
}
batchPrint(tplName,printerName,orderList){
// {"CMD":"batchPrint","status":"1","Data":["DT20210901001","DT20210901002"]}
var printCmd={};
printCmd['CMD']="batchPrint";
printCmd['Status']="1";
if(orderList!=null && orderList.length>0){
let printData={
"tplName":tplName,
"printerName":printerName,
"orderList":orderList.toString()
}
printCmd['Data']=printData
console.log(JSON.stringify(printCmd));
this.send(JSON.stringify(printCmd),null);
}
}
getPrinterList(callback){
// {"CMD":"getPrinterList","status":"1","Data":""}
var printCmd={};
printCmd['CMD']="getPrinterList";
printCmd['Status']="1";
printCmd['Data']="";
console.log(JSON.stringify(printCmd));
this.send(JSON.stringify(printCmd),callback);
}
getPrinterTplList(callback){
// {"CMD":"getPrinterTplList","status":"1","Data":""}
var printCmd={};
printCmd['CMD']="getPrinterTplList";
printCmd['Status']="1";
printCmd['Data']="";
console.log(JSON.stringify(printCmd));
this.send(JSON.stringify(printCmd),callback);
}
// 连接服务器的方法
connect() {
// 连接服务器
if (!window.WebSocket) {
console.log('您的浏览器不支持WebSocket');
return null;
}
if (!this.ws || this.getWSReadyState() === this.WS_CLOSING || this.getWSReadyState() === this.WS_CLOSE) {
try {
this.ws = new WebSocket(this.WS_URL);
this.ws.onerror = () => {
console.log("ws onerror!");
if(this.onError!=null){
this.onError();
}
}
this.ws.onopen = () => {
console.log("ws onopen!");
this.connected = true
this.connectRetryCount = 0
if(this.onOpen != null){
this.onOpen()
}
this.sendToken();
// this.getPrinterList();
};
this.ws.onclose = () => {
console.log("ws onclose!");
this.connected = false;
this.connectRetryCount++;
if(this.onClose != null){
this.onClose()
}
setTimeout(() => {
this.connect()
}, 500 * this.connectRetryCount)
};
this.ws.onmessage = e => {
console.log('ws onmessage:' + e.data)
if (e.data.charAt(0) === "{") {
var msg = this.jsonToObj(e.data);
if(msg["CMD"]=='printerIsRead'){
printerIsRead=true;
}else{
if (this.callbacks[msg["CMD"]]) {
this.callbacks[msg["CMD"]](msg);
}
}
} else {
console("数据格式非法:" + e.data);
}
};
} catch (ex) {
debugger
if (console && console.log) {
console.log(ex);
}
}
}
return this.ws;
}
jsonToObj(json){
return JSON.parse(json)
}
objToJson(obj){
return JSON.stringify(obj)
}
/**
* readyState->0:尚未建立连接;1:已经建立连接;2:正在关闭;3:已经关闭或不可用
* @param thisWs
* @returns {*}
*/
getWSReadyState() {
if (this.ws.readyState !== undefined) {
return this.ws.readyState;
}
return this.WS_INIT;
};
// 发送数据的方法
send(data) {
if (this.getWSReadyState() === this.WS_OPEN ) {
this.sendRetryCount = 0
this.ws.send(data)
return 1;
} else {
console.log("Ws is abnormal,send faild!!")
return -1
}
}
// 关闭socket连接
close() {
if (this.ws) {
this.ws.close();
this.connected = false; // 更新连接状态
clearInterval(this.heartbeatInterval); // 清除心跳检测定时器
}
}
// 开启心跳检测
start() {
this.heartbeatInterval = setInterval(() => {
this.data = {type: "ping"};
this.send(JSON.stringify(this.data));
}, 5000);
}
}
export default TanexPrinterSdk;