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(); // 关闭通知 + } } }