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
+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;
}
},
/**
* 显示校准弹窗
*/