printer.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { Uuid } from 'pc-component-v3';
  2. import Common from './Common.js';
  3. export default {
  4. webSocket: null,
  5. // 获取mac地址
  6. getMacAddress: function () {
  7. const _self = this;
  8. const command = {
  9. id: Uuid.createUUID(),
  10. command: 'getMacAddress',
  11. };
  12. const commandStr = JSON.stringify(command);
  13. return new Promise((resolve, reject) => {
  14. _self.connectNodeRed(resolve, reject, commandStr);
  15. });
  16. },
  17. // 获取打印机
  18. getPrinters: function () {
  19. const _self = this;
  20. const command = {
  21. id: Uuid.createUUID(),
  22. command: 'getPrinters',
  23. };
  24. const commandStr = JSON.stringify(command);
  25. return new Promise((resolve, reject) => {
  26. _self.connectNodeRed(resolve, reject, commandStr);
  27. });
  28. },
  29. // 执行打印
  30. print: function (prnData) {
  31. const _self = this;
  32. const command = {
  33. id: Uuid.createUUID(),
  34. command: 'print',
  35. data: prnData,
  36. };
  37. const commandStr = JSON.stringify(command);
  38. return new Promise((resolve, reject) => {
  39. _self.connectNodeRed(resolve, reject, commandStr);
  40. });
  41. },
  42. // 取消打印
  43. cancelAllTask: function (prnData) {
  44. const _self = this;
  45. const command = {
  46. id: Uuid.createUUID(),
  47. command: 'cancelAllTask',
  48. data: prnData,
  49. };
  50. const commandStr = JSON.stringify(command);
  51. return new Promise((resolve, reject) => {
  52. _self.connectNodeRed(resolve, reject, commandStr);
  53. });
  54. },
  55. // 连接node red的websocket
  56. connectNodeRed: function (resolve, reject, commandStr) {
  57. const _self = this;
  58. let printers = null;
  59. let socketUrl = 'ws://127.0.0.1:10092';
  60. _self.webSocket = new WebSocket(socketUrl);
  61. _self.webSocket.onopen = function (event) {
  62. console.log('打印 Websocket 连接成功。');
  63. if (commandStr != null && commandStr.length > 0) {
  64. _self.sendCommand(commandStr);
  65. } else {
  66. _self.close();
  67. }
  68. };
  69. _self.webSocket.onclose = function (event) {
  70. console.log('打印 Websocket 断开连接。');
  71. resolve(printers);
  72. };
  73. _self.webSocket.onerror = function (event) {
  74. console.log('打印 Websocket 出错。');
  75. _self.close();
  76. reject();
  77. };
  78. _self.webSocket.onmessage = function (event) {
  79. const data = JSON.parse(event.data);
  80. printers = data;
  81. _self.close();
  82. };
  83. },
  84. // WebSocket 发送指令
  85. sendCommand: function (command) {
  86. if (this.webSocket) {
  87. this.webSocket.send(command);
  88. }
  89. },
  90. // 关闭Websocket
  91. close: function () {
  92. if (this.webSocket) {
  93. this.webSocket.close();
  94. this.webSocket = null;
  95. };
  96. },
  97. getRootPath: function () {
  98. var protocol = window.location.protocol;
  99. var host = window.location.hostname;
  100. var path = protocol + host;
  101. return path;
  102. },
  103. // 查询所有模板信息
  104. queryTemplates: function () {
  105. var requestUrl = 'CustomerCloudPrintTemplateResource/querySimpleAllTemplate';
  106. return new Promise((resolve, reject) => {
  107. $.ajax({
  108. url: Common.getApiURL(requestUrl),
  109. type: 'get',
  110. contentType: 'application/json',
  111. beforeSend: function (request) {
  112. Common.addTokenToRequest(request);
  113. },
  114. success: function (data) {
  115. resolve(data);
  116. },
  117. error: function (XMLHttpRequest, textStatus, errorThrown) {
  118. reject(XMLHttpRequest);
  119. },
  120. });
  121. });
  122. },
  123. };