|
@@ -733,42 +733,162 @@ export default {
|
|
|
// });
|
|
|
// });
|
|
|
// }
|
|
|
+ // 确保已经连接好再订阅
|
|
|
|
|
|
- let userId = uni.getStorageSync("userId");
|
|
|
+ const userId = uni.getStorageSync("userId");
|
|
|
const topic = `/mps/wx_${userId}/notice`;
|
|
|
- this.unsubscribeFn = MqttService.subscribe(
|
|
|
- "CMD",
|
|
|
- topic,
|
|
|
- (topic, message) => {
|
|
|
- const data = JSON.parse(message);
|
|
|
- let userId = uni.getStorageSync("userId");
|
|
|
- const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
|
|
|
- const match = topic.match(noticeMatch);
|
|
|
- if (!match) return;
|
|
|
- // 更新页面数据
|
|
|
- this.alarmModel = true;
|
|
|
- this.clientId = data.clientId;
|
|
|
- this.devName = data.devName;
|
|
|
- this.sn = data.clientId;
|
|
|
- this.eventListId = data.eventListId;
|
|
|
- // VOIP 相关逻辑
|
|
|
- this.getVoipAuthor(this.clientId, userId);
|
|
|
- // 格式化时间
|
|
|
- const now = new Date();
|
|
|
- this.nowTime = `${now.getFullYear()}-${String(
|
|
|
- now.getMonth() + 1
|
|
|
- ).padStart(2, "0")}-${String(now.getDate()).padStart(
|
|
|
- 2,
|
|
|
- "0"
|
|
|
- )} ${String(now.getHours()).padStart(2, "0")}:${String(
|
|
|
- now.getMinutes()
|
|
|
- ).padStart(2, "0")}`;
|
|
|
- if (!isWmpf) {
|
|
|
- this.getVoipDevices();
|
|
|
+
|
|
|
+ if (MqttService.cmdConnected) {
|
|
|
+ // 已连接 → 直接订阅
|
|
|
+ this.unsubscribeOther = MqttService.subscribe(
|
|
|
+ "CMD",
|
|
|
+ topic,
|
|
|
+ (message) => {
|
|
|
+ const data = JSON.parse(message);
|
|
|
+ let userId = uni.getStorageSync("userId");
|
|
|
+ const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
|
|
|
+ const match = topic.match(noticeMatch);
|
|
|
+ if (!match) return;
|
|
|
+ // 更新页面数据
|
|
|
+ this.alarmModel = true;
|
|
|
+ this.clientId = data.clientId;
|
|
|
+ this.devName = data.devName;
|
|
|
+ this.sn = data.clientId;
|
|
|
+ this.eventListId = data.eventListId;
|
|
|
+ // VOIP 相关逻辑
|
|
|
+ this.getVoipAuthor(this.clientId, userId);
|
|
|
+ // 格式化时间
|
|
|
+ const now = new Date();
|
|
|
+ this.nowTime = `${now.getFullYear()}-${String(
|
|
|
+ now.getMonth() + 1
|
|
|
+ ).padStart(2, "0")}-${String(now.getDate()).padStart(
|
|
|
+ 2,
|
|
|
+ "0"
|
|
|
+ )} ${String(now.getHours()).padStart(2, "0")}:${String(
|
|
|
+ now.getMinutes()
|
|
|
+ ).padStart(2, "0")}`;
|
|
|
+ if (!isWmpf) {
|
|
|
+ this.getVoipDevices();
|
|
|
+ }
|
|
|
+ console.log("处理后的 CMD 数据:", data);
|
|
|
+ },
|
|
|
+ (err) => {
|
|
|
+ if (!err) console.log(`✅ 页面B 已订阅 ${topic}`);
|
|
|
}
|
|
|
- console.log("处理后的 CMD 数据:", data);
|
|
|
- }
|
|
|
- );
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ // 还没连上 → 等待 mqtt-ready
|
|
|
+ uni.$once("mqtt-ready", () => {
|
|
|
+ this.unsubscribeOther = MqttService.subscribe(
|
|
|
+ "CMD",
|
|
|
+ topic,
|
|
|
+ (message) => {
|
|
|
+ const data = JSON.parse(message);
|
|
|
+ let userId = uni.getStorageSync("userId");
|
|
|
+ const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
|
|
|
+ const match = topic.match(noticeMatch);
|
|
|
+ if (!match) return;
|
|
|
+ // 更新页面数据
|
|
|
+ this.alarmModel = true;
|
|
|
+ this.clientId = data.clientId;
|
|
|
+ this.devName = data.devName;
|
|
|
+ this.sn = data.clientId;
|
|
|
+ this.eventListId = data.eventListId;
|
|
|
+ // VOIP 相关逻辑
|
|
|
+ this.getVoipAuthor(this.clientId, userId);
|
|
|
+ // 格式化时间
|
|
|
+ const now = new Date();
|
|
|
+ this.nowTime = `${now.getFullYear()}-${String(
|
|
|
+ now.getMonth() + 1
|
|
|
+ ).padStart(2, "0")}-${String(now.getDate()).padStart(
|
|
|
+ 2,
|
|
|
+ "0"
|
|
|
+ )} ${String(now.getHours()).padStart(2, "0")}:${String(
|
|
|
+ now.getMinutes()
|
|
|
+ ).padStart(2, "0")}`;
|
|
|
+ if (!isWmpf) {
|
|
|
+ this.getVoipDevices();
|
|
|
+ }
|
|
|
+ console.log("处理后的 CMD 数据:", data);
|
|
|
+ },
|
|
|
+ (err) => {
|
|
|
+ if (!err) console.log(`✅ 页面B 已订阅 ${topic}`);
|
|
|
+ }
|
|
|
+ );
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // if (MqttService.cmdConnected) {
|
|
|
+ // this.unsubscribeFn = MqttService.subscribe(
|
|
|
+ // "CMD",
|
|
|
+ // topic,
|
|
|
+ // (topic, message) => {
|
|
|
+ // const data = JSON.parse(message);
|
|
|
+ // let userId = uni.getStorageSync("userId");
|
|
|
+ // const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
|
|
|
+ // const match = topic.match(noticeMatch);
|
|
|
+ // if (!match) return;
|
|
|
+ // // 更新页面数据
|
|
|
+ // this.alarmModel = true;
|
|
|
+ // this.clientId = data.clientId;
|
|
|
+ // this.devName = data.devName;
|
|
|
+ // this.sn = data.clientId;
|
|
|
+ // this.eventListId = data.eventListId;
|
|
|
+ // // VOIP 相关逻辑
|
|
|
+ // this.getVoipAuthor(this.clientId, userId);
|
|
|
+ // // 格式化时间
|
|
|
+ // const now = new Date();
|
|
|
+ // this.nowTime = `${now.getFullYear()}-${String(
|
|
|
+ // now.getMonth() + 1
|
|
|
+ // ).padStart(2, "0")}-${String(now.getDate()).padStart(
|
|
|
+ // 2,
|
|
|
+ // "0"
|
|
|
+ // )} ${String(now.getHours()).padStart(2, "0")}:${String(
|
|
|
+ // now.getMinutes()
|
|
|
+ // ).padStart(2, "0")}`;
|
|
|
+ // if (!isWmpf) {
|
|
|
+ // this.getVoipDevices();
|
|
|
+ // }
|
|
|
+ // console.log("处理后的 CMD 数据:", data);
|
|
|
+ // }
|
|
|
+ // );
|
|
|
+ // } else {
|
|
|
+ // uni.$once("mqtt-ready", () => {
|
|
|
+ // this.unsubscribeFn = MqttService.subscribe(
|
|
|
+ // "CMD",
|
|
|
+ // topic,
|
|
|
+ // (topic, message) => {
|
|
|
+ // const data = JSON.parse(message);
|
|
|
+ // let userId = uni.getStorageSync("userId");
|
|
|
+ // const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
|
|
|
+ // const match = topic.match(noticeMatch);
|
|
|
+ // if (!match) return;
|
|
|
+ // // 更新页面数据
|
|
|
+ // this.alarmModel = true;
|
|
|
+ // this.clientId = data.clientId;
|
|
|
+ // this.devName = data.devName;
|
|
|
+ // this.sn = data.clientId;
|
|
|
+ // this.eventListId = data.eventListId;
|
|
|
+ // // VOIP 相关逻辑
|
|
|
+ // this.getVoipAuthor(this.clientId, userId);
|
|
|
+ // // 格式化时间
|
|
|
+ // const now = new Date();
|
|
|
+ // this.nowTime = `${now.getFullYear()}-${String(
|
|
|
+ // now.getMonth() + 1
|
|
|
+ // ).padStart(2, "0")}-${String(now.getDate()).padStart(
|
|
|
+ // 2,
|
|
|
+ // "0"
|
|
|
+ // )} ${String(now.getHours()).padStart(2, "0")}:${String(
|
|
|
+ // now.getMinutes()
|
|
|
+ // ).padStart(2, "0")}`;
|
|
|
+ // if (!isWmpf) {
|
|
|
+ // this.getVoipDevices();
|
|
|
+ // }
|
|
|
+ // console.log("处理后的 CMD 数据:", data);
|
|
|
+ // }
|
|
|
+ // );
|
|
|
+ // });
|
|
|
+ // }
|
|
|
},
|
|
|
|
|
|
beforeDestroy() {
|