var ws; var wsUrl = 'ws://localhost:2012';//打印后台地址与 var isWx = true; function connection() { if ('WebSocket' in window) { ws = new WebSocket(wsUrl); } else if ('MozWebSocket' in window) { ws = new MozWebSocket(wsUrl); } else { alert('当前浏览器不支持'); } //注册各类回调 ws.onopen = function () { // alert('连接打印后台成功'); isWx = true } ws.onclose = function () { // alert('与打印后台断开连接'); isWx = false } ws.onerror = function () { alert('数据传输发生错误'); isWx = false } ws.onmessage = function (receiveMsg) { if (receiveMsg.data.split("|")[0] == "A_GetPrinterStatus") { var ret = receiveMsg.data.split("|")[1]; if (ret == 0) { document.getElementById("status").value = "无返回值"; return; } else if (ret == 1) { document.getElementById("status").value = "打印机命令解析器忙碌中!"; return; } else if (ret == 2) { document.getElementById("status").value = "2 纸张用完或安装错误!"; //纸张用完或安装错误! return; } else if (ret == 4) { document.getElementById("status").value = "4 碳带用完或安装错误!"; //碳带用完或安装错误!"; return; } else if (ret == 8) { document.getElementById("status").value = "8 打印批次文档中!"; //打印批次文档中!"; return; } else if (ret == 16) { document.getElementById("status").value = "16 正在打印文件!"; //正在打印文件! return; } else if (ret == 32) { document.getElementById("status").value = "32 打印机暂停!"; //打印机暂停! return; } else if (ret == 64) { document.getElementById("status").value = "64 正在送出标签纸!"; //正在送出标签纸! return; } else if (ret == 9) { document.getElementById("status").value = "9 打印机待机中!"; //打印机待机中! return; } } } } function sendMessage(data) { //尝试向打印后台发送消息 if (!isWx) return; ws.send('A_EnumUSB'); ws.send('A_CreateUSBPort|1'); ws.send('A_Prn_Text_TrueType|5|0|35|宋体|1|600|0|0|0|AA|' + data.code_num + '|1'); ws.send('A_Prn_Text_TrueType|5|20|34|宋体|1|900|0|800|0|BB|' + data.shopName + '|1'); ws.send('A_Prn_Text_TrueType|5|40|28|宋体|1|600|0|0|0|DD|' + data.username + '|1'); ws.send('A_Prn_Text_TrueType|5|60|24|宋体|1|600|0|0|0|HH|' + data.appendix + '|1'); ws.send('A_Prn_Text_TrueType|5|80|28|宋体|1|600|0|0|0|CC|' + data.color + ' ' + '共' + data.total_count + '件|1'); ws.send('A_Prn_Text_TrueType|5|100|28|宋体|1|600|0|0|0|EE|' + data.name + '|1'); ws.send('A_Prn_Text_TrueType|5|120|24|宋体|1|600|0|0|0|FF|' + data.shop_order_no + '|1'); ws.send('A_Prn_Barcode|110|134|2|o|2|3|60|B|1|' + data.code_num + ''); ws.send('A_Prn_Text_TrueType|170|30|32|宋体|4|600|0|0|0|GG|' + data.telnum + '|1'); ws.send('A_Print_Out|1|1|1|1'); ws.send('A_ClosePrn'); } // function getStatus() { // ws.send('A_EnumUSB'); // ws.send('A_CreateUSBPort|1'); // ws.send('A_GetPrinterStatus'); // ws.send('A_ClosePrn'); // } export default { sendMessage: sendMessage, connection: connection }