sub_pages/supplier/print/print-list.vue
@@ -1,7 +1,7 @@
<template>
   <!-- 显示要打印的东西 -->
   <view class="list-container order-delivery-print supplier">
      <view class="p15">
      <view class="p15" v-if="false">
         <view class="m-b-24 order-sale-list list-container">
            <view class="order-sale-item list-item">
               <view class="title flex">
@@ -14,7 +14,11 @@
                  </image>
                  <view class="flex1">
                     <view class=" flex">
                        <view class="title">{{item.flowerName }}
                        <view class="title">
                           <span class="level"
                              v-if="item.flowerCategory">{{ item.flowerCategory || ''}}</span><span
                              class="level">{{ item.flowerLevelStr || ''}}</span>
                           {{item.flowerName }}
                        </view>
                     </view>
                     <view class="each-list ">
@@ -43,11 +47,11 @@
               <view class="delivery-form">
                  <view class="form-item">
                     <view class="label">仓库名称</view>
                     <view class="value red">{{item.warehouseName}}</view>
                     <view class="value red">{{item.warehouseName||'待分配'}}</view>
                  </view>
                  <view class="form-item">
                     <view class="label">库位名称</view>
                     <view class="value red">{{item.warehouseLocationCode}}</view>
                     <view class="value red">{{item.warehouseLocationCode||'待分配'}}</view>
                  </view>
               </view>
@@ -65,7 +69,9 @@
      </view>
      <view class="print-container m-t-12">
         <button class="btn" type="primary" @click="startSearch" :loading="isScanning"> 开始搜索 </button>
         <button class="btn" type="primary" @click="startSearch" :loading="isScanning">
            开始搜索({{buffs&&buffs.length || 0}}个) </button>
         <button class="btn m-t-12" type="default" @click="breakPrint=true"> 中断打印 </button>
         <view v-for="(device,index) of list" :key="device.deviceId" @click="bindViewTap(device)">
            <view class="item">
               <div class="deviceId block">{{device.deviceId}}</div>
