From 71a2835dcffff76e1f1250a33ecba39b53ac0e3e Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Tue, 12 Oct 2021 11:15:55 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=B6=88=E6=81=AF=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E5=BC=B9=E6=A1=86=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../websocket/NotificationWebSocket.java | 16 +++++++++++++++- .../business/components/notice/Notification.vue | 12 +++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/io/metersphere/websocket/NotificationWebSocket.java b/backend/src/main/java/io/metersphere/websocket/NotificationWebSocket.java index a69b04495d..87779101ca 100644 --- a/backend/src/main/java/io/metersphere/websocket/NotificationWebSocket.java +++ b/backend/src/main/java/io/metersphere/websocket/NotificationWebSocket.java @@ -1,9 +1,12 @@ package io.metersphere.websocket; +import com.alibaba.fastjson.JSON; import io.metersphere.base.domain.Notification; import io.metersphere.commons.constants.NotificationConstants; import io.metersphere.commons.utils.LogUtil; import io.metersphere.notice.service.NotificationService; +import lombok.Builder; +import lombok.Data; import org.springframework.stereotype.Component; import javax.annotation.Resource; @@ -98,10 +101,21 @@ public class NotificationWebSocket { notification.setReceiver(userId); notification.setStatus(NotificationConstants.Status.UNREAD.name()); int count = notificationService.countNotification(notification); - session.getBasicRemote().sendText(count + ""); + NotificationMessage message = NotificationMessage.builder() + .count(count) + .now(System.currentTimeMillis()) + .build(); + session.getBasicRemote().sendText(JSON.toJSONString(message)); } catch (Exception e) { LogUtil.error(e.getMessage(), e); } } } + + @Data + @Builder + public static class NotificationMessage { + private Integer count; + private Long now; + } } diff --git a/frontend/src/business/components/notice/Notification.vue b/frontend/src/business/components/notice/Notification.vue index 5fe7c46b97..b85ea54c2b 100644 --- a/frontend/src/business/components/notice/Notification.vue +++ b/frontend/src/business/components/notice/Notification.vue @@ -62,6 +62,7 @@ export default { data() { return { noticeCount: 0, + serverTime: new Date().getTime(), taskVisible: false, result: {}, systemNoticeData: [], @@ -120,13 +121,14 @@ export default { onError(e) { }, onMessage(e) { - let count = e.data; - this.noticeCount = count; + let m = JSON.parse(e.data); + this.noticeCount = m.count; + this.serverTime = m.now; this.initIndex++; - if (count > 0) { + if (this.noticeCount > 0) { this.showNotification(); } - if (this.taskVisible && count > 0 && this.initEnd) { + if (this.taskVisible && this.noticeCount > 0 && this.initEnd) { this.$refs.systemNotice.init(); this.$refs.mentionedMe.init(); } @@ -156,7 +158,7 @@ export default { showNotification() { this.result = this.$post('/notification/list/all/' + 1 + '/' + 10, {}, response => { let data = response.data.listObject; - let now = new Date().getTime(); + let now = this.serverTime; data.filter(d => d.status === 'UNREAD').forEach(d => { if (now - d.createTime > 10 * 1000) { return;