pages/wareHouse/nuclearScale/nuclearscalerecord.vue
@@ -97,31 +97,34 @@
         </scroll-view>
      </view>
      <u-toast ref="uToast" />
      <u-modal v-model="maskShow" title="" v-for="(item, index) in listDevice" :key="index"
         :show-confirm-button="true" @confirm="tapQuery(item)">
         <view class="packing-registration-param-view" @touchmove.stop.prevent="moveHandle" @click="maskclose">
      <u-modal v-model="maskShow" title=""
         :show-confirm-button="false" >
         <view  @touchmove.stop.prevent="moveHandle" @click="maskclose">
            <scroll-view class="uni-scroll_box" scroll-y @touchmove.stop.prevent="moveHandle"
               @click.stop="moveHandle">
               <view class="packing-registration-param-item param-extra">
                  <view class="packing-registration-param-item-left">
                     <text class="item-one">名称:</text>
               <view class="modal-title">选择蓝牙设备</view>
               <view v-if="listDevice.length === 0" class="empty-tip">暂无设备</view>
               <view v-for="(item, index) in listDevice" :key="index" @click="tapQuery(item)" class="device-item">
                  <view class="device-name">
                     <text>名称:</text>
                     <text>{{ item.name || '未知设备' }}</text>
                  </view>
                  <view class="packing-registration-param-item-right">
                     <text class="item-one">{{ item.name }}</text>
                  </view>
               </view>
               <view class="packing-registration-param-item param-extra">
                  <view class="packing-registration-param-item-left">
                     <text class="item-one">UUID:</text>
                  </view>
                  <view class="packing-registration-param-item-right">
                     <text class="item-one">{{ item.address }}</text>
                  <view class="device-uuid">
                     <text>UUID:</text>
                     <text>{{ item.address || '无地址' }}</text>
                  </view>
               </view>
            </scroll-view>
         </view>
      </u-modal>
            <!-- </scroll-view>
         </view>
      </u-modal> -->
      <!-- 连接和打印状态指示器 -->
      <view class="status-indicator" :class="{'connected': connectionStatus === 'connected', 'connecting': connectionStatus === 'connecting', 'disconnected': connectionStatus === 'disconnected'}">
         <text class="status-text">{{ getConnectionStatusText() }}</text>
         <text class="print-status" v-if="printStatus !== 'idle'">- {{ getPrintStatusText() }}</text>
      </view>
   </view>
</template>
<script>
@@ -169,9 +172,10 @@
            },
            maskShow: false,
            listDevice: [], //蓝牙设备数据
            pritList: [],
            printQueue: [], // 打印队列
            printing: false, // 是否正在打印
            printDensity: 3, // 默认打印浓度
            labelType: 1, // 默认标签类型 *
            labelType: 1, // 默认标签类型
            // 1:间隙纸
            // 2:黑标纸
            // 3:连续纸
