blePrintMixin.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { mapState, mapActions } from "vuex";
  2. import { FakeConnectedDevice } from "@psdk/frame-father";
  3. import bluetoothTool from "@/plugins/BluetoothTool.js";
  4. import {
  5. TBar,
  6. TBarCode,
  7. TQRCode,
  8. TBox,
  9. TImage,
  10. TPage,
  11. TRotation,
  12. TCodeType,
  13. TLine,
  14. TText,
  15. TFont,
  16. TTLine,
  17. } from "@psdk/tspl";
  18. ///转成安卓有符号的
  19. function uint8ArrayToSignedArray(uint8Array) {
  20. let signedArray = new Array(uint8Array.length);
  21. for (let i = 0; i < uint8Array.length; i++) {
  22. if (uint8Array[i] >= 128) {
  23. signedArray[i] = uint8Array[i] - 256;
  24. } else {
  25. signedArray[i] = uint8Array[i];
  26. }
  27. }
  28. return signedArray;
  29. }
  30. async function sendMessage(cmd) {
  31. console.log(cmd);
  32. const result = bluetoothTool.sendByteData(cmd);
  33. uni.showToast({
  34. icon: "none",
  35. title: result ? "发送成功!" : "发送失败...",
  36. });
  37. }
  38. export default {
  39. name: "blePrintMixin",
  40. computed: {
  41. ...mapState({
  42. discoveredDevices: (state) => state.ble.discoveredDevices, // 已发现的蓝牙设备
  43. connectedBleDevice: (state) => state.ble.connectedBleDevice,
  44. connectedDeviceId: (state) => state.ble.connectedDeviceId, // 已连接的蓝牙设备ID
  45. }),
  46. },
  47. data() {
  48. return {
  49. bleErrorTipPop: {
  50. show: false,
  51. content: "未连接 蓝牙",
  52. extraText: "请连接蓝牙之后再继续操作",
  53. },
  54. // 蓝牙打印弹框
  55. blePrintPop: {
  56. show: false,
  57. data: {},
  58. },
  59. // 选择蓝牙设备弹框
  60. bleSelectPop: {
  61. show: false,
  62. discoveredDevices: [],
  63. },
  64. };
  65. },
  66. mounted() {},
  67. methods: {
  68. ...mapActions("ble", [
  69. "checkBleStatus", // 检查蓝牙设备连接状态
  70. "discovery", // 搜索蓝牙设备
  71. "connectBT", // 连接蓝牙设备
  72. "cancelDiscovery", // 停止蓝牙搜索
  73. ]),
  74. bleErrorTipConfirm() {
  75. this.bleErrorTipPop.show = false;
  76. this.openBleSelectPop();
  77. },
  78. //打开蓝牙选择弹框
  79. openBleSelectPop() {
  80. this.discovery(); // 搜索蓝牙设备
  81. this.bleSelectPop.show = true;
  82. },
  83. //关闭蓝牙选择弹框
  84. closeBleSelectPop() {
  85. this.cancelDiscovery();
  86. this.bleSelectPop.show = false;
  87. },
  88. //选择蓝牙设备
  89. async handleSelectBle(item) {
  90. console.log("选择蓝牙设备=====", item);
  91. const device = await this.connectBT(item);
  92. if (device) {
  93. this.closeBleSelectPop();
  94. this.$printer.init(new FakeConnectedDevice()); // 初始化打印机
  95. this.blePrintPop.show = true;
  96. }
  97. console.log("this.blePrintPop.data=====", this.blePrintPop.data);
  98. },
  99. // 打开蓝牙打印弹框
  100. async openBlePrintPop(data) {
  101. const res = await this.checkBleStatus(); // 检查蓝牙设备打开状态
  102. this.blePrintPop.data = data;
  103. switch (res.code) {
  104. case 0:
  105. // const devices = bluetoothTool.getPairedDevices(); // 获取已配对的蓝牙设备
  106. // console.log("devices======", devices);
  107. if (this.connectedDeviceId != "") {
  108. this.blePrintPop.show = true;
  109. } else {
  110. // 如果没有配对的设备,提示用户去配对
  111. this.bleErrorTipPop.show = true;
  112. }
  113. break;
  114. case 1:
  115. uni.showToast({
  116. icon: "none",
  117. title: "请打开蓝牙",
  118. });
  119. break;
  120. case 2:
  121. uni.showToast({
  122. icon: "none",
  123. title: "当前设备不支持蓝牙",
  124. });
  125. break;
  126. }
  127. },
  128. // 重新搜索蓝牙设备
  129. refreshBleDevice() {
  130. this.discovery();
  131. },
  132. /**
  133. * 打印
  134. *@data {Object} 需要打印的数据
  135. * */
  136. async writeTsplModel(data) {
  137. const vm = this;
  138. const obj = {
  139. qrCode: data.qrCode, // 二维码
  140. barCode: data.barCode, // 商品条码
  141. customerName: data.customerName, //客户名
  142. number: data.number, // 单据编号
  143. batchNumber: data.batchNumber, // 批次条码
  144. };
  145. try {
  146. const tspl = await vm.$printer
  147. .tspl()
  148. .clear()
  149. .page(
  150. new TPage({
  151. width: 76,
  152. height: 130,
  153. })
  154. )
  155. .qrcode(
  156. new TQRCode({
  157. x: 50,
  158. y: 20,
  159. content: "283423982494284284828484",
  160. cellWidth: 10,
  161. })
  162. )
  163. .text(
  164. new TText({
  165. x: 50 + 30,
  166. y: 20,
  167. content: obj.customerName,
  168. font: TFont.TSS24,
  169. })
  170. )
  171. .text(
  172. new TText({
  173. x: 50 + 30,
  174. y: 20 + 35,
  175. content: obj.number,
  176. font: TFont.TSS24,
  177. })
  178. )
  179. .barcode(
  180. new TBarCode({
  181. x: 50,
  182. y: 20 + 35 + 20,
  183. cellWidth: 2,
  184. height: 60,
  185. content: obj.barCode,
  186. rotation: TRotation.ROTATION_0,
  187. codeType: TCodeType.CODE128,
  188. showType: 2,
  189. })
  190. )
  191. .text(
  192. new TText({
  193. x: 50 + 20,
  194. y: 50 + 100,
  195. content: obj.barCode,
  196. font: TFont.TSS24,
  197. })
  198. )
  199. .print();
  200. var binary = tspl.command().binary();
  201. await sendMessage(Array.from(uint8ArrayToSignedArray(binary)));
  202. } catch (e) {
  203. console.error(e);
  204. uni.showToast({
  205. title: "失败",
  206. });
  207. }
  208. },
  209. async writeData({ num = 1, data }) {
  210. for (let i = 0; i < num; i++) {
  211. await this.writeTsplModel(data);
  212. }
  213. },
  214. },
  215. };