index.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <z-paging ref="paging" bgColor="#f7f7f7" safe-area-inset-bottom scroll-with-animation>
  3. <template #top>
  4. <ut-navbar :title="stepsObject[currentStep].title" :fixed="false" border :breadcrumb="false"></ut-navbar>
  5. <view class="pd-5"></view>
  6. </template>
  7. <view>
  8. <view v-show="currentStep === 'range'">
  9. <range-print :info="stepsObject[currentStep]" :opts="opts" @next="nextSteps" @formdata="getFormData" nextStepValue="connect"></range-print>
  10. </view>
  11. <view v-show="currentStep === 'connect'" class="bg-fff">
  12. <!-- 业务组件链接打印机 -->
  13. <connect-printer :info="stepsObject[currentStep]" @next="nextSteps" @prev="prevSteps" prevStepValue="range" nextStepValue="print"></connect-printer>
  14. </view>
  15. <view v-show="currentStep === 'print'" class="bg-fff">
  16. <!-- 业务组件打印 -->
  17. <!-- 打印写入打印机 -->
  18. <print-wrapper :info="stepsObject[currentStep]" :printCount="form?.printCount" @test="printTest" @prev="prevSteps" @print="printCodes" prevStepValue="connect"></print-wrapper>
  19. </view>
  20. </view>
  21. </z-paging>
  22. </template>
  23. <script setup lang="ts">
  24. import ConnectPrinter from '../models/connect-printer.vue';
  25. import { tem30x30, tem60x40, tem60x60 } from '../models/print-tem';
  26. import PrintWrapper from '../models/print-wrapper.vue';
  27. import { writeBLECharacteristicSend } from '@/utils/blue-device-services';
  28. import jpPrinter from '../models/tspl';
  29. import { useClientRequest } from '@/utils/request';
  30. import RangePrint from '../models/range-print.vue';
  31. const printSpec = ref('30x30');
  32. const printCodes = async (data: any) => {
  33. printSpec.value = data.printSpec;
  34. // 检查是否正在打印
  35. if (isPrinting.value) {
  36. uni.showToast({ icon: 'none', title: '当前打印任务未完成,请稍后' });
  37. return;
  38. }
  39. isPrinting.value = true;
  40. getPrintCodeList();
  41. };
  42. // 打印列表
  43. const getPrintCodeList = async () => {
  44. const res = await useClientRequest
  45. .get(`/plt-api/app/printLog/getPrintCodeList/${opts.value?.id}`, {
  46. start: form.value?.start,
  47. end: form.value?.end,
  48. })
  49. .catch((err) => {
  50. isPrinting.value = false;
  51. });
  52. if (!res || res.code !== 200) {
  53. isPrinting.value = false;
  54. return;
  55. }
  56. prepareSend(res?.data, printSpec.value)
  57. };
  58. const stepsObject = reactive<any>({
  59. range: {
  60. title: '区间打印',
  61. value: 'range',
  62. status: 'in-progress', // 为开始、进行中、已完成 三种状态分别值为 'not-started'、'in-progress'、'completed'
  63. sort: 1,
  64. },
  65. connect: {
  66. title: '连接打印机',
  67. value: 'connect',
  68. status: 'not-started', // 为开始、进行中、已完成 三种状态分别值为 'not-started'、'in-progress'、'completed'
  69. sort: 2,
  70. },
  71. print: {
  72. title: '打印',
  73. value: 'print',
  74. status: 'not-started', // 为开始、进行中、已完成 三种状态分别值为 'not-started'、'in-progress'、'completed'
  75. sort: 3,
  76. },
  77. });
  78. const currentStep = ref('connect');
  79. const isPrinting = ref(false);
  80. const opts = ref<any>(null);
  81. onLoad((options: any) => {
  82. console.log(options);
  83. opts.value = options;
  84. getBatchInfo()
  85. });
  86. const nextSteps = (data: any) => {
  87. if (!data.info || !data?.info?.value) return;
  88. stepsObject[data?.info?.value] = {
  89. ...data.info,
  90. };
  91. stepsObject[data?.nextStepValue].status = 'in-progress';
  92. currentStep.value = data?.nextStepValue;
  93. };
  94. const info = ref<any>(null);
  95. const form = ref<any>(null);
  96. const getFormData = (data: any) => {
  97. console.log(data);
  98. form.value = data;
  99. };
  100. const prevSteps = (data: any) => {
  101. currentStep.value = data?.prevStepValue;
  102. };
  103. const testList = reactive([
  104. {
  105. padSn: '0000002',
  106. prefix: 'PCC1W1260311',
  107. sn: 2,
  108. traceCode: 'PCC1W12603110000002AV',
  109. url: 'https://t.yujin.shuziyunyao.com/pt?c=PCC1W12603110000002AV',
  110. },
  111. ]);
  112. const printTest = (data: any) => {
  113. const device = stepsObject['connect']?.device;
  114. if (!device) {
  115. uni.showToast({ icon: 'none', title: '未连接设备' });
  116. return;
  117. }
  118. // 检查是否正在打印
  119. if (isPrinting.value) {
  120. uni.showToast({ icon: 'none', title: '当前打印任务未完成,请稍后' });
  121. return;
  122. }
  123. isPrinting.value = true;
  124. // 测试数据
  125. prepareSend(testList, data.printSpec);
  126. };
  127. // 打印写入
  128. const prepareSend = (list: any[], spec: string = '30x30') => {
  129. if (spec == '30x30') {
  130. writeSend30x30(list);
  131. }
  132. if (spec == '60x40') {
  133. // 40x40
  134. writeSendCommand(list, '60x40', 0);
  135. }
  136. if (spec == '60x60') {
  137. // 30x30
  138. writeSendCommand(list, '60x60', 0);
  139. }
  140. };
  141. const writeSend30x30 = (list: any[]) => {
  142. // arr两个两个一组,偶数组没有是空
  143. const arr = list.reduce((acc: any, cur: any, idx: number) => {
  144. if (idx % 2 == 0) {
  145. acc.push([cur]);
  146. } else {
  147. acc[acc.length - 1].push(cur);
  148. }
  149. return acc;
  150. }, []);
  151. writeSendCommand(arr, '30x30', 0);
  152. };
  153. // writeSend
  154. const writeSendCommand = async (list: any[], spec: string = '30x30', idx = 0) => {
  155. // 首次调用时设置打印状态为进行中
  156. let command = new jpPrinter();
  157. if (spec == '30x30') {
  158. // 判断idx是否是奇数
  159. command.setSize(60, 30);
  160. command.setGap(10);
  161. command.setCls();
  162. command.setDirection(1);
  163. command = tem30x30(command, list[idx], info.value);
  164. }
  165. if (spec == '60x40') {
  166. // 判断idx是否是奇数
  167. command.setSize(60, 40);
  168. command.setGap(10);
  169. command.setCls();
  170. command.setDirection(1);
  171. command = tem60x40(command, list[idx], info.value);
  172. }
  173. if (spec == '60x60') {
  174. // 判断idx是否是奇数
  175. command.setSize(60, 30);
  176. command.setGap(10);
  177. command.setCls();
  178. command.setDirection(1);
  179. command = tem60x60(command, list[idx], info.value);
  180. }
  181. command.setPagePrint();
  182. const device = stepsObject['connect']?.device;
  183. if (!device) {
  184. uni.showToast({ icon: 'none', title: '未连接设备' });
  185. return;
  186. }
  187. console.log(command.getData());
  188. await writeBLECharacteristicSend({
  189. deviceId: device.deviceId,
  190. serviceId: device.serviceId,
  191. characteristicId: device.characteristicId,
  192. uint8Array: command.getData(),
  193. });
  194. if (list.length - 1 > idx) {
  195. writeSendCommand(list, spec, idx + 1);
  196. } else {
  197. // 打印完成,重置状态
  198. isPrinting.value = false;
  199. uni.showToast({
  200. icon: 'none',
  201. title: '打印完成',
  202. });
  203. }
  204. };
  205. // 获取批次信息接口
  206. const getBatchInfo = async () => {
  207. const res = await useClientRequest.get(`/plt-api/app/packTask/getPackTraceSimpleInfo/${opts.value?.packId}`);
  208. if (!res?.data) return
  209. info.value = res?.data;
  210. }
  211. // 如果是 30x30
  212. </script>
  213. <style lang="scss" scoped></style>