yyb
20 小时以前 fc1a5059e586ab6d41fc3ec3bfacb727accc4765
核磅录入打印装箱单
已添加1个文件
已修改1个文件
417 ■■■■■ 文件已修改
common/bluetoothPrinter.js 102 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pages/wareHouse/nuclearScale/nuclearscalerecord.vue 315 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
common/bluetoothPrinter.js
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,102 @@
const jcapi = uni.requireNativePlugin("JCSDK-JCApiModule");
let state = {
  connectedDevice: null,
  connectionStatus: "disconnected", // disconnected | connecting | connected
  listeners: [],
};
/** é€šçŸ¥æ‰€æœ‰è®¢é˜…页面 */
function notify() {
  state.listeners.forEach(fn => fn({
    connectedDevice: state.connectedDevice,
    connectionStatus: state.connectionStatus,
  }));
}
/** é¡µé¢è®¢é˜…蓝牙状态 */
function onChange(fn) {
  state.listeners.push(fn);
  fn({
    connectedDevice: state.connectedDevice,
    connectionStatus: state.connectionStatus,
  });
}
/** åˆå§‹åŒ–(全局只需一次) */
function init() {
  jcapi.initSDK();
  const saved = uni.getStorageSync("bluetoothConnection");
  if (saved) {
    state.connectedDevice = saved;
    state.connectionStatus = "connected";
  }
  // æ–­å¼€ç›‘听
  jcapi.didReadPrintErrorInfo((r) => {
    if (r.code === 23) {
      state.connectedDevice = null;
      state.connectionStatus = "disconnected";
      uni.removeStorageSync("bluetoothConnection");
      notify();
    }
  });
}
/** æœç´¢è®¾å¤‡ */
function searchDevices() {
  return new Promise((resolve, reject) => {
    uni.openBluetoothAdapter({
      success() {
        jcapi.getBluetoothDevices(list => {
          resolve(list || []);
        });
      },
      fail: reject
    });
  });
}
/** è¿žæŽ¥è®¾å¤‡ */
function connect(device) {
  return new Promise((resolve, reject) => {
    state.connectionStatus = "connecting";
    notify();
    jcapi.openPrinterByDevice({
      address: device.address,
      name: device.name,
      deviceType: 0,
    }, (r) => {
      if (r.code === 0) {
        state.connectedDevice = device;
        state.connectionStatus = "connected";
        uni.setStorageSync("bluetoothConnection", device);
        notify();
        resolve(device);
      } else {
        state.connectionStatus = "disconnected";
        notify();
        reject(r);
      }
    });
  });
}
function isConnected() {
  return state.connectionStatus === "connected";
}
function getCurrentDevice() {
  return state.connectedDevice;
}
export default {
  init,
  onChange,
  searchDevices,
  connect,
  isConnected,
  getCurrentDevice,
};
pages/wareHouse/nuclearScale/nuclearscalerecord.vue
@@ -1,28 +1,56 @@
<template>
    <view class="page">
        <view class="finishProductIn-locno-bg" />
        <u-navbar title="核磅记录" :background="background" :border-bottom="false" :title-bold="true" title-color="#000"
            back-icon-color="#000">
            <view class="navbar-right" slot="right" @click="goPrintAll">
                å…¨éƒ¨æ‰“印
    <u-navbar
      title=""
      :background="background"
      :border-bottom="false"
      :title-bold="true"
      title-color="#000"
      back-icon-color="#000"
    >
      <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">
            <!-- è¿žæŽ¥å’Œæ‰“印状态指示器 -->
            <view class="status-indicator" :class="{
      <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>
        <text class="print-status" v-if="printStatus !== 'idle'"
          >- {{ getPrintStatusText() }}</text
        >
            </view>
            <scroll-view class="finishProductIn-locno-scroll-list" scroll-y="true" @scrolltolower="getmoreList()">
                <u-cell-group class="finishProductIn-locno-scroll-list-group" :border="false">
                    <view class="content" v-for="(item, index) in list" :key="item.locNo" :index="index"
                        @click="selectNo(item)">
      <scroll-view
        class="finishProductIn-locno-scroll-list"
        scroll-y="true"
        @scrolltolower="getmoreList()"
      >
        <u-cell-group
          class="finishProductIn-locno-scroll-list-group"
          :border="false"
        >
          <view
            class="content"
            v-for="(item, index) in list"
            :key="item.locNo"
            :index="index"
            @click="selectNo(item)"
          >
                        <view class="content-header">
                            <view class="content-header-title">{{ list.length - index }}</view>
              <view class="content-header-title">{{
                list.length - index
              }}</view>
                        </view>
                        <view class="content-body">
                            <view class="row-list">
