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
+1
View File
@@ -139,6 +139,7 @@ export default {
},
onLoad() {
this.$store.dispatch('scan/initScanMode')
this.$store.dispatch('scale/initScaleConfig') // 初始化称重配置
},
methods: {
handlePrinter() {
+1
View File
@@ -423,6 +423,7 @@ export default {
uni.showToast({ title: '打印成功', icon: 'none' })
},
fail: (err) => {
console.log(err)
uni.showToast({ title: '发送失败', icon: 'error' })
}
})
+59 -13
View File
@@ -51,6 +51,9 @@
<!-- 断开连接 -->
<view class="section" v-if="connected">
<button class="btn btn-primary" :loading="isReading" @click="handleReadWeight">
{{ isReading ? '读取中...' : '读取重量(R指令)' }}
</button>
<button class="btn btn-outline" @click="handleDisconnect">断开连接</button>
</view>
@@ -79,7 +82,7 @@
</template>
<script>
import scaleService from '@/utils/BluetoothScaleService.js';
import scaleService from '@/utils/BluetoothScaleService.new.js';
import { mapGetters, mapActions } from 'vuex';
export default {
@@ -87,6 +90,7 @@ export default {
return {
isConnecting: false,
isTaring: false,
isReading: false,
isCalibrating: false,
connected: false,
connectingText: '正在检查配置...',
@@ -99,7 +103,7 @@ export default {
}
},
computed: {
...mapGetters('scale', ['formattedWeight']),
...mapGetters('scale', ['formattedWeight', 'scaleDeviceId', 'scaleServiceId', 'scaleCharacteristicId', 'scaleWriteCharId', 'scaleDeviceName']),
displayWeight() {
if (!this.currentWeight || this.currentWeight.value === null) {
return '0.00';
@@ -108,6 +112,7 @@ export default {
}
},
onLoad() {
this.$store.dispatch('scale/initScaleConfig'); // 先初始化,从 localStorage 恢复
this.loadScaleConfig();
this.checkAndConnect();
},
@@ -139,12 +144,11 @@ export default {
scaleService.onError(this.handleError);
},
methods: {
// ...mapActions('scale', ['setWeightValue']),
...mapActions('scale', ['setWeightValue']),
handleWeightChange(weight) {
this.currentWeight = weight;
// 同步到store
// this.setWeightValue(weight);
this.setWeightValue(weight);
},
handleConnectionChange(connected) {
@@ -173,15 +177,12 @@ export default {
},
loadScaleConfig() {
const storeState = uni.getStorageSync('scaleConfig') || {};
const serviceConfig = scaleService.getConfig();
this.scaleConfig = {
deviceId: serviceConfig.deviceId || storeState.deviceId || '',
serviceId: serviceConfig.serviceId || storeState.serviceId || '',
notifyCharId: serviceConfig.notifyCharId || storeState.characteristicId || '',
writeCharId: serviceConfig.writeCharId || storeState.writeCharacteristicId || '',
deviceName: serviceConfig.deviceName || storeState.deviceName || ''
deviceId: this.scaleDeviceId || '',
serviceId: this.scaleServiceId || '',
notifyCharId: this.scaleCharacteristicId || '',
writeCharId: this.scaleWriteCharId || '',
deviceName: this.scaleDeviceName || ''
};
},
@@ -191,9 +192,20 @@ export default {
async checkAndConnect() {
this.loadScaleConfig();
// console.log('【调试】Vuex getter 值:', {
// scaleDeviceId: this.scaleDeviceId,
// scaleServiceId: this.scaleServiceId,
// scaleCharacteristicId: this.scaleCharacteristicId,
// scaleWriteCharId: this.scaleWriteCharId,
// scaleDeviceName: this.scaleDeviceName
// });
// console.log('【调试】loadScaleConfig 后:', this.scaleConfig);
// 检查是否有保存的配置
if (!this.scaleConfig.deviceId) {
this.connectingText = '未配置称重设备,请先配置设备';
this.addLog('未配置称重设备,请先配置设备', 'warn');
return;
}
@@ -243,6 +255,8 @@ export default {
this.loadScaleConfig();
// console.log('【调试】handleConnect 配置:', this.scaleConfig);
if (!this.scaleConfig.deviceId) {
uni.showToast({ title: '请先配置称重设备', icon: 'none' });
this.connectingText = '未配置称重设备';
@@ -320,6 +334,38 @@ export default {
}
},
/**
* 读取重量(主动发送R指令)
*/
async handleReadWeight() {
if (this.isReading) return;
this.isReading = true;
try {
const result = await scaleService.readWeight();
// 控制台打印完整数据(调试用)
// console.log('========== 蓝牙秤原始数据 ==========');
// console.log('HEX原始数据:', result.raw);
// console.log('ASCII原始数据:', result.rawAscii);
// console.log('解析后重量值:', result.value);
// console.log('单位:', result.unit);
// console.log('是否稳定:', result.stable);
// console.log('=====================================');
// 显示在页面上
this.currentWeight = result;
this.addLog(`读取成功: ${result.value} ${result.unit}`);
uni.showToast({ title: `重量: ${result.value} ${result.unit}`, icon: 'none' });
} catch (err) {
console.error('【称重】读取失败:', err);
this.addLog(`读取失败: ${err.message}`, 'error');
uni.showToast({ title: '读取失败', icon: 'none' });
} finally {
this.isReading = false;
}
},
/**
* 显示校准弹窗
*/
+3 -2
View File
@@ -171,8 +171,9 @@ export default {
content: '确定要退出登录吗?',
success: (res) => {
if (res.confirm) {
uni.clearStorageSync()
uni.reLaunch({ url: '/pages/login/login' })
// uni.clearStorageSync()
// uni.reLaunch({ url: '/pages/login/login' })
this.$store.dispatch('user/logout')
}
}
})
+5 -4
View File
@@ -70,7 +70,7 @@
import { api } from '@/api/index'
import scanMixin from '@/utils/barcodeMixin.js'
// #ifdef APP-PLUS
import printerService from '@/utils/BluetoothPrinterService.js'
import printerService from '@/utils/BluetoothPrinterService.new.js'
// #endif
export default {
@@ -96,7 +96,7 @@ export default {
try {
const res = await api.labelPrint({ code: this.form.code })
console.log(res)
if (res.data) {
this.previewData = res.data
uni.showToast({ title: '查询成功', icon: 'none' })
@@ -115,6 +115,7 @@ export default {
}
// #ifdef APP-PLUS
const printData = this.printData()
console.log(printData)
uni.showToast({ title: '打印中...', icon: 'none' })
try {
await printerService.init()
@@ -152,10 +153,10 @@ export default {
'zhuyi': this.previewData.zysx || '-', // 注意事项
'shibiema': this.previewData.szsbm || '', // 数字识别码
'danwei': this.previewData.dwmc || '', // 单位名称
'lianxi': this.previewData.lxr || '', // 联系人
'lianxi': (this.previewData.lxr || '') + ' ' + (this.previewData.lxrsj || ''), // 联系人
'riqi': this.previewData.cssj || '', // 产生时间
'zhongliang': (this.previewData.csl || '') + ' ' + (this.previewData.jldw || ''), // 重量
'beizhu': '危险特性: ' + (this.previewData.wxtx || '-'), // 备注
'beizhu': this.previewData.wxtx || '-', // 备注 危险特征
'biaoshi': '', // 标识
'qrcode': this.previewData.wxfwbqewm || '', // 二维码
'lianxidianhua': this.previewData.lxrsj || '', // 联系电话
+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
}
}
}
}
}