emis-frontend/src/utils/TanexPrinterSdk.js

522 lines
14 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:发送打印
2024-05-23 13:41:55 +00:00
batchGetPDF:PDF预览和下载
2024-05-14 23:37:18 +00:00
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 {
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-20 01:05:34 +00:00
WS_URL = 'ws://'+process.env.VUE_APP_PRINTER_HOST+'/ws';
HTTP_GET_PDF = 'http://'+process.env.VUE_APP_PRINTER_HOST+'/api/downPdfFile';
2024-05-20 00:49:06 +00:00
2024-05-20 00:58:33 +00:00
// WS_URL = "ws://192.168.88.100:17080/ws";
// HTTP_GET_PDF = "http://192.168.88.100:17080/api/downPdfFile";
2024-05-19 16:31:11 +00:00
2024-05-19 04:07:15 +00:00
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-23 13:41:55 +00:00
onBatchGetPDFCallback = [];
2024-05-14 23:37:18 +00:00
// 自定义 回调函数
callbacks ={}
2024-05-18 10:19:01 +00:00
// 打印机列表
printerList = []
// 打印模版列表
printTplList = []
2024-05-23 13:41:55 +00:00
// PDF文件缓存
recievePdfFileList = []
sendPdfFileListCount = 0
pdfFileReceiveMap = new Map();
2024-05-24 01:48:56 +00:00
recievePdfFileTimer = null;
2024-05-23 13:41:55 +00:00
2024-05-18 10:19:01 +00:00
// 默认打印机
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);
}
2024-05-24 01:48:56 +00:00
// 只支持一个事件响应
2024-05-23 13:41:55 +00:00
setOnBatchGetPDF(callback){
2024-05-24 01:48:56 +00:00
// this.onBatchGetPDFCallback.push(callback);
this.onBatchGetPDFCallback=[];
2024-05-23 13:41:55 +00:00
this.onBatchGetPDFCallback.push(callback);
}
2024-05-19 04:07:15 +00:00
// 获取打印机列表
getPrinterTplList(){
return this.printerList;
}
// 获取打印模版列表
getPrintTplList(){
return this.printTplList;
}
// 获取默认打印机
getDefaultPrinter(){
return this.defaultPrinter;
}
2024-05-19 16:31:11 +00:00
// 获取pdf链接
getPdfUrl(ydSn,tplName,printerName){
return this.HTTP_GET_PDF + '?ydSn=' + ydSn+"&tplName="+tplName+"&printerName="+printerName;
}
2024-05-19 04:07:15 +00:00
// 下载PDF文件
2024-05-23 13:41:55 +00:00
// 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();
// }
// 批量获取PDF文件
2024-05-24 01:48:56 +00:00
batchGetPDF(tplName,printerName,orderList,callback){
2024-05-23 13:41:55 +00:00
if(tplName&&printerName&&orderList&&orderList.length>0){
2024-05-24 01:48:56 +00:00
this.setOnBatchGetPDF(callback);
// 超时计算
2024-05-23 13:41:55 +00:00
this.sendBatchGetPDFCMD(tplName,printerName,orderList);
2024-05-24 01:48:56 +00:00
this.recievePdfFileTimer = setTimeout(function(){
if(this.onBatchGetPDFCallback){
if(this.onBatchGetPDFCallback.length>0){
this.onBatchGetPDFCallback.forEach(callback=>{
if(callback!=null){
callback([],false,"打印超时");
}
})
}
}
clearTimeout(this.recievePdfFileTimer);
}, 60000)
2024-05-23 13:41:55 +00:00
}
2024-05-19 16:31:11 +00:00
}
2024-05-23 13:41:55 +00:00
2024-05-19 16:31:11 +00:00
// 批量打印
batchPrint(tplName,printerName,orderList){
if(tplName&&printerName&&orderList&&orderList.length>0){
this.sendBatchPrintCMD(tplName,printerName,orderList);
}
2024-05-19 04:07:15 +00:00
}
//--内部方法--------------------------------------------------------------
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();
2024-05-19 16:31:11 +00:00
// console.log(JSON.stringify(printCmd));
2024-05-14 23:37:18 +00:00
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
2024-05-19 16:31:11 +00:00
// console.log(JSON.stringify(printCmd));
2024-05-14 23:37:18 +00:00
this.send(JSON.stringify(printCmd),null);
}
}
2024-05-23 13:41:55 +00:00
sendBatchGetPDFCMD(tplName,printerName,orderList){
// {"CMD":"batchGetPDF","status":"1","Data":["DT20210901001","DT20210901002"]}
var printCmd={};
printCmd['CMD']="batchGetPDF";
printCmd['Status']="1";
if(orderList!=null && orderList.length>0){
let printData={
"tplName":tplName,
"printerName":printerName,
"orderList":orderList.toString()
}
this.recievePdfFileList = []
this.sendPdfFileListCount = orderList.length;
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']="";
2024-05-19 16:31:11 +00:00
// 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']="";
2024-05-19 16:31:11 +00:00
// 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) {
2024-05-19 16:31:11 +00:00
// console.log('您的浏览器不支持WebSocket');
2024-05-14 23:37:18 +00:00
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 = () => {
2024-05-19 16:31:11 +00:00
// console.log("ws onerror!");
2024-05-19 04:07:15 +00:00
if(this.onErrorCallback.length>0){
this.onErrorCallback.forEach(callback=>{
2024-05-19 16:31:11 +00:00
if(callback!=null){
callback();
}
2024-05-19 04:07:15 +00:00
})
2024-05-14 23:37:18 +00:00
}
}
this.ws.onopen = () => {
2024-05-19 16:31:11 +00:00
// console.log("ws onopen!");
2024-05-14 23:37:18 +00:00
this.connected = true
this.connectRetryCount = 0
2024-05-19 04:07:15 +00:00
if(this.onOpenCallback.length>0){
this.onOpenCallback.forEach(callback=>{
2024-05-19 16:31:11 +00:00
if(callback!=null){
callback();
}
2024-05-19 04:07:15 +00:00
})
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 = () => {
2024-05-19 16:31:11 +00:00
// console.log("ws onpong!");
2024-05-19 04:07:15 +00:00
}
2024-05-14 23:37:18 +00:00
this.ws.onclose = () => {
2024-05-19 16:31:11 +00:00
// console.log("ws onclose!");
2024-05-14 23:37:18 +00:00
this.connected = false;
this.connectRetryCount++;
2024-05-19 04:07:15 +00:00
if(this.onCloseCallback.length>0){
this.onCloseCallback.forEach(callback=>{
2024-05-19 16:31:11 +00:00
if(callback!=null){
callback();
}
2024-05-19 04:07:15 +00:00
})
2024-05-14 23:37:18 +00:00
}
setTimeout(() => {
this.connect()
}, 500 * this.connectRetryCount)
};
this.ws.onmessage = e => {
2024-05-19 16:31:11 +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=>{
2024-05-19 16:31:11 +00:00
if(callback!=null){
callback();
}
2024-05-19 04:07:15 +00:00
})
}
}
2024-05-23 13:41:55 +00:00
else if(msg["CMD"]=='getPrintTplList'){
2024-05-19 04:07:15 +00:00
this.innerGetPrintTplList(msg)
if(this.onGetPrintTplCallback.length>0){
this.onGetPrintTplCallback.forEach(callback=>{
2024-05-19 16:31:11 +00:00
if(callback!=null){
callback();
}
2024-05-19 04:07:15 +00:00
})
}
}
2024-05-23 13:41:55 +00:00
else if(msg["CMD"]=='batchGetPDF'){
this.innerBatchGetPDF(msg)
}
2024-05-19 04:07:15 +00:00
// 通知其他客户端
// if (this.callbacks[msg["CMD"]]) {
// this.callbacks[msg["CMD"]](msg);
// }
2024-05-14 23:37:18 +00:00
}
} else {
2024-05-19 16:31:11 +00:00
// console.log("数据格式非法:" + e.data);
2024-05-14 23:37:18 +00:00
}
};
} catch (ex) {
2024-05-19 16:31:11 +00:00
// if (console && // console.log) {
// // console.log(ex);
// }
2024-05-14 23:37:18 +00:00
}
}
return this.ws;
}
2024-05-19 04:07:15 +00:00
innerGetPrinterList(msg){
2024-05-19 16:31:11 +00:00
// console.log("innerGetPrinterList")
2024-05-18 10:19:01 +00:00
if(msg){
this.printerList=[];
2024-05-19 16:31:11 +00:00
// console.log(msg);
2024-05-19 04:07:15 +00:00
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){
2024-05-19 16:31:11 +00:00
// console.log("innerGetPrintTplList")
2024-05-19 04:07:15 +00:00
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-23 13:41:55 +00:00
innerBatchGetPDF(msg){
console.log("innerBatchGetPDF")
console.log(msg)
2024-05-24 01:48:56 +00:00
// 有出错,停止接收数据
if(msg.Status!='2'){
clearTimeout(this.recievePdfFileTimer);
if(this.onBatchGetPDFCallback.length>0){
this.onBatchGetPDFCallback.forEach(callback=>{
if(callback!=null){
callback([],false,msg.Data);
}
})
}
return;
}
2024-05-23 13:41:55 +00:00
if(this.recievePdfFileList.length<this.sendPdfFileListCount){
if(msg){
let ydSn=msg.Data.ydSn;
if(ydSn in this.pdfFileReceiveMap){
// 文件已存在则返回
console.log(ydSn+"-PDF文件已存在");
return;
}
2024-05-24 01:48:56 +00:00
this.recievePdfFileList.push(msg.Data.file);
2024-05-23 13:41:55 +00:00
this.pdfFileReceiveMap[ydSn]=1;
}
}
// 如果已相等则表示文件已接收完成
if(this.recievePdfFileList.length >= this.sendPdfFileListCount){
if(this.onBatchGetPDFCallback.length>0){
this.onBatchGetPDFCallback.forEach(callback=>{
if(callback!=null){
2024-05-24 01:48:56 +00:00
clearTimeout(this.recievePdfFileTimer);
2024-05-23 13:41:55 +00:00
// 把PDF文件传给终端处理
2024-05-24 01:48:56 +00:00
callback(this.recievePdfFileList,true,"获取成功");
2024-05-23 13:41:55 +00:00
// 发送完成后数据清空
this.recievePdfFileList=[];
this.pdfFileReceiveMap=new Map();
}
})
}
console.log("文件已接收完成");
return;
}
}
2024-05-19 04:07:15 +00:00
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 {
2024-05-23 13:41:55 +00:00
console.log("Ws is abnormal,send faild!!")
2024-05-14 23:37:18 +00:00
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;