From c1f79e2391ab5eb5b16d95a36027d8a26c565b21 Mon Sep 17 00:00:00 2001 From: kun <1422840143@qq.com> Date: Tue, 28 May 2024 15:55:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20BUG=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../personModule/administration.vue | 49 ++++++++++++++++++ .../personModule/laborComponent.vue | 50 ++++++++++++++++++- 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/views/projectFront/laborManage/personModule/administration.vue b/src/views/projectFront/laborManage/personModule/administration.vue index 3eddce30..6e6a0d5e 100644 --- a/src/views/projectFront/laborManage/personModule/administration.vue +++ b/src/views/projectFront/laborManage/personModule/administration.vue @@ -2062,6 +2062,15 @@ import { VueCropper } from "vue-cropper"; import { apiUploadImage } from "@/assets/js/api/common"; import lrz from "lrz"; +import mqtt from "mqtt"; +var client = null; +const options = { + connectTimeout: 40000, + clientId: "mqttjs_" + Math.random().toString(16).substr(2, 8), + username: "root", + password: "123456", + clean: true, +}; export default { components: { VueCropper, @@ -2217,6 +2226,8 @@ export default { radio: "", showImgList: false, imgFileList: [], // 照片文件列表 + topicName: "topic", + userId: "", }; }, created() { @@ -2250,9 +2261,20 @@ export default { this.getUGroupDevList(); this.getProjectConfig(); }, + beforeDestroy() { + if (client) { + client.unsubscribe(this.topicName + this.userId, options); + client.end(); + } + client = null; + }, mounted() { + this.userId = this.$store.state.userInfo.userId; + this.topicName = this.$store.state.userInfo.scope; this.selectJobList(); this.getEnterpriseType(); + client = mqtt.connect(mqttUrl, options); + this.mqttMSG(); }, computed: { headers() { @@ -2262,6 +2284,33 @@ export default { }, }, methods: { + // 监听mqtt下发信息然后刷新列表 + mqttMSG() { + // mqtt连接 +"/#" +workerId + client.on("connect", (e) => { + // console.log('连接成功:', this.topicName + this.userId) + client.subscribe(this.topicName + this.userId, { qos: 0 }, (error) => { + if (!error) { + // console.log('订阅成功123') + } else { + console.log("订阅失败"); + } + }); + }); + // 接收消息处理 + client.on("message", (topic, message) => { + // console.log(message); + this.getTableData(); + }); + // 断开发起重连 + // client.on("reconnect", (error) => { + // console.log("正在重连:", error); + // }); + // 链接异常处理 + client.on("error", (error) => { + console.log("连接失败:", error); + }); + }, // 导入照片数据 saveImportFn() { if (this.imgFileList.length == 0) { diff --git a/src/views/projectFront/laborManage/personModule/laborComponent.vue b/src/views/projectFront/laborManage/personModule/laborComponent.vue index 77afb9d4..c78a60bb 100644 --- a/src/views/projectFront/laborManage/personModule/laborComponent.vue +++ b/src/views/projectFront/laborManage/personModule/laborComponent.vue @@ -2386,6 +2386,15 @@ import { VueCropper } from "vue-cropper"; import { apiUploadImage } from "@/assets/js/api/common"; import lrz from "lrz"; +import mqtt from "mqtt"; +var client = null; +const options = { + connectTimeout: 40000, + clientId: "mqttjs_" + Math.random().toString(16).substr(2, 8), + username: "root", + password: "123456", + clean: true, +}; export default { components: { VueCropper, @@ -2582,6 +2591,8 @@ export default { pageSize: 10, }, whoId: "", + topicName: "topic", + userId: "", }; }, created() { @@ -2615,10 +2626,20 @@ export default { this.getUGroupDevList(); this.getProjectConfig(); }, - + beforeDestroy() { + if (client) { + client.unsubscribe(this.topicName + this.userId, options); + client.end(); + } + client = null; + }, mounted() { // this.webSocket = new WebSocket("ws://127.0.0.1:1818"); + this.userId = this.$store.state.userInfo.userId; + this.topicName = this.$store.state.userInfo.scope; this.getEnterpriseType(); + client = mqtt.connect(mqttUrl, options); + this.mqttMSG(); }, computed: { headers() { @@ -2628,6 +2649,33 @@ export default { }, }, methods: { + // 监听mqtt下发信息然后刷新列表 + mqttMSG() { + // mqtt连接 +"/#" +workerId + client.on("connect", (e) => { + // console.log('连接成功:', this.topicName + this.userId) + client.subscribe(this.topicName + this.userId, { qos: 0 }, (error) => { + if (!error) { + // console.log('订阅成功123') + } else { + console.log("订阅失败"); + } + }); + }); + // 接收消息处理 + client.on("message", (topic, message) => { + // console.log(message); + this.getTableData(); + }); + // 断开发起重连 + // client.on("reconnect", (error) => { + // console.log("正在重连:", error); + // }); + // 链接异常处理 + client.on("error", (error) => { + console.log("连接失败:", error); + }); + }, handleImportSuccess(response, file, fileList) { console.log(response, file, fileList); this.imgFileList = fileList;