From 1884eb0f9dd74e72757493ca32b4514da7afd8d2 Mon Sep 17 00:00:00 2001
From: lld <15027638633@163.com>
Date: Thu, 25 Dec 2025 19:00:45 +0800
Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E6=8E=A7=E5=88=B6=E4=BF=AE?=
=?UTF-8?q?=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pages/control/index.vue | 74 +++++++++++++++++++++++------------------
1 file changed, 41 insertions(+), 33 deletions(-)
diff --git a/pages/control/index.vue b/pages/control/index.vue
index 3bb5cb4..3140acd 100644
--- a/pages/control/index.vue
+++ b/pages/control/index.vue
@@ -20,8 +20,8 @@
卷被开
{{ show.jbk }}
-
-
+
+
@@ -284,8 +284,8 @@ $uni-success: #18bc37 !default;
卷被开
{{ show.jbk }}
-
-
+
+
@@ -295,8 +295,8 @@ $uni-success: #18bc37 !default;
卷被关
{{ show.jbg }}
-
-
+
+
@@ -306,8 +306,8 @@ $uni-success: #18bc37 !default;
卷膜1开
{{ show.jm1k }}
-
-
+
+
@@ -317,8 +317,8 @@ $uni-success: #18bc37 !default;
卷膜1关
{{ show.jm1g }}
-
-
+
+
@@ -328,8 +328,8 @@ $uni-success: #18bc37 !default;
卷膜2开
{{ show.jm2k }}
-
-
+
+
@@ -339,8 +339,8 @@ $uni-success: #18bc37 !default;
卷膜2关
{{ show.jm2g }}
-
-
+
+
@@ -349,8 +349,8 @@ $uni-success: #18bc37 !default;
卷膜3开
{{ show.jm3k }}
-
-
+
+
@@ -360,8 +360,8 @@ $uni-success: #18bc37 !default;
卷膜3关
{{ show.jm3g }}
-
-
+
+
@@ -387,6 +387,7 @@ export default {
publishTopic: '/down',
title:'',
message: {},
+ connected:false,
// 卡片状态(模拟后端返回数据)
show: {
jbk: "暂停",
@@ -398,6 +399,7 @@ export default {
jm3k: "暂停",
jm3g: "暂停"
},
+ deviceType: '',
status: {
jbk: 0,
jbg: 0,
@@ -412,7 +414,7 @@ export default {
},
onLoad() {
this.publishTopic = "dtu/"+this.imei+this.publishTopic;
- this.subscribeTopic = "dtu/"+this.imei+this.subscribeTopic;
+ this.mqttConfig.subscribeTopic = "dtu/"+this.imei+this.mqttConfig.subscribeTopic;
this.title="设备【"+this.imei+"】";
if (this.status.jbk===1) {
this.status.jbg = 0;
@@ -449,6 +451,10 @@ export default {
// 卡片点击事件(实际项目中调用接口修改状态) 功能标识
handleCardClick(status, type) {
+ if (!this.connected) {
+ // 链接mqtt
+ this.connectMqtt()
+ }
uni.showModal({
title: '操作提示:',
content: '确定'+(status===1?"运行":"暂停")+'设备?',
@@ -457,17 +463,14 @@ export default {
success: (res) =>{
if (res.confirm) {
console.info("操作功能:【"+type+"】,变更状态为:"+ status)
- if (!this.connected) {
- // 链接mqtt
- this.connectMqtt()
- }
+
// 组装消息
this.message = JSON.stringify({[type]: status})
console.info("指令:"+this.message)
// 控制设备
this.publishMessage();
// 设备回执
- this.ackMessage(type);
+ this.deviceType=type;
}
}
})
@@ -489,9 +492,11 @@ export default {
this.client.on('connect', () => {
this.connected = true
+ this.client.subscribe(this.mqttConfig.subscribeTopic, { qos: 0 })
this.addMessage('已连接到MQTT服务器')
})
+ this.client.on("message",this.ackMessage);
this.client.on('error', (err) => {
this.addMessage(`控制失败: ${err.message}`)
@@ -517,7 +522,7 @@ export default {
},
publishMessage() {
- console.info("开始发布消息")
+ console.info("开始发布消息",this.connected,this.publishTopic,this.message)
if (!this.connected || !this.publishTopic || !this.message) {
uni.showToast({
title: '控制设备失败',
@@ -538,27 +543,28 @@ export default {
this.message = {};
},
- ackMessage(type) {
+ ackMessage(topic, payload) {
console.info("处理回执消息")
- this.client.on("message", (topic, payload) => {
+
+ // console.info("消息解析:"+topic,payload)
// 1. 先判断是否是目标订阅主题(如dtu/xxx/up)
- if (topic !== this.status.subscribeTopic) return;
+ if (topic !== this.mqttConfig.subscribeTopic) return;
// 2. 解析消息体(注意异常捕获)
let msgData = {};
try {
- msgData = JSON.parse(payload.toString());
+ msgData = JSON.parse(payload);
} catch (e) {
console.error("消息解析失败:", e);
return;
}
-
+ // console.info("回执消息:"+msgData)
// 3. 区分“回执”和“其他内容”
if (msgData.prop && "suc" in msgData) {
// 👉 这是“指令回执”
- this.handleCommandAck(msgData,type);
+ this.handleCommandAck(msgData,this.deviceType);
}
- });
+
},
addMessage(content) {
@@ -569,6 +575,7 @@ export default {
},
// 处理指令回执的函数
handleCommandAck(ackData,type) {
+ console.info("回执消息11111:"+ackData)
// 拿到指令字段(如jm2k)和执行状态(suc)
const commandField = Object.keys(ackData.prop)[0]; // 这里是"jm2k"
const commandValue = ackData.prop[commandField]; // 这里是0
@@ -578,6 +585,7 @@ export default {
this.status[type] = this.status[type] === 0 ? 1 : 0;
this.show[type] = this.status[type] === 0 ? "运行" : "暂停";
}
+ this.deviceType='';
// 业务逻辑:提示“指令执行成功/失败”
console.log(`指令[${commandField}=${commandValue}]执行${isSuccess ? "成功" : "失败"}`);
// (可匹配之前发布的指令msgId,更新UI状态)
@@ -668,4 +676,4 @@ export default {
.card-icon.active {
background-color: #007aff;
}
-
\ No newline at end of file
+