From f998cbdaa20e7043e43b85a1a510b2c75d043e27 Mon Sep 17 00:00:00 2001 From: "HUOJIN\\92525" <925258474@qq.com> Date: Sat, 14 Sep 2024 16:05:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A0=E6=84=9F=E5=88=B7=E6=96=B0=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E6=B6=88=E6=81=AF=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/home.vue | 56 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/src/views/home.vue b/src/views/home.vue index e99b3ea..43fb6e7 100644 --- a/src/views/home.vue +++ b/src/views/home.vue @@ -36,6 +36,8 @@ export default { }, data() { return { + notificationInstance: null, // 用于存储当前的通知实例 + showNotification: false, // 控制是否显示通知 lineChartData: lineChartData.newVisitis } }, @@ -52,6 +54,10 @@ export default { this.websock = new WebSocket(wsUri) this.websock.onerror = this.webSocketOnError this.websock.onmessage = this.webSocketOnMessage + //5秒轮询一次 + this.pollingTimer = setInterval(() => { + stockUrl.stockMsg() + }, 5000); }, webSocketOnError(e) { this.$notify({ @@ -61,15 +67,8 @@ export default { }) }, webSocketOnMessage(e) { const data = JSON.parse(e.data) - console.log(data) if (data.msgType === 'INFO') { - this.$notify({ - title: '点击关注公众号', - message: data.msg, - type: 'success', - offset: 100, - duration: 0 - }); + this.createOrUpdateNotification(data); } else if (data.msgType === 'ERROR') { this.$notify({ title: '', @@ -83,7 +82,48 @@ export default { webSocketSend(agentData) { this.websock.send(agentData) }, + createOrUpdateNotification(data) { + // 更新或创建通知 + if (this.showNotification && this.notificationInstance) { + this.updateNotification(data); + } else { + this.createNotification(data); + } + this.showNotification = true; + }, + updateNotification(data) { + // 尝试更新通知内容 + try { + this.notificationInstance.title = '点击关注公众号'; + this.notificationInstance.message = data.msg; + } catch (error) { + // 如果更新失败,关闭当前通知并重新创建 + this.createNotification(data); + } + }, + + createNotification(data) { + // 创建一个新的通知实例 + this.notificationInstance = this.$notify({ + title: '点击关注公众号', + message: data.msg, + type: 'success', + offset: 100, + duration: 0 + }); + } + }, + beforeDestroy() { + if (this.pollingTimer) { + clearInterval(this.pollingTimer); + } + if (this.websock && this.websock.readyState === WebSocket.OPEN) { + this.websock.close(); + } + if (this.notificationInstance) { + this.notificationInstance.close(); // 关闭通知 + } } }