@@ -83,7 +89,8 @@
   export default {
      data() {
         return {
            item: {},
            // item: {},
            items: [],
            services: [],
            serviceId: 0,
            writeCharacter: false,
@@ -100,11 +107,11 @@
            returnResult: "",
            canvasWidth: 80,
            canvasHeight: 80,
            buffSize: [],
            buffIndex: 0,
            printNum: [],
            printNumIndex: 0,
            printerNum: 1,
            // buffSize: [],
            // buffIndex: 0,
            // printNum: [],
            // printNumIndex: 0,
            // printerNum: 1,
            currentPrint: 1,
            isReceiptSend: false,
            isLabelSend: false,
@@ -114,47 +121,94 @@
            notifyCharaterId: {},
            notifyServiceId: '',
            BserviceId: '',
            buffs: [],
            breakPrint: false
         }
      },
      /**
       * 生命周期函数--监听页面初次渲染完成
       */
      onReady: function() {
         var list = []
         var numList = []
         var j = 0
         for (var i = 20; i < 200; i += 10) {
            list[j] = i;
            j++
         }
         for (var i = 1; i < 10; i++) {
            numList[i - 1] = i
         }
         // var list = []
         // var numList = []
         // var j = 0
         // for (var i = 20; i < 400; i += 10) {
         //    list[j] = i;
         //    j++
         // }
         // for (var i = 1; i < 10; i++) {
         //    numList[i - 1] = i
         // }
         let that = this
         that.buffSize = list
         that.oneTimeData = list[0]
         that.printNum = numList
         that.printerNum = numList[0]
         // that.buffSize = list
         // that.oneTimeData = list[0]
         that.oneTimeData = 20
         // that.printNum = numList
         // that.printerNum = numList[0]
      },
      async onLoad() {
      async onLoad(options) {
         var t = this.$storage.getItem('cache_delivery_order_print') || ''
         if (t) {
            this.item = JSON.parse(t)
            this.items = JSON.parse(t)
            var requrl = ''
            // #ifdef PUB_SUPPLIER
            // apitype = '/supplier'
            if (options.source == 'detail') {
               requrl = `/api/supplier/delivery/list/items/views`
            } else {
               requrl = `/api/supplier/delivery/list/items/views/new`
            }
            // #endif
            // #ifndef PUB_SUPPLIER
            requrl = `/api/delivery/list/items/views`
            // #endif
            //获取详情接口
            this.$message.showLoading()
            const {
               code,
               data
            } = await this.$http.request('get', '/api/supplier/delivery/list/items/view', {
               params: {
                  id: this.item.id
               }
            })
            this.$message.hideLoading()
            if (code == 0 && data) {
               this.item = {
                  ...this.item,
                  ...data
            var ids = []
            for (var item of this.items) {
               if (item.id)
                  ids.push(item.id)
            }
            if (ids.length > 0) {
               this.$message.showLoading()
               // const {
               //    code,
               //    data
               // } = await this.$http.request('get', `/api${apitype}/delivery/list/items/view`, {
               //    params: {
               //       id: this.item.id,
               //       ids: ids
               //    }
               // })
               const {
                  code,
                  data
               } = await this.$http.request('post', requrl, {
                  data: {
                     // id: this.item.id,
                     ids: ids
                  }
               })
               this.$message.hideLoading()
               if (code == 0 && data && data.length > 0) {
                  // this.item = {
                  //    ...this.item,
                  //    ...data
                  // }
                  var tarr = []
                  for (var t of data) {
                     if (t) {
                        tarr.push(t)
                     }
                  }
                  if (tarr.length > 0)
                     this.items = tarr || []
                  console.log('items len:', this.items.length)
               }
            }
         } else {
@@ -324,6 +378,12 @@
          * 链接到某一个蓝牙设备上
          */
         bindViewTap: function(device) {
            if (this.isLabelSend) {
               this.$message.showToast('正在打印中')
               return
            }
            this.breakPrint = false
            var that = this
            wx.stopBluetoothDevicesDiscovery({ //关闭发现蓝牙设备
               success: function(res) {
@@ -476,67 +536,138 @@
               }
            })
         },
         openControl() {
         async openControl() {
            //打印
            var that = this;
            // console.log('openControl start')
            let that = this;
            var command = tsc.jpPrinter.createNew()
            command.setSize(48, 60)
            command.setGap(0)
            command.setCls()
            // command.setText(0, 30, "TSS24.BF2", 1, 1, "图片")
            // command.setQR(40, 120, "L", 5, "A", "www.smarnet.cc佳博智汇")
            // command.setText(60, 20, "TSS24.BF2", 1, 1, "单号:" + (that.item.orderNo || '-'))
            if (that.item.warehouseName)
               command.setText(10, 20, "TSS24.BF2", 1, 1, "仓库: " + that.item.warehouseName)
            if (that.item.warehouseLocationCode)
               command.setText(200, 20, "TSS24.BF2", 1, 1, "库位: " + that.item.warehouseLocationCode)
            // command.setText(170, 50, "TSS24.BF2", 1, 1, "小程序测试")
            // command.setText(170, 90, "TSS24.BF2", 1, 1, "测试数字12345678")
            // command.setText(170, 120, "TSS24.BF2", 1, 1, "测试英文abcdefg")
            // command.setText(170, 150, "TSS24.BF2", 1, 1, "测试符号/*-+!@#$")
            command.setText(10, 60, "TSS24.BF2", 1, 1, "供应商: " + (that.item.supplierName || '-'))
            command.setText(10, 100, "TSS24.BF2", 1, 1, "商品名称: " + (that.item.flowerName || '-'))
            command.setText(10, 140, "TSS24.BF2", 1, 1, "等级: " + (that.item.flowerLevelStr || '-'))
            command.setText(200, 140, "TSS24.BF2", 1, 1, "颜色: " + (that.item.flowerColor || '-'))
            // command.setText(10, 220, "TSS24.BF2", 1, 1, "价格: ¥ " + (that.item.price || '-'))
            command.setText(10, 180, "TSS24.BF2", 1, 1, "规格: " + (that.item.flowerUnit || '-'))
            command.setText(200, 180, "TSS24.BF2", 1, 1, "数目: x " + (that.item.num || '0'))
            // command.setText(10, 300, "TSS24.BF2", 1, 1, "花满芫")
            command.setText(100, 220, "TSS24.BF2", 1, 1, that.$util.toDateMin(new Date()))
            if (that.item.params) {
               var addnum = 0
               var hlen = 220
               for (var p of that.item.params) {
                  if (p.name !== '枝长' && p.name !== '成熟度') {
                     continue
                  }
                  if (p.value) {
                     var lef = 10
                     if (addnum % 2 == 1) {
                        lef = 190
                     } else {
                        lef = 10
                        hlen += 40
                     }
                     command.setText(lef, hlen, "TSS24.BF2", 1, 1, "" + (p.name || '') + ':' + p.value)
                     addnum += 1
                  }
               }
            var totalnum = 0
            for (var each of that.items) {
               totalnum += each.num || 1
            }
            // <view class="delivery-form" v-if="item.params">
            //    <view class="form-item" v-for="(p,k) of item.params" :key="k">
            //       <view class="label">{{p.name||'-'}}:</view>
            //       <view class="value red">{{p.value}}</view>
            //    </view>
            // </view>
            // command.setBar(170, 180, "EAN8", 64, 1, 3, 3, "1234567")
            if (totalnum <= 0) {
               that.$message.showToast('打印数目不能小于0')
               return
            }
            if (totalnum > 3) {
               await that.$message.confirm(`打印数目为${totalnum}张,确定打印吗`)
            }
            if (totalnum <= 0) {
               that.$message.showToast('打印数目不能小于0')
               return
            }
            that.buffs = []
            command.setPagePrint()
            for (var each of that.items) {
               for (var i = 0; i < (each.num || 1); i++) {
                  var command = tsc.jpPrinter.createNew()
                  //根据数目不同,打印不同数目
                  command.setSize(48, 60)
                  command.setGap(0)
                  command.setCls()
                  // command.setText(0, 30, "TSS24.BF2", 1, 1, "图片")
                  // command.setQR(40, 120, "L", 5, "A", "www.smarnet.cc佳博智汇")
                  // command.setText(60, 20, "TSS24.BF2", 1, 1, "单号:" + (each.orderNo || '-'))
                  // 仓库:
                  // if (each.warehouseName)
                  var paramsmap = {}
                  if (each.params) {
                     for (var p of each.params) {
                        if (p.name !== '枝长' && p.name !== '成熟度') {
                           continue
                        }
                        paramsmap[p.name] = p.value || ''
                     }
                  }
                  command.setText(30, 40, "TSS24.BF2", 1, 1, "" + (each.supplierName || '-'))
                  command.setText(200, 40, "TSS24.BF2", 1, 1, "" + (each.warehouseName || '待分配'))
                  var flowerCategory = each.flowerCategory || ''
                  if (flowerCategory) {
                     //只取二级分类
                     var tarr = flowerCategory.split("/")
                     if (tarr.length >= 2) {
                        flowerCategory = tarr[tarr.length - 1]
                     }
                  }
                  command.setText(30, 80, "TSS24.BF2", 1, 1, "" + (flowerCategory || ''))
                  command.setText(200, 80, "TSS24.BF2", 1, 1, "库位:" + (each.warehouseLocationCode || ''))
                  command.setText(30, 120, "TSS24.BF2", 1, 1, "等级:" + (each.flowerLevelStr || ''))
                  command.setText(200, 120, "TSS24.BF2", 1, 1, "" + (each.flowerColor || ''))
                  // 规格:
                  command.setText(30, 160, "TSS24.BF2", 1, 1, "" + (each.flowerUnit || '-'))
                  // 成熟度:
                  command.setText(200, 160, "TSS24.BF2", 1, 1, "" + (paramsmap['成熟度'] || '-'))
                  // 枝长:
                  command.setText(30, 200, "TSS24.BF2", 1, 1, "" + (paramsmap['枝长'] || '-'))
                  command.setText(200, 200, "TSS24.BF2", 1, 1, that.$util.toDate(new Date()))
                  command.setText(60, 230, "TSS24.BF2", 1, 1, "————————————")
                  command.setText(30, 260, "TSS24.BF2", 1, 1, "花满芫")
                  command.setText(200, 260, "TSS24.BF2", 1, 1, `数目: ${i+1}/${each.num||1}`)
                  // command.setText(20, 40, "TSS24.BF2", 1, 1, "" + (that.item.warehouseName || '待分配'))
                  // // 库位:
                  // // if (that.item.warehouseLocationCode)
                  // command.setText(200, 40, "TSS24.BF2", 1, 1, "" + (that.item.warehouseLocationCode || '待分配'))
                  // // command.setText(170, 50, "TSS24.BF2", 1, 1, "小程序测试")
                  // // command.setText(170, 90, "TSS24.BF2", 1, 1, "测试数字12345678")
                  // // command.setText(170, 120, "TSS24.BF2", 1, 1, "测试英文abcdefg")
                  // // command.setText(170, 150, "TSS24.BF2", 1, 1, "测试符号/*-+!@#$")
                  // // 供应商:
                  // command.setText(20, 80, "TSS24.BF2", 1, 1, "" + (that.item.supplierName || '-'))
                  // // 商品名称:
                  // command.setText(20, 120, "TSS24.BF2", 1, 1, "" + (that.item.flowerName || '-'))
                  // // 颜色:
                  // command.setText(200, 120, "TSS24.BF2", 1, 1, "" + (that.item.flowerColor || '-'))
                  // // command.setText(10, 220, "TSS24.BF2", 1, 1, "价格: ¥ " + (that.item.price || '-'))
                  // command.setText(20, 160, "TSS24.BF2", 1, 1, "规格: " + (that.item.flowerUnit || '-'))
                  // command.setText(200, 160, "TSS24.BF2", 1, 1, `数目: ${i+1}/${that.item.num}`)
                  // // command.setText(10, 300, "TSS24.BF2", 1, 1, "花满芫")
                  // command.setText(100, 200, "TSS24.BF2", 1, 1, that.$util.toDateMin(new Date()))
                  // if (that.item.params) {
                  //    var addnum = 0
                  //    var hlen = 200
                  //    for (var p of that.item.params) {
                  //       if (p.name !== '枝长' && p.name !== '成熟度') {
                  //          continue
                  //       }
                  //       if (p.value) {
                  //          var lef = 20
                  //          if (addnum % 2 == 1) {
                  //             lef = 190
                  //          } else {
                  //             lef = 20
                  //             hlen += 40
                  //          }
                  //          command.setText(lef, hlen, "TSS24.BF2", 1, 1, "" + (p.name || '') + ':' + p.value)
                  //          addnum += 1
                  //       }
                  //    }
                  // }
                  // <view class="delivery-form" v-if="item.params">
                  //    <view class="form-item" v-for="(p,k) of item.params" :key="k">
                  //       <view class="label">{{p.name||'-'}}:</view>
                  //       <view class="value red">{{p.value}}</view>
                  //    </view>
                  // </view>
                  // command.setBar(170, 180, "EAN8", 64, 1, 3, 3, "1234567")
                  command.setPagePrint()
                  // console.log('that.buffs', that.buffs.length)
                  that.buffs.push(command.getData())
               }
            }
            that.isLabelSend = true
            that.prepareSend(command.getData())
            // that.prepareSend(command.getData())
            that.prepareSend(that.buffs[0])
            // wx.canvasGetImageData({
            //   canvasId: 'edit_area_canvas',
@@ -571,8 +702,8 @@
            var loopTime = that.looptime
            var lastData = that.lastData
            var onTimeData = that.oneTimeData
            var printNum = that.printerNum
            var currentPrint = that.currentPrint
            // var printNum = that.printerNum
            let currentPrint = that.currentPrint
            var buf
            var dataView
            if (currentTime < loopTime) {
@@ -588,17 +719,21 @@
                  dataView.setUint8(i, buff[(currentTime - 1) * onTimeData + i])
               }
            }
            console.log("第" + currentTime + "次发送数据大小为:" + buf.byteLength)
            console.log("第" + currentTime + "次发送数据大小为:" + buf.byteLength, that.$util.toDateTime(new Date()))
            wx.writeBLECharacteristicValue({
               deviceId: that.deviceId,
               serviceId: that.writeServiceId,
               characteristicId: that.writeCharaterId,
               value: buf,
               success: function(res) {
                  console.log(res)
                  // console.log(res)
               },
               fail: function(e) {
                  console.log(e)
                  // wx.showToast({
                  //    title: '打印失败:' + e,
                  // })
               },
               complete: function() {
                  currentTime++
@@ -610,21 +745,40 @@
                     wx.showToast({
                        title: '已打印第' + currentPrint + '张',
                     })
                     // if (currentPrint == printNum) {
                     that.looptime = 0
                     that.lastData = 0
                     that.currentTime = 1
                     that.isReceiptSend = false
                     that.isLabelSend = false
                     that.currentPrint = 1
                     try {
                        wx.closeBLEConnection({
                           deviceId: that.deviceId,
                           success: function(res) {
                              console.log("关闭蓝牙成功")
                           },
                        })
                     } catch (e) {
                     if (currentPrint == that.buffs.length || that.breakPrint) {
                        if (that.breakPrint) {
                           wx.showToast({
                              title: '中断成功',
                           })
                           that.breakPrint = false
                        }
                        that.looptime = 0
                        that.lastData = 0
                        that.currentTime = 1
                        that.isReceiptSend = false
                        that.isLabelSend = false
                        that.currentPrint = 1
                        try {
                           wx.closeBLEConnection({
                              deviceId: that.deviceId,
                              success: function(res) {
                                 console.log("关闭蓝牙成功")
                              },
                           })
                        } catch (e) {
                        }
                     } else {
                        console.log('已打印第' + currentPrint + '张')
                        setTimeout(() => {
                           currentPrint++
                           that.currentPrint = currentPrint
                           that.currentTime = 1
                           // that.Send(that.buffs[that.currentPrint - 1])
                           that.prepareSend(that.buffs[that.currentPrint - 1])
                        }, 800)
                     }
@@ -734,6 +888,14 @@
               font-size: 28rpx;
               color: #000000;
               line-height: 40rpx;
               .level {
                  font-weight: 400;
                  font-size: 28rpx;
                  color: #20613D;
                  line-height: 40rpx;
                  margin-right: 20rpx;
               }
            }
            .status {
@@ -763,6 +925,14 @@
                  font-size: 28rpx;
                  color: #000000;
                  line-height: 40rpx;
                  .level {
                     font-weight: 400;
                     font-size: 28rpx;
                     color: #20613D;
                     line-height: 40rpx;
                     margin-right: 20rpx;
                  }
               }
               .desc {}