@@ -180,7 +184,10 @@
            printMode: 2, // 默认打印模式
            //   1:热敏
            //    2:热转印
            connectedDevice: null // 已连接的打印机
            connectedDevice: null, // 已连接的打印机
            connectionStatus: 'disconnected', // 连接状态: disconnected, connecting, connected
            printStatus: 'idle', // 打印状态: idle, printing, error
            errorMessage: '' // 错误信息
         };
      },
      onLoad() {
@@ -263,25 +270,110 @@
               this.getlist();
            }
         },
         // 打印单个信息
         // 检查蓝牙连接状态
         async checkBluetoothConnection() {
            if (!this.connectedDevice) return false;
            console.log('disconnected1111111111111')
            if (!this.connectedDevice) {
               this.connectionStatus = 'disconnected';
               return false;
            }
            console.log('disconnected2222222222')
            // 尝试发送一个简单命令检查连接状态
            // 设置为连接中状态
            this.connectionStatus = 'connecting';
            console.log('disconnected33333333333')
            // 增强连接状态检测
            return new Promise(resolve => {
               jcapi.checkPrinterStatus(this.connectedDevice.address, r => {
                  if (r && r.code === 0) {
               // 简化连接状态检测,直接使用已记录的连接状态
               if (this.connectedDevice) {
                  this.connectionStatus = 'connected';
                     resolve(true);
                  } else {
                     // 连接已断开
                     this.connectedDevice = null;
                  this.connectionStatus = 'disconnected';
                  uni.showToast({
                     icon: 'none',
                     title: '打印机未连接,请先连接',
                     duration: 2000
                  });
                     resolve(false);
                  }
               });
            });
         },
         // 添加到打印队列
         addToPrintQueue(items) {
            if (!Array.isArray(items)) {
               items = [items];
            }
            this.printQueue.push(...items);
            // 如果不在打印中,则开始处理队列
            if (!this.printing) {
               this.processPrintQueue();
            }
         },
         // 处理打印队列
         async processPrintQueue() {
            while (this.printQueue.length > 0) {
               console.log('111111111111111', this.printQueue.length)
               // 检查连接状态
               console.log('12222222222222222')
               const isConnected = await this.checkBluetoothConnection();
               console.log('isConnected',isConnected)
               if (!isConnected) {
                  // 如果未连接,显示蓝牙连接对话框
                  this.searchDevice();
                  // 不是直接return,而是等待用户连接后再继续处理
                  return;
               }
               this.printing = true;
               this.printStatus = 'printing';
               // 取出队列中的第一个任务
               const item = this.printQueue.shift();
               console.log('打印的任务',item)
               try {
                  // 执行打印
                  await this.printQrCode(item);
                  // 打印成功后继续处理下一个
                  uni.showToast({
                     title: '打印成功',
                     icon: 'success',
                     duration: 1000
                  });
                  // 短暂延迟,避免打印机缓存溢出
                  await new Promise(resolve => setTimeout(resolve, 1000));
               } catch (error) {
                  console.error('打印出错:', error);
                  this.printStatus = 'error';
                  this.errorMessage = error.message || '打印时发生未知错误';
                  this.printing = false;
                  // 记录失败的任务,不重新添加到队列
                  this.failedPrintJobs = this.failedPrintJobs || [];
                  this.failedPrintJobs.push(item);
                  uni.showToast({
                     title: `打印失败: ${this.errorMessage}`,
                     icon: 'none',
                     duration: 2000
                  });
                  // 继续处理下一个任务
                  continue;
               }
            }
            this.printing = false;
            this.printStatus = 'idle';
         },
         // 打印单个信息
         selectNo(item) {
            let that = this
            uni.showModal({
@@ -290,45 +382,24 @@
               showCancel: true,
               success: async function(res) {
                  if (res.confirm) {
                     // 检查蓝牙连接状态
                     const isConnected = await that.checkBluetoothConnection();
                     if (isConnected) {
                        // 已连接,直接打印
                        that.pritList = [item]
                        that.pritList.forEach(async item => {
                           await that.printQrCode(item)
                        })
                     } else {
                        // 未连接或连接已断开,搜索设备
                        that.searchDevice()
                        that.pritList = [item]
                     }
                     // 添加到打印队列
                     that.addToPrintQueue(item);
                  }
               }
            });
         },
         // 打印全部信息
         goPrintAll() {
            let that = this
            uni.showModal({
               title: '提示',
               content: '是否打印装箱单?',
               content: `是否打印全部${that.list.length}条装箱单?`,
               showCancel: true,
               success: async function(res) {
                  if (res.confirm) {
                     // 检查蓝牙连接状态
                     const isConnected = await that.checkBluetoothConnection();
                     if (isConnected) {
                        // 已连接,直接打印
                        that.pritList = that.list
                        that.pritList.forEach(async item => {
                           await that.printQrCode(item)
                        })
                     } else {
                        // 未连接或连接已断开,搜索设备
                        that.searchDevice()
                        that.pritList = that.list
                     }
                     // 添加到打印队列
                     that.addToPrintQueue([...that.list]);
                  }
               }
            });
@@ -341,29 +412,33 @@
         },
         //打印二维码
         async printQrCode(item) {
            return new Promise(async (resolve, reject) => {
            console.log('item de shuju ', item)
            const params = {
               rwMatlBindId: item.id
            };
               // const params = {
               //    rwMatlBindId: item.id
               // };
            try {
               let res = await this.$u.api.dailyPaper.zongjianLabelInfo(params)
                  let res = {
                     data: {
                     ht:'1',
                     lj:'1',
                     ljms:'1',
                     fh:'1',
                     scdt:'1',
                     scph:'1',
                     fhdw:'1',
                     shdw:'1',
                  }}
                  // let res = await this.$u.api.dailyPaper.zongjianLabelInfo(params)
               if (!res.data) {
                  this.$refs.uToast.show({
                     title: '未查询到该标签信息!',
                     type: 'warning '
                  })
                     reject(new Error('未查询到该标签信息'));
                  return
               }
               // const res = {
               //    ht: 'HT20250717995352665',
               //    lj: '88.118.1/A0611091',
               //    ljms: '铜单线 裸铜-TY-圆形-3.0',
               //    fh: '1083.6',
               //    scdt: '2025-07-14 13:46:55.0',
               //    scph: '2025071273-3',
               //    fhdw: '江东合金技术有限公司',
               //    shdw: '昆山泰德通铜业有限公司',
               // }
               // 从API响应中提取所需数据
               const {
                  ht,
@@ -375,7 +450,6 @@
                  fhdw,
                  shdw
               } = res.data
               // const jsonString = JSON.stringify(res.data)
               // 初始化画板
               jcapi.initDrawingBoard({
@@ -396,8 +470,8 @@
                  rotate: 0,
                  codeType: QrCodeType.QrCode,
               });
               // getDrawLabelLine   (x坐标,y坐标,线条宽度,线条高度,旋转角度,线条类型)
               // 绘制横线 实线8条
                  // 绘制横线
               this.getDrawLabelLine(1, 1, 67, 0.5, 0, LineType.Solid)
               this.getDrawLabelLine(1, 6, 42, 0.3, 0, LineType.Solid)
               this.getDrawLabelLine(1, 11, 42, 0.3, 0, LineType.Solid)
@@ -408,14 +482,14 @@
               this.getDrawLabelLine(1, 37, 67, 0.3, 0, LineType.Solid)
               this.getDrawLabelLine(1, 42, 67, 0.3, 0, LineType.Solid)
               this.getDrawLabelLine(1, 47, 67, 0.5, 0, LineType.Solid)
               //绘制竖线
               this.getDrawLabelLine(1, 1, 0.5, 46, 0, LineType.Solid) // 竖线1
               this.getDrawLabelLine(43, 1, 0.3, 26, 0, LineType.Solid) // 竖线2
               this.getDrawLabelLine(68, 1, 0.5, 46, 0, LineType.Solid) // 竖线2
               // getDrawLabelText   (1.x坐标,2.y坐标,3.文本框宽度,4.文本框高度,5.绘制文字内容,6.字体大小(默认高度4mm),7.旋转角度,8.换行方式,9.⾏间隔,10.字间隔,11.文字水平布局,12文字垂直布局)
               // 标题
                  this.getDrawLabelLine(68, 1, 0.5, 46, 0, LineType.Solid) // 竖线3
                  // 绘制文本
               this.getDrawLabelText(3, 2.5, 40, 8, '中天科技装箱清单', 2.5, 0, LineModeType.AutoHeight, 0, 0, 1, 1, 1)
               // 填冲文字标题
               this.getDrawLabelText(2, 7, 25, 8, '合同号:', 2.5, 0, LineModeType.AutoHeight, 0, 0, 0, 1, 1)
               this.getDrawLabelText(2, 12, 25, 8, '零件号:', 2.5, 0, LineModeType.AutoHeight, 0, 0, 0, 1, 1)
               this.getDrawLabelText(2, 17, 25, 8, '零件描述:', 2.5, 0, LineModeType.AutoHeight, 0, 0, 0, 1, 1)
@@ -424,83 +498,106 @@
               this.getDrawLabelText(2, 32.5, 25, 8, '生产批号:', 2.5, 0, LineModeType.AutoHeight, 0, 0, 0, 1, 1)
               this.getDrawLabelText(2, 37.5, 25, 8, '发货单位:', 2.5, 0, LineModeType.AutoHeight, 0, 0, 0, 1, 1)
               this.getDrawLabelText(2, 42.5, 25, 8, '收货单位:', 2.5, 0, LineModeType.AutoHeight, 0, 0, 0, 1, 1)
               // 填冲参数值
                  // 填充参数值
               this.getDrawLabelText(11, 7, 30, 8, ht, 2.5, 0, LineModeType.AutoHeight, 0, 0, 0, 0, 1)
               this.getDrawLabelText(11, 12, 32, 8, lj, 2.5, 0, LineModeType.AutoHeight, 0, 0, 0, 0, 1)
               this.getDrawLabelText(14, 17, 30, 8, ljms, 2.5, 0, LineModeType.AutoHeight, 0, 0, 0, 0, 1)
               this.getDrawLabelText(17, 22.5, 27, 8, fh, 2.5, 0, LineModeType.AutoHeight, 0, 0, 0, 0, 1)
               this.getDrawLabelText(14, 27.5, 55, 8, scdt, 2.5, 0, LineModeType.AutoHeight, 0, 0, 0, 0, 1)
               this.getDrawLabelText(14, 32.5, 55, 8, scph, 2.5, 0, LineModeType.AutoHeight, 0, 0, 0, 0, 1)
                  this.getDrawLabelText(14, 32.7, 55, 8, scph, 2.5, 0, LineModeType.AutoHeight, 0, 0, 0, 0, 1)
               this.getDrawLabelText(14, 37.5, 55, 8, fhdw, 2.5, 0, LineModeType.AutoHeight, 0, 0, 0, 0, 1)
               this.getDrawLabelText(14, 42.5, 55, 8, shdw, 2.5, 0, LineModeType.AutoHeight, 0, 0, 0, 0, 1)
               // 生成打印数据
               let imageJsonObj = jcapi.generateLabelJson()
               let imageJsonObj111 = jcapi.generatePrintPreviewImage(imageJsonObj)
               console.log('预览', imageJsonObj111)
               // // 设置打印任务
                  console.log('打印的数据',imageJsonObj)
                  // 设置打印任务
               jcapi.startJob({
                  totalCount: 1,
                  density: this.printDensity,
                  labelType: this.labelType,
                  printMode: this.printMode,
               }, function(r) {
                  console.log('打印', r)
                  if (r.code == 0) {
                     // 打印数据
                        // 存储原始打印计数回调
                        const originalPrintCountCallback = jcapi.didReadPrintCountInfo._callback;
                        // 临时打印计数回调,用于确认打印完成
                        const tempPrintCountCallback = (printResult) => {
                           console.log("打印计数回调:", printResult);
                           // 恢复原始回调
                           jcapi.didReadPrintCountInfo(originalPrintCountCallback);
                           // 结束当前打印任务
                           // 解析Promise表示打印完成
                           resolve();
                        };
                        // 设置临时回调
                        jcapi.didReadPrintCountInfo(tempPrintCountCallback);
                        // 添加打印超时处理
                        const printTimeout = setTimeout(() => {
                           jcapi.didReadPrintCountInfo(originalPrintCountCallback);
                           reject(new Error('打印超时未响应'));
                        }, 10000);
                     jcapi.printData(imageJsonObj, {
                        "printQuantity": 1,
                     }, function(r) {
                        if (r.code == 0) {
                           console.log("打印成功")
                           uni.showToast({
                              title: '打印成功',
                              icon: 'success'
                           })
                        } else {
                           console.log("打印失败")
                           uni.showToast({
                              title: '打印失败: ' + (r.msg || '未知错误'),
                              icon: 'none'
                           })
                           if (r.code != 0) {
                              console.log("打印失败", r);
                              // 恢复原始回调
                              jcapi.didReadPrintCountInfo(originalPrintCountCallback);
                              // 结束当前打印任务
                              reject(new Error(r.msg || '打印失败'));
                        }
                     })
                  } else {
                     console.log("设置打印任务失败")
                  }
                           // ��印数据发送成功后等待打印计数回调确认实际打印完成
                        })
                     } else {
                        console.log("设置打印任务失败", r)
                        reject(new Error(r.msg || '设置打印任务失败'));
                     }
               })
            } catch (error) {
               console.log(error)
                  reject(error);
            }
            });
         },
         tapQuery(item) {
            this.maskShow = false
            // 连接打印机
            let _this = this;
            console.log(item)
            // 设置为连接中状态
            this.connectionStatus = 'connecting';
            jcapi.openPrinterByDevice({
               address: item.address,
               name: item.name,
               deviceType: 0 // 设备类型:0-蓝牙,1-网络
            }, function(r) {
               uni.showToast({
                  title: (r.code == 0) ? "连接成功" : "连接失败",
                  duration: 2 * 1000
               })
               if (r.code == 0) {
                  _this.connectedDevice = item
                  // 添加打印列表空值检查
                  if (!_this.pritList || _this.pritList.length === 0) {
                  _this.connectionStatus = 'connected';
                     uni.showToast({
                        title: '没有可打印的数据',
                        icon: 'none'
                     title: "连接成功",
                     duration: 1500
                     })
                     return
                  // 开始处理打印队列
                  console.log('打印队列',_this.printQueue)
                  if (_this.printQueue.length > 0) {
                     _this.processPrintQueue();
                  }
                  // 绘标签进行打印
                  _this.pritList.forEach(async item => {
                     await _this.printQrCode(item)
               } else {
                  _this.connectionStatus = 'disconnected';
                  uni.showToast({
                     title: "连接失败: " + (r.msg || '未知错误'),
                     icon: 'none',
                     duration: 2000
                  })
               }
            })
@@ -542,7 +639,7 @@
               }
            })
         },
         // 绘制横线 实线
         // 绘制横线 ���线
         getDrawLabelLine(x, y, width, height, rotate, lineType) {
            jcapi.drawLabelLine({
               x: x,
@@ -575,6 +672,28 @@
         },
         //刷新上一个页面
         getConnectionStatusText() {
            switch(this.connectionStatus) {
               case 'connected':
                  return '蓝牙已连接';
               case 'connecting':
                  return '蓝牙连接中...';
               default:
                  return '蓝牙未连接';
            }
         },
         getPrintStatusText() {
            switch(this.printStatus) {
               case 'printing':
                  const total = this.printQueue.length + 1; // 队列中剩余 + 当前正在打印
                  const current = total - this.printQueue.length;
                  return `打印中(${current}/${total})`;
               case 'error':
                  return `打印错误: ${this.errorMessage}`;
               default:
                  return '就绪';
            }
         },
         refreshLastPage(no) {
            // 告知 A.vue 更新数据
            // 获取页面栈
@@ -731,4 +850,87 @@
      line-height: 36rpx;
      margin-right: 32rpx;
   }
   /* 模态框样式优化 */
   .u-modal {
      --modal-width: 85% !important;
      --modal-radius: 16rpx !important;
      --modal-bg-color: #ffffff !important;
   }
   /* 模态框内容区域 */
   .uni-scroll_box {
      height: 500rpx !important;
      padding: 20rpx;
   }
   /* 设备列表项样式 */
   .device-item {
      background-color: #f5f9ff;
      border-radius: 12rpx;
      padding: 20rpx;
      margin-bottom: 16rpx;
      box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
   }
   /* 设备名称样式 */
   .device-name {
      font-size: 28rpx;
      font-weight: 500;
      color: #333333;
      margin-bottom: 10rpx;
      display: flex;
      justify-content: space-between;
   }
   /* UUID文本样式 */
   .device-uuid {
      font-size: 24rpx;
      color: #666666;
      word-break: break-all;
   }
   /* 模态框标题 */
   .modal-title {
      font-size: 32rpx;
      font-weight: bold;
      color: #333333;
      text-align: center;
      margin-bottom: 20rpx;
      padding-bottom: 10rpx;
      border-bottom: 1rpx solid #eeeeee;
   }
   /* 空状态提示 */
   .empty-tip {
      text-align: center;
      padding: 100rpx 0;
      color: #999999;
      font-size: 28rpx;
   }
   .status-indicator {
      padding: 8rpx 16rpx;
      font-size: 24rpx;
      text-align: center;
      color: #fff;
      background-color: #666;
      &.connected {
         background-color: #07c160;
      }
      &.connecting {
         background-color: #ff976a;
      }
      &.disconnected {
         background-color: #f53f3f;
      }
      .print-status {
         margin-left: 10rpx;
         font-size: 22rpx;
      }
   }
</style>