PrintUtil.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * node-red打印程序
  3. */
  4. var Common = require("../../common/Common.js");
  5. var Uuid = require("../../common/Uuid.js");
  6. module.exports = {
  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. promise.then(successData => {
  67. // resolve();
  68. }, errorData => {
  69. console.log(errorData);
  70. // reject(errorData);
  71. });
  72. },
  73. /**
  74. * 打印机文件预览
  75. * @param {*} printPages
  76. */
  77. printPreview: function(printPages, printerName){
  78. var _self = this;
  79. var commandData = {
  80. id: Uuid.createUUID(),
  81. command: 'printPreview',
  82. printerName: printerName,
  83. data: printPages,
  84. }
  85. var commandStr = JSON.stringify(commandData);
  86. var promise = new Promise((resolve, reject) => {
  87. _self.connectNodeRed(resolve, reject, commandStr);
  88. });
  89. console.log(promise);
  90. return promise;
  91. },
  92. // 连接node red的websocket
  93. connectNodeRed: function (resolve, reject, commandStr) {
  94. var _self = this;
  95. let printers = [];
  96. _self.webSocket = new WebSocket(this.socketUrl);
  97. _self.webSocket.onopen = function (event) {
  98. console.log("打印 Websocket 连接成功。");
  99. if (commandStr != null && commandStr.length > 0) {
  100. _self.sendCommand(commandStr);
  101. } else {
  102. _self.close();
  103. }
  104. };
  105. _self.webSocket.onclose = function (event) {
  106. console.log("打印 Websocket 断开连接。");
  107. resolve(printers);
  108. };
  109. _self.webSocket.onerror = function (event) {
  110. console.log("打印 Websocket 出错。");
  111. _self.close();
  112. reject();
  113. };
  114. _self.webSocket.onmessage = function (event) {
  115. var data = JSON.parse(event.data);
  116. if (data.command == 'getPrinters') {
  117. printers = data.data;
  118. }
  119. if (data.command == 'printPreview') {
  120. printers = data.previewImages;
  121. }
  122. _self.close();
  123. };
  124. },
  125. /**
  126. * WebSocket 发送指令
  127. * @param {指令} command
  128. */
  129. sendCommand: function (command) {
  130. if (this.webSocket) {
  131. this.webSocket.send(command);
  132. }
  133. },
  134. /**
  135. * 关闭Websocket
  136. */
  137. close: function () {
  138. if (this.webSocket) {
  139. this.webSocket.close()
  140. this.webSocket = null;
  141. };
  142. },
  143. }