diff --git a/src/utils/TanexPrinterSdk.js b/src/utils/TanexPrinterSdk.js new file mode 100644 index 00000000..884146a5 --- /dev/null +++ b/src/utils/TanexPrinterSdk.js @@ -0,0 +1,240 @@ +'use strict' +/* + TanexPrinterSdk + + getPrinterList: 获取打印机列表 + getPrinterTplList: 获取打印模版列表 + batchPrint:发送打印 + getPrinterVer:获取打印组件版本信息 + + printerIsRead:打印机已经OK的信号 + + + */ +import { getToken } from '@/utils/auth' + +class TanexPrinterSdk { + VERSION="1.0.0" + MAX_SENT_RETRY_COUNT = 9; + // WS_URL = "ws://127.0.0.1:17080"; + WS_URL = "ws://192.168.88.100:17080"; + 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; diff --git a/src/utils/TnPrinterSdk copy.js b/src/utils/TnPrinterSdk copy.js new file mode 100644 index 00000000..7c6bf65a --- /dev/null +++ b/src/utils/TnPrinterSdk copy.js @@ -0,0 +1,121 @@ +import { Message } from 'element-ui' +import { getToken } from '@/utils/auth' +let websock = null +let messageCallback = null +let errorCallback = null +let wsUrl = '' +let tryTime = 0 +// 与后端的协商,websocket请求需要带上token参数 +/* +命令名称: +command +getPrinterList: 获取打印机列表 +getPrinterTplList: 获取打印模版列表 +sendPrinterTplList: 获取打印模版列表 + +sendToPrint:发送打印 +getPrinterVer:获取打印组件版本信息 +*/ + +// 接收ws后端返回的数据 +function websocketonmessage (e) { + messageCallback(JSON.parse(e.data)) +} + +/** + * 发起websocket连接 + * @param {Object} agentData 需要向后台传递的参数数据 + */ +function websocketSend (agentData) { + // 加延迟是为了尽量让ws连接状态变为OPEN + setTimeout(() => { + // 添加状态判断,当为OPEN时,发送消息 + if (websock.readyState === websock.OPEN) { // websock.OPEN = 1 + // 发给后端的数据需要字符串化 + websock.send(JSON.stringify(agentData)) + } + if (websock.readyState === websock.CLOSED) { // websock.CLOSED = 3 + console.log('websock.readyState=3') + Message.error('ws连接异常,请稍候重试') + errorCallback() + } + }, 500) +} + +// 关闭ws连接 +function websocketclose (e) { + // e.code === 1000 表示正常关闭。 无论为何目的而创建, 该链接都已成功完成任务。 + // e.code !== 1000 表示非正常关闭。 + if (e && e.code !== 1000) { + Message.error('ws连接异常,请稍候重试') + errorCallback() + // // 如果需要设置异常重连则可替换为下面的代码,自行进行测试 + // if (tryTime < 10) { + // setTimeout(function() { + // websock = null + // tryTime++ + // initWebSocket() + // console.log(`第${tryTime}次重连`) + // }, 3 * 1000) + //} else { + // Message.error('重连失败!请稍后重试') + //} + } +} +// 建立ws连接 +function websocketOpen (e) { + // console.log('ws连接成功') +} + +// 初始化weosocket +function initWebSocket () { + if (typeof (WebSocket) === 'undefined') { + Message.error('您的浏览器不支持WebSocket,无法获取数据') + return false + } + + const token = 'X-Token=' + getToken() + // ws请求完整地址 + const requstWsUrl = wsUrl + '?' + token + websock = new WebSocket(requstWsUrl) + + websock.onmessage = function (e) { + websocketonmessage(e) + } + websock.onopen = function () { + websocketOpen() + } + websock.onerror = function () { + Message.error('ws连接异常,请稍候重试') + errorCallback() + } + websock.onclose = function (e) { + websocketclose(e) + } +} + +/** + * 发起websocket请求函数 + * @param {string} url ws连接地址 + * @param {Object} agentData 传给后台的参数 + * @param {function} successCallback 接收到ws数据,对数据进行处理的回调函数 + * @param {function} errCallback ws连接错误的回调函数 + */ +export function sendWebsocket (url, agentData, successCallback, errCallback) { + wsUrl = url + initWebSocket() + messageCallback = successCallback + errorCallback = errCallback + websocketSend(agentData) +} + +/** + * 关闭websocket函数 + */ +export function closeWebsocket () { + if (websock) { + websock.close() // 关闭websocket + websock.onclose() // 关闭websocket + } +} + diff --git a/src/views/emis/emisWaybill/emisWaybillRecord copy.vue b/src/views/emis/emisWaybill/emisWaybillRecord copy.vue new file mode 100644 index 00000000..1da05cca --- /dev/null +++ b/src/views/emis/emisWaybill/emisWaybillRecord copy.vue @@ -0,0 +1,1331 @@ + + + + + + diff --git a/src/views/emis/emisWaybill/waybillIsPrinted.vue b/src/views/emis/emisWaybill/waybillIsPrinted.vue new file mode 100644 index 00000000..8dd681eb --- /dev/null +++ b/src/views/emis/emisWaybill/waybillIsPrinted.vue @@ -0,0 +1,797 @@ + + + + +