添加大棚后更新订阅主题
parent
81d08dbd56
commit
ecad105f36
41
App.vue
41
App.vue
|
|
@ -3,10 +3,6 @@ import config from './config'
|
||||||
import { getToken } from '@/utils/auth'
|
import { getToken } from '@/utils/auth'
|
||||||
import mqttUtil from '@/utils/mqtt'
|
import mqttUtil from '@/utils/mqtt'
|
||||||
import {startMqttOnlinePing, stopMqttOnlinePing} from "./utils/mqtt";
|
import {startMqttOnlinePing, stopMqttOnlinePing} from "./utils/mqtt";
|
||||||
import {batchSubscribe} from "./api/system/mqtt";
|
|
||||||
import store from "store";
|
|
||||||
import {listAgri} from "./api/system/assets/agri";
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
subscribeList:[],
|
subscribeList:[],
|
||||||
globalData: {
|
globalData: {
|
||||||
|
|
@ -123,27 +119,7 @@ export default {
|
||||||
console.error('MQTT连接失败')
|
console.error('MQTT连接失败')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var clientId = mqttUtil.getMqttState().clientId;
|
mqttUtil.updateSubscribeTopic();
|
||||||
this.getSubscribeImei(clientId).then(res=>{
|
|
||||||
console.info("subscribeList",res)
|
|
||||||
const subscribeList = res;
|
|
||||||
this.globalData.mqtt.subscribeList = subscribeList || []
|
|
||||||
batchSubscribe({clientId: clientId}).then((result) => {
|
|
||||||
if (result.code === 200) {
|
|
||||||
console.info(`设备列表订阅成功:${subscribeList}`)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// ========== 新增:登录成功时同步到localStorage ==========
|
|
||||||
uni.setStorageSync('mqtt_subscribe_list', subscribeList || [])
|
|
||||||
|
|
||||||
if (subscribeList.length > 0) {
|
|
||||||
mqttUtil.updateSubscribeList(subscribeList)
|
|
||||||
console.log('恢复MQTT订阅列表:', subscribeList)
|
|
||||||
// ========== 新增:恢复订阅后同步到localStorage ==========
|
|
||||||
uni.setStorageSync('mqtt_subscribe_list', subscribeList)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
loginSuccess(token) {
|
loginSuccess(token) {
|
||||||
this.globalData.mqtt.hasLogin = true
|
this.globalData.mqtt.hasLogin = true
|
||||||
|
|
@ -163,21 +139,6 @@ export default {
|
||||||
uni.removeStorageSync('mqtt_subscribe_list')
|
uni.removeStorageSync('mqtt_subscribe_list')
|
||||||
console.log('登出成功,MQTT已断开')
|
console.log('登出成功,MQTT已断开')
|
||||||
},
|
},
|
||||||
|
|
||||||
getSubscribeImei(clientId) {
|
|
||||||
return listAgri().then(response => {
|
|
||||||
const subscribeList = [];
|
|
||||||
if (response.code === 200) {
|
|
||||||
response.rows.forEach(item =>
|
|
||||||
subscribeList.push(`frontend/${clientId}/dtu/${item.imei}/+`)
|
|
||||||
);
|
|
||||||
if (store.getters && store.getters.name === 'admin') {
|
|
||||||
subscribeList.push(`frontend/${clientId}/dtu/862538065276061/+`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return subscribeList;
|
|
||||||
})
|
|
||||||
},
|
|
||||||
// token过期处理逻辑(核心)
|
// token过期处理逻辑(核心)
|
||||||
handleTokenExpired() {
|
handleTokenExpired() {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,9 @@
|
||||||
<script>
|
<script>
|
||||||
import UniPopup from "../../uni_modules/uni-popup/components/uni-popup/uni-popup.vue";
|
import UniPopup from "../../uni_modules/uni-popup/components/uni-popup/uni-popup.vue";
|
||||||
import {addAgriMobile} from "../../api/system/assets/agri";
|
import {addAgriMobile} from "../../api/system/assets/agri";
|
||||||
|
import {updateSubscribeTopic} from "../../utils/mqtt";
|
||||||
|
import {batchUnsubscribe} from "../../api/system/mqtt";
|
||||||
|
import * as mqttUtil from "../../utils/mqtt";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "addAgri",
|
name: "addAgri",
|
||||||
|
|
@ -98,6 +101,11 @@ export default {
|
||||||
// 4. 只有用户点击弹窗的“确定”后,才执行后续操作
|
// 4. 只有用户点击弹窗的“确定”后,才执行后续操作
|
||||||
if (res.confirm) {
|
if (res.confirm) {
|
||||||
this.$emit("reload"); // 向父组件传值触发刷新
|
this.$emit("reload"); // 向父组件传值触发刷新
|
||||||
|
batchUnsubscribe({clientId: mqttUtil.getMqttState().clientId}).then(response => {
|
||||||
|
if (response.code === 200) {
|
||||||
|
updateSubscribeTopic();
|
||||||
|
}
|
||||||
|
})
|
||||||
this.close(); // 关闭添加大棚的弹窗(移到这里)
|
this.close(); // 关闭添加大棚的弹窗(移到这里)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,10 @@
|
||||||
|
|
||||||
// 微信小程序端:引入适配小程序的 mqtt 版本
|
// 微信小程序端:引入适配小程序的 mqtt 版本
|
||||||
import mqtt from 'mqtt/dist/mqtt'
|
import mqtt from 'mqtt/dist/mqtt'
|
||||||
import {batchUnsubscribe} from "../api/system/mqtt";
|
import {batchSubscribe, batchUnsubscribe} from "../api/system/mqtt";
|
||||||
import {getToken} from "./auth";
|
import {getToken} from "./auth";
|
||||||
|
import {listAgri} from "../api/system/assets/agri";
|
||||||
|
import store from "../store";
|
||||||
|
|
||||||
// ===================== MQTT配置(暂时写死,TODO:后续从数据字典获取)=====================
|
// ===================== MQTT配置(暂时写死,TODO:后续从数据字典获取)=====================
|
||||||
const MQTT_CONFIG = {
|
const MQTT_CONFIG = {
|
||||||
|
|
@ -292,7 +294,6 @@ export function getMqttState() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// utils/mqttOnline.js
|
// utils/mqttOnline.js
|
||||||
let timer = null;
|
let timer = null;
|
||||||
|
|
||||||
|
|
@ -309,7 +310,8 @@ export function startMqttOnlinePing(intervalMs = 20000) {
|
||||||
// qos=0 足够;retain 不要
|
// qos=0 足够;retain 不要
|
||||||
mqttState.client.publish(topic, payload, {qos: 0, retain: false});
|
mqttState.client.publish(topic, payload, {qos: 0, retain: false});
|
||||||
// console.info(`主题:${topic}:${payload}`)
|
// console.info(`主题:${topic}:${payload}`)
|
||||||
} catch (e) {}
|
} catch (e) {
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ping();
|
ping();
|
||||||
|
|
@ -325,6 +327,60 @@ export function stopMqttOnlinePing() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新MQTT订阅主题(无返回值、不用async、纯回调实现)
|
||||||
|
*/
|
||||||
|
export function updateSubscribeTopic() {
|
||||||
|
const subscribeList = [];
|
||||||
|
// 初始化clientId,空值保护
|
||||||
|
let clientId = mqttState.options?.clientId || '';
|
||||||
|
if (!clientId) {
|
||||||
|
throw new Error('MQTT clientId 为空,无法生成订阅主题');
|
||||||
|
}
|
||||||
|
// 1. 获取设备列表(纯.then/.catch回调)
|
||||||
|
listAgri().then(response => {
|
||||||
|
if (response.code === 200) {
|
||||||
|
|
||||||
|
// 生成设备订阅主题(校验imei非空)
|
||||||
|
response.rows.forEach(item => {
|
||||||
|
if (item?.imei) {
|
||||||
|
subscribeList.push(`frontend/${clientId}/dtu/${item.imei}/+`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// 管理员额外订阅指定设备
|
||||||
|
if (store.getters && store.getters.name === 'admin') {
|
||||||
|
subscribeList.push(`frontend/${clientId}/dtu/862538065276061/+`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 有订阅主题时执行批量订阅
|
||||||
|
if (subscribeList.length > 0) {
|
||||||
|
// 2. 批量订阅(链式.then保证执行顺序)
|
||||||
|
batchSubscribe({clientId})
|
||||||
|
.then(result => {
|
||||||
|
if (result.code === 200) {
|
||||||
|
console.info(`设备列表订阅成功:${subscribeList}`);
|
||||||
|
// 更新订阅列表+同步本地存储
|
||||||
|
updateSubscribeList(subscribeList);
|
||||||
|
uni.setStorageSync('mqtt_subscribe_list', subscribeList);
|
||||||
|
console.log('恢复MQTT订阅列表:', subscribeList);
|
||||||
|
} else {
|
||||||
|
console.error(`批量订阅失败,返回码:${result.code}`);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(batchErr => {
|
||||||
|
console.error('批量订阅失败:', batchErr.message);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
console.warn('无可用的订阅主题,跳过订阅操作');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.error(`获取设备列表失败,返回码:${response.code}`);
|
||||||
|
}
|
||||||
|
}).catch(error => {
|
||||||
|
console.error('更新MQTT订阅主题失败:', error.message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// 导出所有方法(全局调用)
|
// 导出所有方法(全局调用)
|
||||||
export default {
|
export default {
|
||||||
initMqttConfig,
|
initMqttConfig,
|
||||||
|
|
@ -332,6 +388,7 @@ export default {
|
||||||
setOnMessageCallback,
|
setOnMessageCallback,
|
||||||
removeOnMessageCallback,
|
removeOnMessageCallback,
|
||||||
updateSubscribeList,
|
updateSubscribeList,
|
||||||
|
updateSubscribeTopic,
|
||||||
publishMqtt,
|
publishMqtt,
|
||||||
disconnectMqtt,
|
disconnectMqtt,
|
||||||
getMqttState
|
getMqttState
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue