소스 검색

1.修改首页消息提醒

jzp 3 일 전
부모
커밋
d5797330ae
1개의 변경된 파일56개의 추가작업 그리고 11개의 파일을 삭제
  1. 56 11
      src/layout/components/Navbar.vue

+ 56 - 11
src/layout/components/Navbar.vue

@@ -107,32 +107,77 @@ export default {
   data() {
     return {
       msgCount:0,
+      previousMsgCount: 0, // 保存上一次的消息计数
       msg:{
         open:false,
         title:'通知消息'
       },
+      msgPollingTimer: null, // 轮询定时器
     }
   },
   created() {
     this.getMsgCount();
+    // 启动消息轮询,每30秒检查一次新消息
+    this.startMsgPolling();
+  },
+  beforeDestroy() {
+    // 组件销毁前清除定时器
+    this.stopMsgPolling();
   },
   methods: {
+    startMsgPolling() {
+      // 清除已存在的定时器
+      if (this.msgPollingTimer) {
+        clearInterval(this.msgPollingTimer);
+      }
+
+      // 设置定时器,每30秒获取一次消息计数
+      this.msgPollingTimer = setInterval(() => {
+        this.getMsgCount();
+      }, 30000); // 30秒轮询一次
+    },
+    stopMsgPolling() {
+      if (this.msgPollingTimer) {
+        clearInterval(this.msgPollingTimer);
+        this.msgPollingTimer = null;
+      }
+    },
     getMsgCount(){
-      // getMsgCount().then(response => {
-      //     this.msgCount = response.counts;
-      //     if(this.msgCount>0){
-      //       this.$notify({
-      //         title: '提示',
-      //         message: '您有'+this.msgCount+"条消息通知",
-      //         position: 'top-right'
-      //       });
-      //     }
-
-      // });
+      getMsg().then(response => {
+        // 计算所有未读消息总数
+        let totalCount = 0;
+        response.counts.forEach(item => {
+          totalCount += item.total;
+        });
+
+        // 保存旧的消息计数
+        this.previousMsgCount = this.msgCount;
+        // 更新消息计数
+        this.msgCount = totalCount;
+
+        // 如果消息计数增加了,显示通知弹框
+        if (this.msgCount > this.previousMsgCount && this.msgCount > 0) {
+          this.$notify({
+            title: '提示',
+            message: '您有'+this.msgCount+"条消息通知",
+            position: 'top-right',
+            type: 'info'
+          });
+        }
+
+      }).catch(error => {
+        console.error("获取消息计数失败:", error);
+      });
     },
     openMsg(){
       console.log(11);
       this.msg.open=true;
+      // 每次打开消息界面时刷新数据
+      if (this.$refs.msg) {
+        this.$refs.msg.getMsg();
+      }
+      // 打开消息后重新获取消息计数来更新角标
+      this.getMsgCount();
     },
     toggleSideBar() {
       this.$store.dispatch('app/toggleSideBar')