wtt
5 小时以前 fa64020eaeb43c489a0597bd74791a307d75b8e8
components/labelPrinterComponent/labelPrinterComponent.vue
@@ -1,10 +1,12 @@
<template>
   <view>
      <uni-popup ref="popup" type="bottom" popupshow @close="popupCloseHandler">
   <view class="container" :hidden="isPopup === false">
      <uni-popup ref="popup" type="bottom" popupshow @change="popupChange">
         <view style="height: 80vh; background-color: #fff; padding-bottom: 2rem;">
            <blueToothConnectorVue ref="bt"></blueToothConnectorVue>
         </view>
      </uni-popup>
      <!-- 使用uni-popup类的组件时,当popup不显示时,组件树中没有popup内的模组,无法使用ref调用模组内的函数,故需要在外部挂载一个透明的组件 -->
      <blueToothConnectorVue style="display: none;position: absolute; bottom: 0;right: 0;" ref="bt2"></blueToothConnectorVue>
   </view>
</template>
@@ -28,12 +30,16 @@
   import {
      CStatus
   } from '@psdk/cpcl';
   import {
      CommonUtils
   } from '../../utils/common';
import { nextTick } from "vue";
   export default {
      name: "lablePrinterComponent",
      data() {
         return {
            blueToothConnector: null,
            isPopup: false,
         };
      },
      props: {
@@ -50,15 +56,78 @@
         blueToothConnectorVue
      },
      methods: {
         openPopup() {
         async openPopup() {
            this.$refs.popup.open();
            // 在popup中的操作针对ref bt对应的组件,需要将bt组件的状态与bt2进行一次同步
            this.$nextTick(() => {
               this.blueToothConnector = this.$refs.bt
               this.$refs.bt2 = this.$refs.bt
            })
         },
         popupCloseHandler() {
         async popupChange(e) {
            this.isPopup = e['show']
            await this.$nextTick()
         },
         async batchPrint(commandList) {
            console.log(this.$refs.bt2)
            let vm = this;
            let binarys = [];
            for (let command of commandList) {
                    console.log('command: ',command);
               switch (vm.printMode) {
                  case "tspl":
                     let tspl = await vm.$printer.tspl().clear();
                     tspl.raw(Raw.text(command))
                     console.log("print command:\n" + tspl.command().string())
                     binarys.push(tspl.command().binary());
                     break;
                  case "cpcl":
                     let cpcl = await vm.$printer.cpcl().clear();
                     cpcl.raw(Raw.text(command))
                     console.log("print command:\n" + cpcl.command().string())
                     binarys.push(cpcl.command().binary());
                     break;
                  case "esc":
                     let esc = await vm.$printer.esc().clear();
                     esc.raw(Raw.text(command))
                     console.log("print command:\n" + esc.command().string())
                     binarys.push(esc.command().binary());
                     break;
                  default:
                     return uni.showToast({
                        icon: 'none',
                        title: 'printMode类型错误!'
                     })
               }
            }
            let sendSuccess = false
            for(let binary of binarys){
                    try{
                        sendSuccess = await this.$refs.bt2.sendMessage(binary);
                        console.log("当前发送是否成功: ", sendSuccess)
                    }catch(e) {
                        uni.showToast({
                            icon: 'none',
                            title: e
                        })
                        // 断开蓝牙连接
                        this.$refs.bt2.closeBluetooth()
                        // 停止传输
                        return
                    }
            }
                if(sendSuccess === false) {
                   return
                }else {
                    return uni.showToast({
                        icon: 'none',
                        title: '打印成功'
                    })
                }
         },
         async execPrint() {
            // 检查蓝牙连接
@@ -74,49 +143,53 @@
               this.showToast("无设备连接!")
               return
            }
            try {
               if (typeof this.printInfo === 'function') {
                  // 传入的printInfo是函数,直接执行
                  let msg = await this.printInfo()
                  msg = this.uint8ArrayToSignedArray(msg)
                  console.log(msg)
                  this.sendMessage(msg)
                  this.$refs.bt2.sendMessage(msg)
               } else if (typeof this.printInfo === 'string') {
                  // 传入的参数是字符串,则根据打印模式调用不同的打印API
                  // 这里的字符串需要对应打印模式
                  let printStr = this.printInfo.replace(/\n/g, "\r\n")
                  let vm = this;
                  let binary = null;
                  switch (vm.printMode) {
                     case "tspl":
                        const tspl = await vm.$printer.tspl().clear();
                        tspl.raw(Raw.text(printStr))
                        console.log("print command:\n" + tspl.command().string())
                        binary = tspl.command().binary();
                        await vm.blueToothConnector.sendMessage(binary);
                        break;
                     case "cpcl":
                        const cpcl = await vm.$printer.cpcl().clear();
                        cpcl.raw(Raw.text(printStr))
                        console.log("print command:\n" + cpcl.command().string())
                        binary = cpcl.command().binary();
                        await vm.blueToothConnector.sendMessage(binary);
                        break;
                     case "esc":
                        const esc = await vm.$printer.esc().clear();
                        esc.raw(Raw.text(printStr))
                        console.log("print command:\n" + esc.command().string())
                        binary = esc.command().binary();
                        await vm.blueToothConnector.sendMessage(binary);
                        break;
                     default:
                        return uni.showToast({
                           icon: 'none',
                           title: 'printMode类型错误!'
                        })
                  if (CommonUtils.isJson(this.printInfo) === true) {
                     // 如果需要批量传输,需要传JSON
                     let commandList = JSON.parse(this.printInfo)
                     for (let command of commandList) {
                        command.replace(/\n/g, "\r\n")
                     }
                     this.batchPrint(commandList)
                  } else {
                     let printStr = this.printInfo.replace(/\n/g, "\r\n")
                     let vm = this;
                     let binary = null;
                     switch (vm.printMode) {
                        case "tspl":
                           const tspl = await vm.$printer.tspl().clear();
                           tspl.raw(Raw.text(printStr))
                           console.log("print command:\n" + tspl.command().string())
                           binary = tspl.command().binary();
                           await this.$refs.bt2.sendMessage(binary);
                           break;
                        case "cpcl":
                           const cpcl = await vm.$printer.cpcl().clear();
                           cpcl.raw(Raw.text(printStr))
                           console.log("print command:\n" + cpcl.command().string())
                           binary = cpcl.command().binary();
                           await this.$refs.bt2.sendMessage(binary);
                           break;
                        case "esc":
                           const esc = await vm.$printer.esc().clear();
                           esc.raw(Raw.text(printStr))
                           console.log("print command:\n" + esc.command().string())
                           binary = esc.command().binary();
                           await this.$refs.bt2.sendMessage(binary);
                           break;
                        default:
                           return uni.showToast({
                              icon: 'none',
                              title: 'printMode类型错误!'
                           })
                     }
                  }
               } else {
                  uni.showToast({
@@ -137,8 +210,12 @@
   }
</script>
<style lang="scss">
<style lang="scss" scoped>
   * {
      box-sizing: border-box;
   }
   .container[hidden] {
      padding: 0 !important;
      height: 0 !important;
   }
</style>