zhuguifei
2025-07-04 186d172fc06dfe44dc2a61d238356e6a7db652d5
mqtt/MqttView.vue
@@ -4,7 +4,6 @@
</template>
<script>
   export default {
      name: 'MqttView',
      data() {
@@ -13,92 +12,105 @@
         }
      },
      onReady() {
      },
      methods: {
         initMqtt(){
         initMqtt() {
            uni.getSystemInfo({
               success: (res) => {
                  let deviceId = res.deviceId
                  if (!deviceId) {
                     deviceId = 'mobile-' + this.tenantId + '-' + Date.parse(new Date())
                  }
                  uni.setStorageSync(this.$constant.DEVICE_ID, 'mobile-' + this.tenantId + '-' + res.deviceId);
                  uni.setStorageSync(this.$constant.DEVICE_ID, 'mobile-' + this.tenantId + '-' + res
                     .deviceId);
               }
            })
               this.startConnect();
            this.startConnect();
         },
          /* 连接MQTT */
          async startConnect() {
             var _this = this
             const account = uni.getStorageSync('account');
             const deviceid = uni.getStorageSync(this.$constant.DEVICE_ID);
             if (!account) {
                return false
             }
             let opts = {
                // #ifdef H5
                url: 'ws://' + this.$api.mqttBaseUrl + ':8083/mqtt',
                // #endif
                // #ifdef APP-PLUS
                url: 'wx://' + this.$api.mqttBaseUrl + ':8083/mqtt',
                // #endif
                clientId: deviceid,
                username: account.username,
                password: account.password
             }
             if (!this.$mqttTool.client) {
                var client = await this.$mqttTool.connect(opts);
             }
             //订阅查询设备状态返回数据
             this.$mqttTool.subscribe({
                topic: this.$constant.SERVICE_DOWN + '/' + deviceid + '/#',
                qos: 0
             }).then(res => {
                console.error(res)
             })
             //订阅设备故障广播(广播不在乎客户端id,发送给租户下所有在线的设备)
             this.$mqttTool.subscribe({
                topic: this.$constant.SERVICE_BROADCAST_TENANT_REAL_FAULT.replace('%s', this.tenantId),
                qos: 0
             }).then(res => {
                console.error(res)
             })
             //订阅发送指令返回结果
             // this.$mqttTool.subscribe({
             //    topic: this.$constant.SERVICE_RES_EQU_CMD,
             //    qos: 0
             // }).then(res => {
             //    console.error(res)
             // })
             // if (!client) {
             //    return false
             // }
            let that = this
             client.on('connect', function(res) {
                console.error('连接成功')
             })
             client.on('reconnect', function(res) {
                console.error('重新连接')
             })
             client.on('error', function(res) {
                console.info('连接错误')
             })
             client.on('close', function(res) {
                console.error('关闭成功')
             })
             client.on('message', function(topic, message, buffer) {
               that.$eventBus.$emit(that.$constant.MQTT_TOPIC_MESSAGE, message);
                console.info(message)
             })
          },
         /* 连接MQTT */
         async startConnect() {
            var _this = this
            const account = uni.getStorageSync('account');
            const deviceid = uni.getStorageSync(this.$constant.DEVICE_ID);
            if (!account) {
               return false
            }
            let opts = {
               // #ifdef H5
               url: 'ws://' + this.$api.mqttBaseUrl + ':8083/mqtt',
               // #endif
               // #ifdef APP-PLUS
               url: 'wx://' + this.$api.mqttBaseUrl + ':8083/mqtt',
               // #endif
               clientId: deviceid,
               username: account.username,
               password: account.password
            }
            if (!this.$mqttTool.client) {
               this.$mqttTool.client = await this.$mqttTool.connect(opts);
            }
            //订阅查询设备状态返回数据
            this.$mqttTool.subscribe({
               topic: this.$constant.SERVICE_DOWN + '/' + deviceid + '/#',
               qos: 0
            }).then(res => {
               console.error(res)
            })
            //订阅设备故障广播(广播不在乎客户端id,发送给租户下所有在线的设备)
            this.$mqttTool.subscribe({
               topic: this.$constant.SERVICE_BROADCAST_TENANT_REAL_FAULT.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_RES_EQU_CMD,
            //    qos: 0
            // }).then(res => {
            //    console.error(res)
            // })
            // if (!client) {
            //    return false
            // }
            let that = this
            let client = this.$mqttTool.client
            client.on('connect', function(res) {
               console.error('连接成功')
            })
            client.on('reconnect', function(res) {
               console.error('重新连接')
            })
            client.on('error', function(res) {
               console.info('连接错误')
            })
            client.on('close', function(res) {
               console.error('关闭成功')
            })
            client.on('message', function(topic, message, buffer) {
               uni.$emit(that.$constant.MQTT_TOPIC_MESSAGE, message);
               console.error("收到message(总):"+topic)
            })
         },
      },
      computed: {
         tenantId() {