This commit is contained in:
2026-06-27 20:58:16 +08:00
parent b13668095d
commit 451dd2330b
10 changed files with 1346 additions and 133 deletions
+42 -41
View File
@@ -69,10 +69,10 @@
<script>
import * as iconv from 'iconv-lite'
import { api } from '@/api'
import { getDateTime } from '@/utils/common.js'
import printerService from '@/utils/BluetoothPrinterService.js';
// 【蓝牙秤】引入蓝牙称重服务,用于从电子秤读取重量
import scaleService from '@/utils/BluetoothScaleService.js';
import { getDateTime, getDate } from '@/utils/common.js'
import { mapGetters } from 'vuex';
import printerService from '@/utils/BluetoothPrinterService.new.js';
import scaleService from '@/utils/BluetoothScaleService.new.js';
export default {
data() {
@@ -107,14 +107,20 @@ export default {
},
currentPrintData: null,
// 【蓝牙秤】蓝牙秤连接状态标记,避免重复连接/断开
scaleConnected: false
scaleConnected: false,
// 【打印机】蓝牙打印机连接状态标记(新增)
printerConnected: false
}
},
computed: {
...mapGetters('scale', ['scaleDeviceId', 'scaleServiceId', 'scaleCharacteristicId'])
},
/**
* 页面加载生命周期
* 初始化产生日期为当前时间,并默认选中危废类型、经办人、去向的第一项
*/
onLoad() {
this.$store.dispatch('scale/initScaleConfig'); // 初始化称重配置
this.form.productionDate = getDateTime()
this.initData()
},
@@ -191,13 +197,7 @@ export default {
* 4. 解析返回数据填入 form.weight
*/
async getWeight() {
// ============================================================
// Step 1: 检查是否已配置称重设备
// 配置存储在 LocalStorage 的 'scaleConfig' 中
// 如果没有配置,提示用户去「设置 > 设备管理 > 称重设备」页面配置
// ============================================================
const scaleConfig = uni.getStorageSync('scaleConfig');
if (!scaleConfig || !scaleConfig.deviceId) {
if (!this.scaleDeviceId || !this.scaleServiceId || !this.scaleCharacteristicId) {
uni.showModal({
title: '未配置称重设备',
content: '请先在设置中配置蓝牙称重设备,是否前往设置?',
@@ -213,11 +213,6 @@ export default {
uni.showToast({ title: '正在获取重量...', icon: 'none' });
try {
// ============================================================
// Step 2: 检查连接状态,未连接则自动连接
// autoConnect() 会从 LocalStorage 读取 deviceId/serviceId 等配置
// 然后调用底层 BluetoothManager 建立 BLE 连接
// ============================================================
if (!scaleService.isConnected || !this.scaleConnected) {
await scaleService.autoConnect();
this.scaleConnected = true;
@@ -253,17 +248,22 @@ export default {
} catch (error) {
console.error('【蓝牙秤】获取重量失败:', error);
// 提取错误信息(兼容 Error 对象和 uni-app 错误对象)
const errMsg = error.message || error.errMsg || JSON.stringify(error);
// 根据错误类型给出不同提示
let errMsg = '获取重量失败';
if (error.message.includes('未配置')) {
errMsg = '请先配置称重设备';
} else if (error.message.includes('未连接') || error.message.includes('连接失败')) {
errMsg = '蓝牙连接失败,请检查设备是否开机';
} else if (error.message.includes('超时')) {
errMsg = '读取超时,设备无响应';
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: errMsg, icon: 'none' });
uni.showToast({ title: toastMsg, icon: 'none', duration: 2000 });
}
},
/**
@@ -339,17 +339,17 @@ export default {
const printData = {
'mingcheng': hwInfo.waste_name, // 危废名称
'leibie': hwInfo.waste_category, // 危废类别
'daima': hwInfo.waste_code, // 危废代码
'daima': hwInfo.waste_category_code, // 危废代码
'xingtai': hwInfo.waste_form || '-', // 废物形态
'zhuyaochengfen': hwInfo.main_component || '-', // 主要成分
'zhuyaochengfen': hwInfo.main_components || '-', // 主要成分
'youhaichengfen': hwInfo.hazardous_components || '-', // 有害成分
'zhuyi': '-', // 注意事项
'zhuyi': hwInfo.main_properties || '-', // 注意事项
'shibiema': result.digital_id, // 数字识别码
'danwei': hwInfo.unit, // 单位
'lianxi': '', // 联系人
'riqi': getDate(submitData.time), // 日期
'riqi': getDate(), // 日期
'zhongliang': submitData.weight, // 重量
'beizhu': '-', // 备注 台账编号,包装类型
'beizhu': result.produce_ledger_no || '-', // 备注 台账编号,包装类型
'biaoshi': '', // 标识
'qrcode': result.label_qrcode // 二维码
}
@@ -388,7 +388,8 @@ export default {
try {
await printerService.init();
await printerService.autoConnect();
await printerService.printWasteLabel(currentPrintData);
this.printerConnected = true
await printerService.printWasteLabel(this.currentPrintData);
await this.handleDisconnect()
uni.showToast({ title: '打印成功', icon: 'success' })
this.currentPrintData = null
@@ -412,16 +413,16 @@ export default {
}
}
// 断开蓝牙打印机
if (this.connected) {
try {
await printerService.disconnect()
this.connected = false
} catch (err) {
console.warn('【蓝牙打印】断开连接失败:', err)
this.connected = false
}
}
// 断开蓝牙打印机
if (this.printerConnected) { // ✅ 修改:this.connected → this.printerConnected
try {
await printerService.disconnect()
this.printerConnected = false // ✅ 修改:this.connected → this.printerConnected
} catch (err) {
console.warn('【蓝牙打印】断开连接失败:', err)
this.printerConnected = false // ✅ 修改:this.connected → this.printerConnected
}
}
}
}
}