修复mqtt客户端问题

This commit is contained in:
微微一笑
2025-08-09 10:39:05 +08:00
parent c6babaa262
commit 00e0a4bbd1
2 changed files with 152 additions and 105 deletions

View File

@ -918,10 +918,6 @@
...this.Progress,
show: false, // 隐藏进度条
};
uni.showToast({
title: '网络异常',
icon: 'none'
});
}
},
@ -945,44 +941,48 @@
// 订阅来自设备的状态更新
const statusTopic = `A/${this.itemInfo.deviceImei}`;
this.mqttClient.subscribe(statusTopic, (payload) => {
console.log(`收到来自 ${statusTopic} 的消息:`, payload);
//收到电量上报。延迟20s请求接口数据
const parsedMessage = typeof payload === 'string' ? JSON.parse(payload) :
payload;
const deviceState = parsedMessage.state; // 直接取 state 数组
// ✅ 发送全局事件通知主页面更新
uni.$emit('deviceStatusUpdate', {
message: parsedMessage, // 消息内容
timestamp: new Date().getTime() // 时间戳
});
if (deviceState[0] === 12) {
setTimeout(() => {
this.fetchDeviceDetail(data.data.id);
}, 20000);
// 这里判断电量低于20%,弹框提示
if (this.deviceInfo.batteryPercentage < 20) {
this.popupType = 'bettery'
this.popupMessage = '请及时充电';
this.showPopupFlag = true;
try {
console.log(`收到来自 ${statusTopic} 的消息:`, payload);
//收到电量上报。延迟20s请求接口数据
const parsedMessage = typeof payload === 'string' ? JSON.parse(payload) :
payload;
const deviceState = parsedMessage.state; // 直接取 state 数组
// ✅ 发送全局事件通知主页面更新
uni.$emit('deviceStatusUpdate', {
message: parsedMessage, // 消息内容
timestamp: new Date().getTime() // 时间戳
});
if (deviceState[0] === 12) {
setTimeout(() => {
this.fetchDeviceDetail(data.data.id);
}, 20000);
// 这里判断电量低于20%,弹框提示
if (this.deviceInfo.batteryPercentage < 20) {
this.popupType = 'bettery'
this.popupMessage = '请及时充电';
this.showPopupFlag = true;
}
}
}
// 处理上传照片进度消息
if (deviceState[0] === 3) {
const progress = deviceState[1];
// 更新进度条
this.Progress = {
...this.Progress,
curr: progress * 2,
show: progress < 50 // 进度达到100时自动隐藏
};
// 当进度为100时显示成功弹框
if (progress === 50) {
this.popupType = 'logo';
this.popupMessage = '上传成功';
this.showPopupFlag = true;
this.lightModeB = false; // 关闭上传弹窗
this.selectedImage = ''; // 清空已选图片
// 处理上传照片进度消息
if (deviceState[0] === 3) {
const progress = deviceState[1];
// 更新进度条
this.Progress = {
...this.Progress,
curr: progress * 2,
show: progress < 50 // 进度达到100时自动隐藏
};
// 当进度为100时显示成功弹框
if (progress === 50) {
this.popupType = 'logo';
this.popupMessage = '上传成功';
this.showPopupFlag = true;
this.lightModeB = false; // 关闭上传弹窗
this.selectedImage = ''; // 清空已选图片
}
}
} catch (error) {
console.error('解析MQTT消息失败:', error, '原始消息:', payload);
}
});
})