zhuguifei
2025-09-02 9dfbc038667839631578c12ff748534e5939bc82
mqtt/MqttView.vue
@@ -15,6 +15,12 @@
      },
      destroyed() {
         console.error("MQTT组件销毁")
         this.$mqttTool.end().then(res => {
            console.error(res)
         })
      },
      methods: {
         initMqtt() {
            uni.getSystemInfo({
@@ -43,11 +49,11 @@
               // #ifdef H5
               url: 'wss://' + this.$api.mqttBaseUrl + ':8084/mqtt',
               // #endif
               // #ifdef MP-WEIXIN
               url: 'wxs://' + this.$api.mqttBaseUrl + ':8084/mqtt',// 微信小程序强制 WSS
               // #endif
               // #ifdef APP-PLUS
               url: 'wxs://' + this.$api.mqttBaseUrl + ':8084/mqtt',  // Android/iOS 用普通 WebSocket
               // #ifdef MP-WEIXIN
               url: 'wxs://' + this.$api.mqttBaseUrl + ':8084/mqtt', // 微信小程序强制 WSS
               // #endif
               // #ifdef APP-PLUS
               url: 'wxs://' + this.$api.mqttBaseUrl + ':8084/mqtt', // Android/iOS 用普通 WebSocket
               // #endif
               clientId: deviceid,
               username: account.username,
@@ -70,39 +76,40 @@
               qos: 0
            }).then(res => {
               console.error(res)
            })
            //订阅设备实时故障(主动请求只发给请求设备)
            this.$mqttTool.subscribe({
               topic: this.$constant.SERVICE_ONECE_TENANT_REAL_FAULT.replace('%s', deviceid),
               qos: 0
            }).then(res => {
               console.error(res)
            })
            //订阅干燥设备连接断开状态更新-租户内所有设备
            this.$mqttTool.subscribe({
               topic: this.$constant.SERVICE_BROADCAST_TENANT_UPDATE_EQU_STATU.replace('%s', this.tenantId),
               qos: 0
            }).then(res => {
               console.error(res)
            })
            //订阅查询干燥设备连接状态-单个设备
            this.$mqttTool.subscribe({
               topic: this.$constant.SERVICE_RES_EQU_STATU.replace('%s', deviceid),
               qos: 0
            }).then(res => {
               console.error(res)
            })
            //订阅干燥设备实时数据
            this.$mqttTool.subscribe({
               topic: this.$constant.SERVICE_BROADCAST_TENANT_REAL_DATA.replace('%s', this.tenantId),
               qos: 0
            }).then(res => {
               console.error(res)
            })
            })
            //订阅设备实时故障(主动请求只发给请求设备)
            this.$mqttTool.subscribe({
               topic: this.$constant.SERVICE_ONECE_TENANT_REAL_FAULT.replace('%s', deviceid),
               qos: 0
            }).then(res => {
               console.error(res)
            })
            //订阅干燥设备连接断开状态更新-租户内所有设备
            this.$mqttTool.subscribe({
               topic: this.$constant.SERVICE_BROADCAST_TENANT_UPDATE_EQU_STATU.replace('%s', this
                  .tenantId),
               qos: 0
            }).then(res => {
               console.error(res)
            })
            //订阅查询干燥设备连接状态-单个设备
            this.$mqttTool.subscribe({
               topic: this.$constant.SERVICE_RES_EQU_STATU.replace('%s', deviceid),
               qos: 0
            }).then(res => {
               console.error(res)
            })
            //订阅干燥设备实时数据
            this.$mqttTool.subscribe({
               topic: this.$constant.SERVICE_BROADCAST_TENANT_REAL_DATA.replace('%s', this.tenantId),
               qos: 0
            }).then(res => {
               console.error(res)
            })
            //订阅发送指令返回结果
            // this.$mqttTool.subscribe({
            //    topic: this.$constant.SERVICE_RES_EQU_CMD,
@@ -113,30 +120,30 @@
            // if (!client) {
            //    return false
            // }
            let that = this
            let that = this
            let client = this.$mqttTool.client
            client.on('connect', function(res) {
               console.error('连接成功')
               console.error('连接成功')
               console.error(res)
            })
            client.on('reconnect', function(res) {
               console.error('重新连接')
               console.error('重新连接')
               console.error(res)
            })
            client.on('error', function(res) {
               console.info('连接错误')
               console.info('连接错误')
               console.error(res)
            })
            client.on('close', function(res) {
               console.error('关闭成功')
               console.error('关闭成功')
               console.error(res)
            })
            client.on('message', function(topic, message, buffer) {
               uni.$emit(that.$constant.MQTT_TOPIC_MESSAGE, message);
               console.error("收到message(总):"+topic)
               //console.error("收到message(总):"+topic)
            })