PrintUtil.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * node-red打印程序
  3. */
  4. import Common from '../../common/Common.js';
  5. import Uuid from '../../common/Uuid.js';
  6. export default {
  7. webSocket: '', // websocket对象
  8. socketUrl: 'ws://127.0.0.1:18800/api/ws/print', // websocket客户端地址
  9. /**
  10. * 获取所有的打印机
  11. */
  12. getPrinters: function () {
  13. var _self = this;
  14. var command = {
  15. id: Uuid.createUUID(),
  16. command: 'getPrinters',
  17. data: null,
  18. };
  19. var commandStr = JSON.stringify(command);
  20. return new Promise((resolve, reject) => {
  21. _self.connectNodeRed(resolve, reject, commandStr);
  22. });
  23. },
  24. /**
  25. * 打印条码程序
  26. * @param {调用的打印方法, 使用http请求头,举例:url = "http://127.0.0.1:8080/api/JobOrderResource/print} url
  27. * @param {打印机名称, 取值"TOSHIBA" ,"GK888t","Zebra"} printerName
  28. */
  29. print: function (url, printerName) {
  30. var _self = this;
  31. return new Promise((resolve, reject) => {
  32. $.ajax({
  33. type: 'get',
  34. url: url,
  35. beforeSend: function (request) {
  36. Common.addTokenToRequest(request);
  37. },
  38. success: function (data) {
  39. if (data && data.length > 0) {
  40. var printPagesStr = JSON.parse(data);
  41. _self.printPrintPages(printPagesStr, printerName);
  42. }
  43. },
  44. error: function (XMLHttpRequest, textStatus, errorThrown) {
  45. reject(XMLHttpRequest);
  46. },
  47. });
  48. });
  49. },
  50. /**
  51. * 打印机文件
  52. * @param {*} printPages
  53. */
  54. printPrintPages: function(printPages, printerName){
  55. var _self = this;
  56. var commandData = {
  57. id: Uuid.createUUID(),
  58. command: 'print',
  59. printerName: printerName,
  60. data: printPages,
  61. };
  62. var commandStr = JSON.stringify(commandData);
  63. var promise = new Promise((resolve, reject) => {
  64. _self.connectNodeRed(resolve, reject, commandStr);
  65. });
  66. return promise;
  67. },
  68. /**
  69. * 打印机文件预览
  70. * @param {*} printPages
  71. */
  72. printPreview: function(printPages, printerName){
  73. var _self = this;
  74. var commandData = {
  75. id: Uuid.createUUID(),
  76. command: 'printPreview',
  77. printerName: printerName,
  78. data: printPages,
  79. };
  80. var commandStr = JSON.stringify(commandData);
  81. var promise = new Promise((resolve, reject) => {
  82. _self.connectNodeRed(resolve, reject, commandStr);
  83. });
  84. return promise;
  85. },
  86. // 连接node red的websocket
  87. connectNodeRed: function (resolve, reject, commandStr) {
  88. var _self = this;
  89. let printers = [];
  90. _self.webSocket = new WebSocket(this.socketUrl);
  91. _self.webSocket.onopen = function (event) {
  92. console.log('打印 Websocket 连接成功。');
  93. if (commandStr != null && commandStr.length > 0) {
  94. _self.sendCommand(commandStr);
  95. } else {
  96. _self.close();
  97. }
  98. };
  99. _self.webSocket.onclose = function (event) {
  100. console.log('打印 Websocket 断开连接。');
  101. resolve(printers);
  102. };
  103. _self.webSocket.onerror = function (event) {
  104. console.log('打印 Websocket 出错。');
  105. _self.close();
  106. reject();
  107. };
  108. _self.webSocket.onmessage = function (event) {
  109. var data = JSON.parse(event.data);
  110. if (data.command == 'getPrinters') {
  111. printers = data.data;
  112. }
  113. if (data.command == 'printPreview') {
  114. printers = data.previewImages;
  115. }
  116. _self.close();
  117. };
  118. },
  119. /**
  120. * WebSocket 发送指令
  121. * @param {指令} command
  122. */
  123. sendCommand: function (command) {
  124. if (this.webSocket) {
  125. this.webSocket.send(command);
  126. }
  127. },
  128. /**
  129. * 关闭Websocket
  130. */
  131. close: function () {
  132. if (this.webSocket) {
  133. this.webSocket.close();
  134. this.webSocket = null;
  135. };
  136. },
  137. };