代码还原
parent
be0492f952
commit
f48815d556
109
App.vue
109
App.vue
|
|
@ -1,98 +1,33 @@
|
||||||
<script>
|
<script>
|
||||||
import config from './config'
|
import config from './config'
|
||||||
import { getToken } from '@/utils/auth'
|
import { getToken } from '@/utils/auth'
|
||||||
import { initMQTT, disconnectMQTT, getMQTTStatus, getMQTTClient } from '@/utils/mqtt'; // 替换为你的mqtt工具层路径
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
globalData: {
|
onLaunch: function() {
|
||||||
mqttClient: null, // 全局MQTT客户端实例
|
this.initApp()
|
||||||
mqttConfig: { // MQTT全局配置(可根据环境切换)
|
},
|
||||||
host: '1.94.254.176',
|
methods: {
|
||||||
port: 9001, // 小程序用wss端口(如EMQ X的8084)
|
// 初始化应用
|
||||||
isSSL: false, // true=wss,false=ws
|
initApp() {
|
||||||
username: 'admin',
|
// 初始化应用配置
|
||||||
password: 'Admin#12345678',
|
this.initConfig()
|
||||||
keepalive: 60,
|
// 检查用户登录状态
|
||||||
clean: true
|
//#ifdef H5
|
||||||
},
|
this.checkLogin()
|
||||||
mqttSubscribeList: [ // 全局基础订阅列表(所有页面都需要的主题)
|
//#endif
|
||||||
"dtu/864865085016294/up",
|
|
||||||
"dtu/864536071808560/up",
|
|
||||||
"dtu/864865085008135/up"
|
|
||||||
// {topic: 'system/status', qos: 1}
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
onLaunch() {
|
initConfig() {
|
||||||
this.initApp()
|
this.globalData.config = config
|
||||||
console.log('应用启动,初始化MQTT连接');
|
|
||||||
this.initGlobalMQTT();
|
|
||||||
},
|
},
|
||||||
onShow() {
|
checkLogin() {
|
||||||
console.log('应用切前台,检查MQTT连接');
|
if (!getToken()) {
|
||||||
const { isConnected, isManualDisconnect } = getMQTTStatus();
|
this.$tab.reLaunch('/pages/login')
|
||||||
// 未连接且非手动断开 → 重新初始化连接
|
|
||||||
if (!isConnected && !isManualDisconnect) {
|
|
||||||
this.initGlobalMQTT();
|
|
||||||
}
|
|
||||||
console.info("mqtt:",isConnected,getMQTTStatus);
|
|
||||||
},
|
|
||||||
// 应用切后台时触发(最小化/锁屏/切换应用)
|
|
||||||
onHide() {
|
|
||||||
console.log('应用切后台,断开MQTT连接');
|
|
||||||
// 手动断开连接(避免后台消耗资源)
|
|
||||||
disconnectMQTT(() => {
|
|
||||||
console.log('MQTT断开完成,应用进入后台');
|
|
||||||
});
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
// 初始化应用
|
|
||||||
initApp() {
|
|
||||||
// 初始化应用配置
|
|
||||||
this.initConfig()
|
|
||||||
// 检查用户登录状态
|
|
||||||
//#ifdef H5
|
|
||||||
this.checkLogin()
|
|
||||||
//#endif
|
|
||||||
},
|
|
||||||
initConfig() {
|
|
||||||
this.globalData.config = config
|
|
||||||
},
|
|
||||||
checkLogin() {
|
|
||||||
if (!getToken()) {
|
|
||||||
this.$tab.reLaunch('/pages/login')
|
|
||||||
}
|
|
||||||
},
|
|
||||||
/**
|
|
||||||
* 初始化全局MQTT连接(核心方法)
|
|
||||||
*/
|
|
||||||
initGlobalMQTT() {
|
|
||||||
const {mqttConfig, mqttSubscribeList} = this.globalData;
|
|
||||||
|
|
||||||
// 调用MQTT工具层的初始化方法
|
|
||||||
initMQTT(
|
|
||||||
mqttConfig,
|
|
||||||
mqttSubscribeList,
|
|
||||||
// 连接成功回调
|
|
||||||
(client) => {
|
|
||||||
console.log('MQTT全局连接成功');
|
|
||||||
this.globalData.mqttClient = getMQTTClient();
|
|
||||||
},
|
|
||||||
// 连接失败回调
|
|
||||||
(err) => {
|
|
||||||
console.error('MQTT全局连接失败:', err);
|
|
||||||
// 可添加重试逻辑/用户提示
|
|
||||||
uni.showToast({
|
|
||||||
title: '设备通信异常',
|
|
||||||
icon: 'none',
|
|
||||||
duration: 3000
|
|
||||||
});
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss">
|
<style lang="scss">
|
||||||
@import '@/static/scss/index.scss';
|
@import '@/static/scss/index.scss'
|
||||||
</style>
|
</style>
|
||||||
|
|
|
||||||
|
|
@ -284,6 +284,14 @@ export default {
|
||||||
onUnload() {
|
onUnload() {
|
||||||
// 页面卸载时断开连接
|
// 页面卸载时断开连接
|
||||||
if (this.client && this.isConnected) {
|
if (this.client && this.isConnected) {
|
||||||
|
console.info("mqtt链接已关闭")
|
||||||
|
this.client.end()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onHide() {
|
||||||
|
// 页面卸载时断开连接
|
||||||
|
if (this.client && this.isConnected) {
|
||||||
|
console.info("mqtt链接已关闭")
|
||||||
this.client.end()
|
this.client.end()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue