| | |
| | | <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"> |
| | |
| | | </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> |
| | |
| | | </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); |
| | | } |
| | | }, |
| | | }); |
| | |
| | | let imageJsonObj = jcapi.generateLabelJson(); |
| | | |
| | | // 设置打印任务 |
| | | jcapi.startJob({ |
| | | jcapi.startJob( |
| | | { |
| | | totalCount: 1, |
| | | density: this.printDensity, |
| | | labelType: this.labelType, |
| | |
| | | }, 20000); |
| | | |
| | | jcapi.printData( |
| | | imageJsonObj, { |
| | | imageJsonObj, |
| | | { |
| | | printQuantity: 1, |
| | | }, |
| | | function(r) { |
| | | if (r.code != 0) { |
| | | // 恢复原始回调 |
| | | jcapi.didReadPrintCountInfo( |
| | | originalPrintCountCallback); |
| | | jcapi.didReadPrintCountInfo(originalPrintCountCallback); |
| | | // 结束当前打印任务 |
| | | reject(new Error(r.msg || "打印失败")); |
| | | } |
| | |
| | | } |
| | | }); |
| | | }, |
| | | 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({ |