emis-frontend/src/utils/TanexPrinterSdk.js

374 lines
9.4 KiB
JavaScript
Raw Normal View History

2024-05-14 23:37:18 +00:00
'use strict'
2024-05-19 04:07:15 +00:00
import { download } from '@/utils/request'
2024-05-14 23:37:18 +00:00
/*
TanexPrinterSdk
getPrinterList: 获取打印机列表
2024-05-19 04:07:15 +00:00
getPrintTplList: 获取打印模版列表
2024-05-14 23:37:18 +00:00
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'
2024-05-19 04:07:15 +00:00
import { forEach } from 'lodash';
2024-05-14 23:37:18 +00:00
class TanexPrinterSdk {
2024-05-18 10:19:01 +00:00
2024-05-14 23:37:18 +00:00
VERSION="1.0.0"
MAX_SENT_RETRY_COUNT = 9;
2024-05-18 10:19:01 +00:00
// WS_URL = "ws://127.0.0.1:17080";
2024-05-19 04:07:15 +00:00
// WS_URL = "ws://192.168.88.100:17080";
WS_URL = "ws://192.168.88.100:17080/ws";
HTTP_GET_PDF = "http://192.168.88.100:17080/api/downPdfFile";
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 回调函数
2024-05-19 04:07:15 +00:00
onOpenCallback =[];
onCloseCallback=[];
onErrorCallback =[];
// onMessageCallback =[];
onGetPrinterCallback =[];
onGetPrintTplCallback =[];
2024-05-14 23:37:18 +00:00
// 自定义 回调函数
callbacks ={}
2024-05-18 10:19:01 +00:00
// 打印机列表
printerList = []
// 打印模版列表
printTplList = []
// 默认打印机
defaultPrinter = null;
2024-05-14 23:37:18 +00:00
// 标识:是否连接成功 , 记录重试的次数,重新连接尝试的次数
connected = false;
printerIsRead = false;
sendRetryCount = 0;
connectRetryCount = 0;
2024-05-18 10:19:01 +00:00
static instance = null;
constructor() {
this.instance = null
}
static getInstance() {
if (!this.instance) {
this.instance = new TanexPrinterSdk()
}
return this.instance
}
2024-05-19 04:07:15 +00:00
// 外部方法----------------------------------------------------------------
init(onOpen,onClose,onError,onGetPrinter,onGetPrintTpl,OnGetPrintTpl){
// 初始化参数建立连接
2024-05-14 23:37:18 +00:00
this.token=getToken();
2024-05-19 04:07:15 +00:00
this.connect();
this.start();
}
2024-05-14 23:37:18 +00:00
2024-05-19 04:07:15 +00:00
setOnOpen(callback){
this.onOpenCallback.push(callback);
}
2024-05-14 23:37:18 +00:00
2024-05-19 04:07:15 +00:00
setOnClose(callback){
this.onCloseCallback.push(callback);
}
2024-05-18 10:19:01 +00:00
2024-05-19 04:07:15 +00:00
setOnError(callback){
this.onErrorCallback.push(callback);
}
setOnGetPrinter(callback){
this.onGetPrinterCallback.push(callback);
2024-05-14 23:37:18 +00:00
}
2024-05-19 04:07:15 +00:00
setOnGetPrintTpl(callback){
this.onGetPrintTplCallback.push(callback);
}
// 获取打印机列表
getPrinterTplList(){
return this.printerList;
}
// 获取打印模版列表
getPrintTplList(){
return this.printTplList;
}
// 获取默认打印机
getDefaultPrinter(){
return this.defaultPrinter;
}
// 下载PDF文件
downPdfFile(ydSn,tplName,printerName){
let link = document.createElement('a');
link.style.display = 'none';
link.href = this.HTTP_GET_PDF + '?ydSn=' + ydSn+"&tplName="+tplName+"&printerName="+printerName;
window.open(link.href, '_blank');
// document.body.appendChild(link);
// link.click();
}
//--内部方法--------------------------------------------------------------
sendSetTokenCMD(){
2024-05-14 23:37:18 +00:00
// {"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);
}
2024-05-19 04:07:15 +00:00
sendBatchPrintCMD(tplName,printerName,orderList){
2024-05-14 23:37:18 +00:00
// {"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);
}
}
2024-05-19 04:07:15 +00:00
sendGetPrinterListCMD(){
2024-05-14 23:37:18 +00:00
// {"CMD":"getPrinterList","status":"1","Data":""}
var printCmd={};
printCmd['CMD']="getPrinterList";
printCmd['Status']="1";
printCmd['Data']="";
console.log(JSON.stringify(printCmd));
2024-05-19 04:07:15 +00:00
this.send(JSON.stringify(printCmd),null);
2024-05-14 23:37:18 +00:00
}
2024-05-19 04:07:15 +00:00
sendGetPrintTplListCMD(){
// {"CMD":"getPrintTplList","status":"1","Data":""}
2024-05-14 23:37:18 +00:00
var printCmd={};
2024-05-19 04:07:15 +00:00
printCmd['CMD']="getPrintTplList";
2024-05-14 23:37:18 +00:00
printCmd['Status']="1";
printCmd['Data']="";
console.log(JSON.stringify(printCmd));
2024-05-19 04:07:15 +00:00
this.send(JSON.stringify(printCmd),null);
2024-05-14 23:37:18 +00:00
}
// 连接服务器的方法
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!");
2024-05-19 04:07:15 +00:00
if(this.onErrorCallback.length>0){
this.onErrorCallback.forEach(callback=>{
callback();
})
2024-05-14 23:37:18 +00:00
}
}
this.ws.onopen = () => {
console.log("ws onopen!");
this.connected = true
this.connectRetryCount = 0
2024-05-19 04:07:15 +00:00
if(this.onOpenCallback.length>0){
this.onOpenCallback.forEach(callback=>{
callback();
})
2024-05-14 23:37:18 +00:00
}
2024-05-19 04:07:15 +00:00
this.sendSetTokenCMD();
this.sendGetPrinterListCMD();
this.sendGetPrintTplListCMD();
2024-05-14 23:37:18 +00:00
};
2024-05-19 04:07:15 +00:00
this.ws.onpong = () => {
console.log("ws onpong!");
}
2024-05-14 23:37:18 +00:00
this.ws.onclose = () => {
console.log("ws onclose!");
this.connected = false;
this.connectRetryCount++;
2024-05-19 04:07:15 +00:00
if(this.onCloseCallback.length>0){
this.onCloseCallback.forEach(callback=>{
callback();
})
2024-05-14 23:37:18 +00:00
}
setTimeout(() => {
this.connect()
}, 500 * this.connectRetryCount)
};
this.ws.onmessage = e => {
2024-05-19 04:07:15 +00:00
console.log('ws onmessage:' + e.data)
2024-05-14 23:37:18 +00:00
if (e.data.charAt(0) === "{") {
var msg = this.jsonToObj(e.data);
if(msg["CMD"]=='printerIsRead'){
printerIsRead=true;
}
2024-05-19 04:07:15 +00:00
else{
if(msg["CMD"]=='getPrinterList'){
this.innerGetPrinterList(msg)
if(this.onGetPrinterCallback.length>0){
this.onGetPrinterCallback.forEach(callback=>{
callback();
})
}
}
if(msg["CMD"]=='getPrintTplList'){
this.innerGetPrintTplList(msg)
if(this.onGetPrintTplCallback.length>0){
this.onGetPrintTplCallback.forEach(callback=>{
callback();
})
}
}
// 通知其他客户端
// if (this.callbacks[msg["CMD"]]) {
// this.callbacks[msg["CMD"]](msg);
// }
2024-05-14 23:37:18 +00:00
}
} else {
2024-05-19 04:07:15 +00:00
console.log("数据格式非法:" + e.data);
2024-05-14 23:37:18 +00:00
}
};
} catch (ex) {
debugger
if (console && console.log) {
console.log(ex);
}
}
}
return this.ws;
}
2024-05-19 04:07:15 +00:00
innerGetPrinterList(msg){
console.log("innerGetPrinterList")
2024-05-18 10:19:01 +00:00
if(msg){
this.printerList=[];
2024-05-19 04:07:15 +00:00
console.log(msg);
2024-05-18 10:19:01 +00:00
msg.Data.printerList.forEach(item=>{
let optionItem={
label:item,
value:item
}
this.printerList.push(optionItem);
2024-05-19 04:07:15 +00:00
this.defaultPrinter=msg.Data.defaultPrinter;
2024-05-18 10:19:01 +00:00
})
}
}
2024-05-19 04:07:15 +00:00
innerGetPrintTplList(msg){
console.log("innerGetPrintTplList")
if(msg){
this.printTplList=[];
msg.Data.tplList.forEach(item=>{
let optionItem={
label:item,
value:item
}
this.printTplList.push(optionItem);
this.selTplName=this.printTplList[0].value;
})
}
}
2024-05-14 23:37:18 +00:00
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(() => {
2024-05-19 04:07:15 +00:00
if (this.getWSReadyState() === this.WS_OPEN ) {
// this.ws.ping();
this.ws.send(0x9);
}
}, 10000);
2024-05-14 23:37:18 +00:00
}
}
export default TanexPrinterSdk;