diff --git a/manifest.json b/manifest.json index 67ea116..8ae953e 100644 --- a/manifest.json +++ b/manifest.json @@ -2,8 +2,8 @@ "name" : "危废数据终端", "appid" : "__UNI__F18DD0A", "description" : "", - "versionName" : "1.0.6", - "versionCode" : 106, + "versionName" : "1.0.8", + "versionCode" : 108, "transformPx" : false, /* 5+App特有相关 */ "app-plus" : { diff --git a/pages/weigh/generate.vue b/pages/weigh/generate.vue index 17f2486..2e95739 100644 --- a/pages/weigh/generate.vue +++ b/pages/weigh/generate.vue @@ -124,10 +124,24 @@ export default { this.form.productionDate = getDateTime() this.initData() }, + onShow() { + // 页面显示时静默连接蓝牙秤(未配置或失败不弹窗) + this.connectScaleSilently() + }, + mounted() { + // 注册蓝牙秤实时监听回调 + scaleService.onWeightChange(this.handleWeightChange) + scaleService.onConnectionChange(this.handleConnectionChange) + }, + beforeDestroy() { + // 注销蓝牙秤监听回调 + scaleService.offWeightChange(this.handleWeightChange) + scaleService.offConnectionChange(this.handleConnectionChange) + }, onUnload() { - // 退出页面时断开蓝牙打印机和称重设备连接 - this.handleDisconnect() - }, + // 退出页面时断开蓝牙打印机和称重设备连接 + this.handleDisconnect() + }, methods: { async initData() { await this.gethwType() @@ -187,14 +201,14 @@ export default { this.form.handlerName = handler.name }, /** - * 获取重量 - * 从蓝牙秤获取当前重量并填入表单 + * 获取重量(实时监听模式) + * 蓝牙秤连接后会自动推送重量数据,无需发送读取指令。 * * 流程: * 1. 检查是否已配置称重设备 → 未配置则引导去设置页 - * 2. 检查蓝牙秤连接状态 → 未连接则自动连接 - * 3. 发送 'R' 命令读取重量(5秒超时) - * 4. 解析返回数据填入 form.weight + * 2. 检查蓝牙秤连接状态 → 已连接则提示实时称重中 + * 3. 未连接则调用 autoConnect() 建立连接 + * 4. 连接成功后设备通过 notify 主动推送数据,handleWeightChange 自动填入表单 */ async getWeight() { if (!this.scaleDeviceId || !this.scaleServiceId || !this.scaleCharacteristicId) { @@ -210,62 +224,56 @@ export default { return; } - uni.showToast({ title: '正在获取重量...', icon: 'none' }); + // 已连接状态:无需重复连接,重量通过 notify 实时推送 + if (scaleService.isConnected && this.scaleConnected) { + uni.showToast({ title: '已开启实时称重', icon: 'none' }); + return; + } + + uni.showToast({ title: '正在连接称重设备...', icon: 'none' }); try { - if (!scaleService.isConnected || !this.scaleConnected) { - await scaleService.autoConnect(); - this.scaleConnected = true; - } - - // ============================================================ - // Step 3: 发送读取命令 'R' (0x52),等待设备返回数据 - // readWeight() 内部流程: - // - 向 WriteCharacteristic 写入 0x52 - // - 启动 5 秒超时定时器 - // - 等待 NotifyCharacteristic 收到设备响应 - // - 调用 _parseCustom() 解析返回数据 - // - // 【调试】如果解析失败(value=null),会显示原始HEX/ASCII数据, - // 你可以根据实际数据修改 _parseCustom() 中的协议解析逻辑 - // ============================================================ - const result = await scaleService.readWeight(); - - if (result.value !== null && !isNaN(result.value)) { - // 解析成功:将重量值填入表单 - this.form.weight = String(result.value); - uni.showToast({ title: `重量: ${result.value} ${result.unit}`, icon: 'success' }); - } else { - // 解析失败:显示原始数据便于调试 - console.warn('【蓝牙秤】原始返回数据:', result); - uni.showModal({ - title: '无法解析重量', - content: `原始数据:\nHEX: ${result.rawHex || '-'}\nASCII: ${result.rawAscii || '-'}\n\n请检查设备协议格式`, - showCancel: false - }); - } - + const success = await scaleService.autoConnect(); + this.scaleConnected = success; + uni.showToast({ title: success ? '已连接,实时称重中' : '连接失败', icon: success ? 'success' : 'none' }); } catch (error) { - console.error('【蓝牙秤】获取重量失败:', error); - - // 提取错误信息(兼容 Error 对象和 uni-app 错误对象) - const errMsg = error.message || error.errMsg || JSON.stringify(error); - - // 根据错误类型给出不同提示 - let toastMsg = '获取重量失败'; - if (errMsg.includes('未配置')) { - toastMsg = '请先配置称重设备'; - } else if (errMsg.includes('未连接') || errMsg.includes('连接失败') || errMsg.includes('already connect')) { - toastMsg = '蓝牙连接失败,请检查设备是否开机'; - } else if (errMsg.includes('超时') || errMsg.includes('time out')) { - toastMsg = '连接超时,请检查蓝牙设备'; - } else if (errMsg.includes('invalid data')) { - toastMsg = '数据格式错误'; - } - - uni.showToast({ title: toastMsg, icon: 'none', duration: 2000 }); + console.error('【蓝牙秤】连接失败:', error); + this.scaleConnected = false; + uni.showToast({ title: '连接失败', icon: 'none' }); } }, + + /** + * 静默连接蓝牙秤(未配置或失败不弹窗) + * 在 onShow 时调用,用于页面切回时自动恢复连接 + */ + async connectScaleSilently() { + if (!this.scaleDeviceId || !this.scaleServiceId || !this.scaleCharacteristicId) return; + if (scaleService.isConnected && this.scaleConnected) return; + try { + const success = await scaleService.autoConnect(); + this.scaleConnected = success; + } catch (e) { + console.warn('【蓝牙秤】静默连接失败:', e); + } + }, + + /** + * 蓝牙秤实时重量回调 + * 设备通过 notify 主动推送数据时触发,自动填入表单 + */ + handleWeightChange(weight) { + if (weight && weight.value !== null && !isNaN(weight.value)) { + this.form.weight = String(weight.value); + } + }, + + /** + * 蓝牙秤连接状态变更回调 + */ + handleConnectionChange(connected) { + this.scaleConnected = connected; + }, /** * 危废类型选择变更 * @param {Object} e - picker change 事件对象 @@ -321,15 +329,15 @@ export default { tid: this.form.hwTypeId, time: getDateTime() } + + if (this.hwInfo.unit == '吨') { + submitData.weight = submitData.weight / 1000 + } + console.log('提交数据:', submitData) - // uni.showLoading({ title: '提交中...' }) api.hwGenerate({ ...submitData }).then(res => { console.log('hwGenerate res', res) - // "flow_no": "019e5ceb-fc23-70a1-b0e4-06ed6bc7d951", - // "digital_id": "12510100580027911690003949202605250008", - // "label_qrcode": "https://www.cswm.org.cn/qr/?code=7G6MOYELnFHZfF1Y0F8e6h4LCHuJYm7dR2hXRJkeKK7UoS8KYXm5UyRKUnh430z2O&type=WFBQ", - // "produce_ledger_no": "CS20260525000008" if (res.code == 1000) { uni.showToast({ title: '登记成功', icon: 'success' }) @@ -356,8 +364,6 @@ export default { this.currentPrintData = printData this.doPrint() } - }).finally(() => { - // uni.hideLoading() }) }, diff --git a/store/modules/print.js b/store/modules/print.js index 16b8bf0..3db7470 100644 --- a/store/modules/print.js +++ b/store/modules/print.js @@ -99,13 +99,10 @@ export default { // 断开打印机连接 async disconnectPrinter({ commit, dispatch }) { try { - const config = { - connected: false, - deviceId: '', - deviceName: '', - lastUpdated: new Date().toISOString() - }; - await dispatch('savePrinterConfig', config); + commit('SET_PRINTER_CONFIG', { + connected: false // ✅ 只更新连接状态 + }); + await dispatch('savePrinterConfig'); return true; } catch (error) { console.error('断开打印机失败:', error);