yyb
6 小时以前 fc1a5059e586ab6d41fc3ec3bfacb727accc4765
pages/wareHouse/nuclearScale/nuclearscalerecord.vue
@@ -2,15 +2,18 @@
  <view class="page">
    <view class="finishProductIn-locno-bg" />
    <u-navbar
      title="核磅记录"
      title=""
      :background="background"
      :border-bottom="false"
      :title-bold="true"
      title-color="#000"
      back-icon-color="#000"
    >
      <view class="navbar-right" slot="right" @click="goPrintAll">
        全部打印
      <view class="navbar-right" slot="right">
        <text class="bluetooth-btn" @click="searchDevice">
          {{ connectedDevice ? `已连接:${connectedDevice.name}` : "连接蓝牙" }}
        </text>
        <text class="print-all-btn" @click="goPrintAll">全部打印</text>
      </view>
    </u-navbar>
    <view class="wrap">
@@ -45,7 +48,9 @@
            @click="selectNo(item)"
          >
            <view class="content-header">
              <view class="content-header-title">{{ index + 1 }}</view>
              <view class="content-header-title">{{
                list.length - index
              }}</view>
            </view>
            <view class="content-body">
              <view class="row-list">
@@ -165,6 +170,8 @@
</template>
<script>
import content_bg from "@/static/custom/finishProductIn/locNoBg.png";
import bluetooth from "@/common/bluetoothPrinter.js";
const jcapi = uni.requireNativePlugin("JCSDK-JCApiModule");
// 二维码类型
@@ -210,7 +217,7 @@
      listDevice: [], //蓝牙设备数据
      printQueue: [], // 打印队列
      printing: false, // 是否正在打印
      printDensity: 3, // 默认打印浓度
      printDensity: 10, // 默认打印浓度
      labelType: 1, // 默认标签类型
      // 1:间隙纸
      // 2:黑标纸
@@ -220,8 +227,8 @@
      printMode: 2, // 默认打印模式
      //   1:热敏
      //    2:热转印
      connectedDevice: null, // 已连接的打印机
      connectionStatus: "disconnected", // 连接状态: disconnected, connecting, connected
      connectedDevice: null,
      connectionStatus: "disconnected",
      printStatus: "idle", // 打印状态: idle, printing, error
      errorMessage: "", // 错误信息
      verificationNo: "",
