refactor: 消息通知弹框优化

This commit is contained in:
Captain.B 2021-10-12 11:15:55 +08:00 committed by 刘瑞斌
parent d74cb6d4f7
commit 71a2835dcf
2 changed files with 22 additions and 6 deletions

View File

@ -1,9 +1,12 @@
package io.metersphere.websocket; package io.metersphere.websocket;
import com.alibaba.fastjson.JSON;
import io.metersphere.base.domain.Notification; import io.metersphere.base.domain.Notification;
import io.metersphere.commons.constants.NotificationConstants; import io.metersphere.commons.constants.NotificationConstants;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.notice.service.NotificationService; import io.metersphere.notice.service.NotificationService;
import lombok.Builder;
import lombok.Data;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -98,10 +101,21 @@ public class NotificationWebSocket {
notification.setReceiver(userId); notification.setReceiver(userId);
notification.setStatus(NotificationConstants.Status.UNREAD.name()); notification.setStatus(NotificationConstants.Status.UNREAD.name());
int count = notificationService.countNotification(notification); 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) { } catch (Exception e) {
LogUtil.error(e.getMessage(), e); LogUtil.error(e.getMessage(), e);
} }
} }
} }
@Data
@Builder
public static class NotificationMessage {
private Integer count;
private Long now;
}
} }

View File

@ -62,6 +62,7 @@ export default {
data() { data() {
return { return {
noticeCount: 0, noticeCount: 0,
serverTime: new Date().getTime(),
taskVisible: false, taskVisible: false,
result: {}, result: {},
systemNoticeData: [], systemNoticeData: [],
@ -120,13 +121,14 @@ export default {
onError(e) { onError(e) {
}, },
onMessage(e) { onMessage(e) {
let count = e.data; let m = JSON.parse(e.data);
this.noticeCount = count; this.noticeCount = m.count;
this.serverTime = m.now;
this.initIndex++; this.initIndex++;
if (count > 0) { if (this.noticeCount > 0) {
this.showNotification(); this.showNotification();
} }
if (this.taskVisible && count > 0 && this.initEnd) { if (this.taskVisible && this.noticeCount > 0 && this.initEnd) {
this.$refs.systemNotice.init(); this.$refs.systemNotice.init();
this.$refs.mentionedMe.init(); this.$refs.mentionedMe.init();
} }
@ -156,7 +158,7 @@ export default {
showNotification() { showNotification() {
this.result = this.$post('/notification/list/all/' + 1 + '/' + 10, {}, response => { this.result = this.$post('/notification/list/all/' + 1 + '/' + 10, {}, response => {
let data = response.data.listObject; let data = response.data.listObject;
let now = new Date().getTime(); let now = this.serverTime;
data.filter(d => d.status === 'UNREAD').forEach(d => { data.filter(d => d.status === 'UNREAD').forEach(d => {
if (now - d.createTime > 10 * 1000) { if (now - d.createTime > 10 * 1000) {
return; return;