代码还原

master
xce 2026-01-05 21:49:29 +08:00
parent be0492f952
commit f48815d556
2 changed files with 30 additions and 87 deletions

109
App.vue
View File

@ -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, // wssEMQ X8084 //
isSSL: false, // true=wssfalse=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>

View File

@ -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()
} }
}, },