| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- /**
- * node-red打印程序
- */
- import Common from '../../common/Common.js';
- import Uuid from '../../common/Uuid.js';
- export default {
- webSocket: '', // websocket对象
- socketUrl: 'ws://127.0.0.1:18800/api/ws/print', // websocket客户端地址
- /**
- * 获取所有的打印机
- */
- getPrinters: function () {
- var _self = this;
- var command = {
- id: Uuid.createUUID(),
- command: 'getPrinters',
- data: null,
- };
- var commandStr = JSON.stringify(command);
- return new Promise((resolve, reject) => {
- _self.connectNodeRed(resolve, reject, commandStr);
- });
- },
- /**
- * 打印条码程序
- * @param {调用的打印方法, 使用http请求头,举例:url = "http://127.0.0.1:8080/api/JobOrderResource/print} url
- * @param {打印机名称, 取值"TOSHIBA" ,"GK888t","Zebra"} printerName
- */
- print: function (url, printerName) {
- var _self = this;
- return new Promise((resolve, reject) => {
- $.ajax({
- type: 'get',
- url: url,
- beforeSend: function (request) {
- Common.addTokenToRequest(request);
- },
- success: function (data) {
- if (data && data.length > 0) {
- var printPagesStr = JSON.parse(data);
- _self.printPrintPages(printPagesStr, printerName);
- }
- },
- error: function (XMLHttpRequest, textStatus, errorThrown) {
- reject(XMLHttpRequest);
- },
- });
- });
- },
- /**
- * 打印机文件
- * @param {*} printPages
- */
- printPrintPages: function(printPages, printerName){
- var _self = this;
- var commandData = {
- id: Uuid.createUUID(),
- command: 'print',
- printerName: printerName,
- data: printPages,
- };
- var commandStr = JSON.stringify(commandData);
- var promise = new Promise((resolve, reject) => {
- _self.connectNodeRed(resolve, reject, commandStr);
- });
- return promise;
- },
- /**
- * 打印机文件预览
- * @param {*} printPages
- */
- printPreview: function(printPages, printerName){
- var _self = this;
- var commandData = {
- id: Uuid.createUUID(),
- command: 'printPreview',
- printerName: printerName,
- data: printPages,
- };
- var commandStr = JSON.stringify(commandData);
- var promise = new Promise((resolve, reject) => {
- _self.connectNodeRed(resolve, reject, commandStr);
- });
- return promise;
- },
- // 连接node red的websocket
- connectNodeRed: function (resolve, reject, commandStr) {
- var _self = this;
- let printers = [];
- _self.webSocket = new WebSocket(this.socketUrl);
- _self.webSocket.onopen = function (event) {
- console.log('打印 Websocket 连接成功。');
- if (commandStr != null && commandStr.length > 0) {
- _self.sendCommand(commandStr);
- } else {
- _self.close();
- }
- };
- _self.webSocket.onclose = function (event) {
- console.log('打印 Websocket 断开连接。');
- resolve(printers);
- };
- _self.webSocket.onerror = function (event) {
- console.log('打印 Websocket 出错。');
- _self.close();
- reject();
- };
- _self.webSocket.onmessage = function (event) {
- var data = JSON.parse(event.data);
- if (data.command == 'getPrinters') {
- printers = data.data;
- }
- if (data.command == 'printPreview') {
- printers = data.previewImages;
- }
- _self.close();
- };
- },
- /**
- * WebSocket 发送指令
- * @param {指令} command
- */
- sendCommand: function (command) {
- if (this.webSocket) {
- this.webSocket.send(command);
- }
- },
- /**
- * 关闭Websocket
- */
- close: function () {
- if (this.webSocket) {
- this.webSocket.close();
- this.webSocket = null;
- };
- },
- };
|