功能修复
parent
1fef05d415
commit
0ea24e0329
102
App.vue
102
App.vue
|
|
@ -1,89 +1,33 @@
|
|||
<script>
|
||||
import config from './config'
|
||||
import { getToken } from '@/utils/auth'
|
||||
import {
|
||||
initMQTT,
|
||||
disconnectMQTT,
|
||||
getMQTTStatus,
|
||||
manualReconnect
|
||||
} from '@/utils/mqtt';
|
||||
export default {
|
||||
globalData: {
|
||||
mqttClient: null, // 全局MQTT客户端实例
|
||||
mqttConfig: { // MQTT全局配置(可根据环境切换)
|
||||
host: '1.94.254.176',
|
||||
port: 9001, // 小程序用wss端口(如EMQ X的8084)
|
||||
isSSL: false, // true=wss,false=ws
|
||||
username: 'admin',
|
||||
password: 'Admin#12345678',
|
||||
keepalive: 60,
|
||||
clean: true
|
||||
},
|
||||
mqttSubscribeList: [ // 全局基础订阅列表(所有页面都需要的主题)
|
||||
{topic: 'dtu/#', qos: 0},
|
||||
// {topic: 'system/status', qos: 1}
|
||||
],
|
||||
},
|
||||
onLaunch() {
|
||||
this.initApp()
|
||||
console.log('应用启动,初始化MQTT连接');
|
||||
this.initGlobalMQTT();
|
||||
},
|
||||
onShow() {
|
||||
console.log('应用切前台,检查MQTT连接');
|
||||
const { isConnected } = getMQTTStatus();
|
||||
if (!isConnected) {
|
||||
this.initGlobalMQTT();
|
||||
}
|
||||
console.info("mqtt:",isConnected,getMQTTStatus);
|
||||
},
|
||||
import config from './config'
|
||||
import { getToken } from '@/utils/auth'
|
||||
|
||||
onHide() {
|
||||
console.log('应用切后台,断开MQTT连接');
|
||||
// 切后台主动断开(避免小程序后台占用资源)
|
||||
disconnectMQTT();
|
||||
this.globalData.mqttClient = null;
|
||||
export default {
|
||||
onLaunch: function() {
|
||||
this.initApp()
|
||||
},
|
||||
methods: {
|
||||
// 初始化应用
|
||||
initApp() {
|
||||
// 初始化应用配置
|
||||
this.initConfig()
|
||||
// 检查用户登录状态
|
||||
//#ifdef H5
|
||||
this.checkLogin()
|
||||
//#endif
|
||||
},
|
||||
methods: {
|
||||
// 初始化应用
|
||||
initApp() {
|
||||
// 初始化应用配置
|
||||
this.initConfig()
|
||||
// 检查用户登录状态
|
||||
//#ifdef H5
|
||||
this.checkLogin()
|
||||
//#endif
|
||||
},
|
||||
initConfig() {
|
||||
this.globalData.config = config
|
||||
},
|
||||
checkLogin() {
|
||||
if (!getToken()) {
|
||||
this.$tab.reLaunch('/pages/login')
|
||||
}
|
||||
},
|
||||
// 初始化全局MQTT
|
||||
async initGlobalMQTT() {
|
||||
try {
|
||||
// 调用工具类初始化连接(传入全局配置+全局订阅列表)
|
||||
const client = await initMQTT(
|
||||
this.globalData.mqttConfig,
|
||||
this.globalData.mqttSubscribeList
|
||||
);
|
||||
// 挂载客户端到全局
|
||||
this.globalData.mqttClient = client;
|
||||
// 通知所有页面:客户端已就绪,可绑定自定义事件
|
||||
uni.$emit('mqtt_client_ready', client);
|
||||
console.log('全局MQTT初始化成功');
|
||||
} catch (err) {
|
||||
console.error('全局MQTT初始化失败:', err);
|
||||
uni.$emit('mqtt_init_fail', err);
|
||||
}
|
||||
initConfig() {
|
||||
this.globalData.config = config
|
||||
},
|
||||
checkLogin() {
|
||||
if (!getToken()) {
|
||||
this.$tab.reLaunch('/pages/login')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
@import '@/static/scss/index.scss';
|
||||
@import '@/static/scss/index.scss'
|
||||
</style>
|
||||
|
|
|
|||
|
|
@ -359,7 +359,7 @@ export default {
|
|||
//todo
|
||||
|
||||
// this.status[type] = this.status[type] === 0 ? 1 : 0;
|
||||
// this.show[type] = this.status[type] === 0 ? "运行" : "暂停";
|
||||
// this.show[type] = this.status[type] === 0 ? "暂停" : "运行";
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -475,7 +475,7 @@ export default {
|
|||
|
||||
if (isSuccess) {
|
||||
this.status[type] = this.status[type] === 0 ? 1 : 0;
|
||||
this.show[type] = this.status[type] === 0 ? "运行" : "暂停";
|
||||
this.show[type] = this.status[type] === 0 ? "暂停" : "运行";
|
||||
}
|
||||
this.deviceType = '';
|
||||
this.$modal.msgSuccess("设备操作成功!")
|
||||
|
|
|
|||
Loading…
Reference in New Issue