代码还原

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

77
App.vue
View File

@ -1,48 +1,10 @@
<script>
import config from './config'
import { getToken } from '@/utils/auth'
import { initMQTT, disconnectMQTT, getMQTTStatus, getMQTTClient } from '@/utils/mqtt'; // mqtt
import config from './config'
import { getToken } from '@/utils/auth'
export default {
globalData: {
mqttClient: null, // MQTT
mqttConfig: { // MQTT
host: '1.94.254.176',
port: 9001, // wssEMQ X8084
isSSL: false, // true=wssfalse=ws
username: 'admin',
password: 'Admin#12345678',
keepalive: 60,
clean: true
},
mqttSubscribeList: [ //
"dtu/864865085016294/up",
"dtu/864536071808560/up",
"dtu/864865085008135/up"
// {topic: 'system/status', qos: 1}
],
},
onLaunch() {
export default {
onLaunch: function() {
this.initApp()
console.log('应用启动初始化MQTT连接');
this.initGlobalMQTT();
},
onShow() {
console.log('应用切前台检查MQTT连接');
const { isConnected, isManualDisconnect } = getMQTTStatus();
//
if (!isConnected && !isManualDisconnect) {
this.initGlobalMQTT();
}
console.info("mqtt",isConnected,getMQTTStatus);
},
// //
onHide() {
console.log('应用切后台断开MQTT连接');
//
disconnectMQTT(() => {
console.log('MQTT断开完成应用进入后台');
});
},
methods: {
//
@ -61,38 +23,11 @@
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>
<style lang="scss">
@import '@/static/scss/index.scss';
@import '@/static/scss/index.scss'
</style>

View File

@ -284,6 +284,14 @@ export default {
onUnload() {
//
if (this.client && this.isConnected) {
console.info("mqtt链接已关闭")
this.client.end()
}
},
onHide() {
//
if (this.client && this.isConnected) {
console.info("mqtt链接已关闭")
this.client.end()
}
},