| | |
| | | <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"> |
| | |
| | | @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"> |
| | |
| | | </template> |
| | | <script> |
| | | import content_bg from "@/static/custom/finishProductIn/locNoBg.png"; |
| | | import bluetooth from "@/common/bluetoothPrinter.js"; |
| | | |
| | | const jcapi = uni.requireNativePlugin("JCSDK-JCApiModule"); |
| | | |
| | | // 二维码类型 |
| | |
| | | listDevice: [], //蓝牙设备数据 |
| | | printQueue: [], // 打印队列 |
| | | printing: false, // 是否正在打印 |
| | | printDensity: 3, // 默认打印浓度 |
| | | printDensity: 10, // 默认打印浓度 |
| | | labelType: 1, // 默认标签类型 |
| | | // 1:间隙纸 |
| | | // 2:黑标纸 |
| | |
| | | printMode: 2, // 默认打印模式 |
| | | // 1:热敏 |
| | | // 2:热转印 |
| | | connectedDevice: null, // 已连接的打印机 |
| | | connectionStatus: "disconnected", // 连接状态: disconnected, connecting, connected |
| | | connectedDevice: null, |
| | | connectionStatus: "disconnected", |
| | | printStatus: "idle", // 打印状态: idle, printing, error |
| | | errorMessage: "", // 错误信息 |
| | | verificationNo: "", |
| | |
| | | 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"; |
| | |
| | | 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) { |
| | |
| | | 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); |
| | | } |
| | | }, |
| | | }); |
| | |
| | | const lj = item.partNo; |
| | | const ljms = item.ccontrProModel; |
| | | const fh = item.newNetWeight; |
| | | const scdt = item.outboundDate; |
| | | const scdt = item.productionDate; |
| | | const scph = item.batchNo; |
| | | const shdw = item.customerName; |
| | | const fhdw = "江东合金技术有限公司"; |
| | |
| | | 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); |
| | |
| | | ); |
| | | this.getDrawLabelText( |
| | | 2, |
| | | 22.5, |
| | | 23.5, |
| | | 25, |
| | | 8, |
| | | "发货/装箱数:", |
| | |
| | | ); |
| | | this.getDrawLabelText( |
| | | 2, |
| | | 27.5, |
| | | 28.5, |
| | | 25, |
| | | 8, |
| | | "生产日期:", |
| | |
| | | ); |
| | | this.getDrawLabelText( |
| | | 17, |
| | | 22.5, |
| | | 23.5, |
| | | 27, |
| | | 8, |
| | | fh, |
| | |
| | | ); |
| | | this.getDrawLabelText( |
| | | 14, |
| | | 27.5, |
| | | 28.5, |
| | | 55, |
| | | 8, |
| | | scdt, |
| | |
| | | } |
| | | }); |
| | | }, |
| | | 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({ |