@@ -101,18 +129,31 @@
                    </view>
                </u-cell-group>
                <view class="loadmore" @click="getmoreList()">
                    <u-loadmore :status="status" :load-text="loadText" @loadmore="getmoreList()" />
          <u-loadmore
            :status="status"
            :load-text="loadText"
            @loadmore="getmoreList()"
          />
                </view>
            </scroll-view>
        </view>
        <u-toast ref="uToast" />
        <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">
        <scroll-view
          class="uni-scroll_box"
          scroll-y
          @touchmove.stop.prevent="moveHandle"
          @click.stop="moveHandle"
        >
                    <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
            v-for="(item, index) in listDevice"
            :key="index"
            @click="tapQuery(item)"
            class="device-item"
          >
                        <view class="device-name">
                            <text>名称:</text>
                            <text>{{ item.name || "未知设备" }}</text>
@@ -129,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");
    // äºŒç»´ç ç±»åž‹
@@ -174,7 +217,7 @@
                listDevice: [], //蓝牙设备数据
                printQueue: [], // æ‰“印队列
                printing: false, // æ˜¯å¦æ­£åœ¨æ‰“印
                printDensity: 3, // é»˜è®¤æ‰“印浓度
      printDensity: 10, // é»˜è®¤æ‰“印浓度
                labelType: 1, // é»˜è®¤æ ‡ç­¾ç±»åž‹
                // 1:间隙纸
                // 2:黑标纸
@@ -184,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: "",
@@ -196,74 +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";
@@ -311,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) {
@@ -412,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);
                        }
                    },
                });
@@ -773,7 +769,8 @@
                        let imageJsonObj = jcapi.generateLabelJson();
                        // è®¾ç½®æ‰“印任务
                        jcapi.startJob({
          jcapi.startJob(
            {
                                totalCount: 1,
                                density: this.printDensity,
                                labelType: this.labelType,
@@ -805,14 +802,14 @@
                                    }, 20000);
                                    jcapi.printData(
                                        imageJsonObj, {
                  imageJsonObj,
                  {
                                            printQuantity: 1,
                                        },
                                        function(r) {
                                            if (r.code != 0) {
                                                // æ¢å¤åŽŸå§‹å›žè°ƒ
                                                jcapi.didReadPrintCountInfo(
                                                    originalPrintCountCallback);
                      jcapi.didReadPrintCountInfo(originalPrintCountCallback);
                                                // ç»“束当前打印任务
                                                reject(new Error(r.msg || "打印失败"));
                                            }
@@ -829,85 +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();
        // å¦‚果有打印任务,继续打印
        if (this.printQueue.length) {
          this.processPrintQueue();
                            }
                        } else {
                            _this.connectionStatus = "disconnected";
      } catch (e) {
                            uni.showToast({
                                title: "连接失败: " + (r.msg || "未知错误"),
                                icon: "none",
                                duration: 2000,
          title: "连接失败",
                            });
                        }
                    }
                );
            },
            //打印相关
            searchDevice() {
                let _this = this;
                uni.openBluetoothAdapter({
                    // ç¡®è®¤è“ç‰™æ˜¯å¦æ‰“å¼€
                    success(r) {
                        uni.showLoading({
                            title: "搜索中...",
                        });
                        // æœªæŽˆäºˆè“ç‰™ç›¸å…³æƒé™å’Œæœªæ‰“开手机定位会搜索不到设备
                        jcapi.getBluetoothDevices(function(r) {
                            console.log("device:" + JSON.stringify(r));
                            // æœç´¢åˆ°è®¾å¤‡å›žè°ƒ
    async searchDevice() {
      try {
        uni.showLoading({ title: "搜索中..." });
        const devices = await bluetooth.searchDevices();
                            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) {
        this.listDevice = devices;
        this.maskShow = true;
      } catch (e) {
        uni.hideLoading();
                        uni.showModal({
                            title: "提示",
                            content: "打开蓝牙失败,请检查蓝牙是否开启",
          content: "请确认蓝牙和定位已开启",
                            showCancel: false,
                        });
                        console.log("开启蓝牙设备失败" + e);
      }
                    },
                });
            },
            // ç»˜åˆ¶æ¨ªçº¿
            getDrawLabelLine(x, y, width, height, rotate, lineType) {
                jcapi.drawLabelLine({