@@ -232,75 +239,43 @@
    this.verificationNo = JSON.parse(
      decodeURIComponent(options.verificationNo)
    );
    bluetooth.init();
    bluetooth.onChange(({ connectedDevice, connectionStatus }) => {
      this.connectedDevice = connectedDevice;
      this.connectionStatus = connectionStatus;
    });
    this.getlist();
    // 初始化SDK
    jcapi.initSDK();
  },
    // 尝试恢复之前的蓝牙连接状态
    this.restoreBluetoothConnection();
    // 监听页码回调
    jcapi.didReadPrintCountInfo(function (r) {
      console.log("页码", r);
    });
    // 监听错误回调
    jcapi.didReadPrintErrorInfo((r) => {
      console.log("错误", r);
      if (r.code == 23) {
        // 打印机断开连接
        this.connectedDevice = null;
        // 清除本地存储的连接信息
        uni.removeStorageSync("bluetoothConnection");
        uni.showToast({
          icon: "none",
          title: "打印机连接已断开,请重新连接",
          duration: 2 * 1000,
        });
      } else {
        uni.showToast({
          icon: "none",
          title: JSON.stringify(r),
          duration: 2 * 1000,
        });
      }
    });
  // 页面卸载时不主动断开蓝牙连接,保持连接状态
  onUnload() {
    // 不执行断开连接操作,保持蓝牙连接
  },
  methods: {
    restoreBluetoothConnection() {
      try {
        // 从本地存储获取之前保存的连接信息
        const savedConnection = uni.getStorageSync("bluetoothConnection");
        if (savedConnection && savedConnection.address) {
          console.log("尝试恢复蓝牙连接:", savedConnection);
          // 设置为连接中状态
          this.connectionStatus = "connecting";
    // restoreBluetoothConnection() {
    //    try {
    //       // 从本地存储获取之前保存的连接信息
    //       const savedConnection = uni.getStorageSync("bluetoothConnection");
    //       if (savedConnection && savedConnection.address) {
    //          console.log("尝试恢复蓝牙连接:", savedConnection);
    //          // 直接设置为已连接状态,避免不必要的重连
    //          // 实际连接状态会通过SDK的回调自动更新
    //          this.connectedDevice = savedConnection;
    //          this.connectionStatus = "connected";
    //          console.log("蓝牙连接状态已恢复");
          // 尝试重新连接
          jcapi.openPrinterByDevice(
            {
              address: savedConnection.address,
              name: savedConnection.name,
              deviceType: 0, // 设备类型:0-蓝牙,1-网络
            },
            (r) => {
              if (r.code == 0) {
                this.connectedDevice = savedConnection;
                this.connectionStatus = "connected";
                console.log("蓝牙连接已恢复");
              } else {
                this.connectionStatus = "disconnected";
                // 如果重连失败,清除保存的连接信息
                uni.removeStorageSync("bluetoothConnection");
                console.log("蓝牙重连失败:", r);
              }
            }
          );
        }
      } catch (e) {
        console.error("恢复蓝牙连接失败:", e);
      }
    },
    //          // 验证连接是否真的有效,通过发送一个简单的命令或检查状态
    //          // 这里不执行实际的重连操作,而是依赖SDK的连接状态回调
    //          // 如果连接已断开,SDK会通过didReadPrintErrorInfo回调通知我们
    //       }
    //    } catch (e) {
    //       console.error("恢复蓝牙连接失败:", e);
    //       this.connectionStatus = "disconnected";
    //    }
    // },
    getmoreList() {
      if (this.pageSize >= this.total) {
        this.status = "nomore";
@@ -348,33 +323,15 @@
        this.getlist();
      }
    },
    // 检查蓝牙连接状态
    async checkBluetoothConnection() {
      if (!this.connectedDevice) {
        this.connectionStatus = "disconnected";
        return false;
      }
      // 设置为连接中状态
      this.connectionStatus = "connecting";
      // 增强连接状态检测
      return new Promise((resolve) => {
        // 简化连接状态检测,直接使用已记录的连接状态
        if (this.connectedDevice) {
          this.connectionStatus = "connected";
          resolve(true);
        } else {
          this.connectionStatus = "disconnected";
          uni.showToast({
            icon: "none",
            title: "打印机未连接,请先连接",
            duration: 2000,
          });
          resolve(false);
        }
      });
    checkBluetoothConnection() {
      return bluetooth.isConnected();
    },
    //    // 直接使用已记录的连接状态,避免不必要的连接检查
    //    // 实际连接状态会通过SDK的回调自动更新
    //    this.connectionStatus = "connected";
    //    return true;
    // },
    // 添加到打印队列
    addToPrintQueue(items) {
@@ -449,11 +406,13 @@
      uni.showModal({
        title: "提示",
        content: "是否打印装箱单?",
        showCancel: true,
        success: async function (res) {
        success(res) {
          if (res.confirm) {
            // 添加到打印队列
            that.addToPrintQueue(item);
            // 按 batchNo 合并
            const sameBatchList = that.list.filter(
              (i) => i.batchNo === item.batchNo
            );
            that.addToPrintQueue(sameBatchList);
          }
        },
      });
@@ -536,8 +495,8 @@
          this.getDrawLabelLine(1, 6, 42, 0.3, 0, LineType.Solid);
          this.getDrawLabelLine(1, 11, 42, 0.3, 0, LineType.Solid);
          this.getDrawLabelLine(1, 16, 42, 0.3, 0, LineType.Solid);
          this.getDrawLabelLine(1, 21, 42, 0.3, 0, LineType.Solid);
          this.getDrawLabelLine(1, 27, 67, 0.3, 0, LineType.Solid);
          this.getDrawLabelLine(1, 23, 42, 0.3, 0, LineType.Solid);
          this.getDrawLabelLine(1, 27.5, 67, 0.3, 0, LineType.Solid);
          this.getDrawLabelLine(1, 32, 67, 0.3, 0, LineType.Solid);
          this.getDrawLabelLine(1, 37, 67, 0.3, 0, LineType.Solid);
          this.getDrawLabelLine(1, 42, 67, 0.3, 0, LineType.Solid);
@@ -611,7 +570,7 @@
          );
          this.getDrawLabelText(
            2,
            22.5,
            23.5,
            25,
            8,
            "发货/装箱数:",
@@ -626,7 +585,7 @@
          );
          this.getDrawLabelText(
            2,
            27.5,
            28.5,
            25,
            8,
            "生产日期:",
@@ -733,7 +692,7 @@
          );
          this.getDrawLabelText(
            17,
            22.5,
            23.5,
            27,
            8,
            fh,
@@ -748,7 +707,7 @@
          );
          this.getDrawLabelText(
            14,
            27.5,
            28.5,
            55,
            8,
            scdt,
@@ -867,86 +826,43 @@
        }
      });
    },
    tapQuery(item) {
    async tapQuery(item) {
      this.maskShow = false;
      // 连接打印机
      let _this = this;
      try {
        await bluetooth.connect(item);
        uni.showToast({ title: "连接成功" });
      // 设置为连接中状态
      this.connectionStatus = "connecting";
      jcapi.openPrinterByDevice(
        {
          address: item.address,
          name: item.name,
          deviceType: 0, // 设备类型:0-蓝牙,1-网络
        },
        function (r) {
          // 在tapQuery方法中,连接成功时添加保存连接信息的代码
          if (r.code == 0) {
            _this.connectedDevice = item;
            _this.connectionStatus = "connected";
            // 保存连接信息到本地存储
            uni.setStorageSync("bluetoothConnection", item);
            uni.showToast({
              title: "连接成功",
              duration: 1500,
            });
            // 开始处理打印队列
            if (_this.printQueue.length > 0) {
              _this.processPrintQueue();
            }
          } else {
            _this.connectionStatus = "disconnected";
            uni.showToast({
              title: "连接失败: " + (r.msg || "未知错误"),
              icon: "none",
              duration: 2000,
            });
          }
        // 如果有打印任务,继续打印
        if (this.printQueue.length) {
          this.processPrintQueue();
        }
      );
      } catch (e) {
        uni.showToast({
          icon: "none",
          title: "连接失败",
        });
      }
    },
    //打印相关
    searchDevice() {
      let _this = this;
      uni.openBluetoothAdapter({
        // 确认蓝牙是否打开
        success(r) {
          uni.showLoading({
            title: "搜索中...",
          });
          // 未授予蓝牙相关权限和未打开手机定位会搜索不到设备
          jcapi.getBluetoothDevices(function (r) {
            console.log("device:" + JSON.stringify(r));
            // 搜索到设备回调
            uni.hideLoading();
            // 如果之前有连接的设备但不在搜索结果中,说明设备已更换
            if (
              _this.connectedDevice &&
              !r.some((dev) => dev.address === _this.connectedDevice.address)
            ) {
              _this.connectedDevice = null;
              uni.showToast({
                icon: "none",
                title: "已连接的蓝牙设备已更换,请重新选择",
                duration: 2000,
              });
            }
            _this.listDevice = r;
            _this.maskShow = true;
          });
        },
        fail(e) {
          uni.showModal({
            title: "提示",
            content: "打开蓝牙失败,请检查蓝牙是否开启",
            showCancel: false,
          });
          console.log("开启蓝牙设备失败" + e);
        },
      });
    async searchDevice() {
      try {
        uni.showLoading({ title: "搜索中..." });
        const devices = await bluetooth.searchDevices();
        uni.hideLoading();
        this.listDevice = devices;
        this.maskShow = true;
      } catch (e) {
        uni.hideLoading();
        uni.showModal({
          title: "提示",
          content: "请确认蓝牙和定位已开启",
          showCancel: false,
        });
      }
    },
    // 绘制横线
    getDrawLabelLine(x, y, width, height, rotate, lineType) {
      jcapi.drawLabelLine({