|
@@ -107,32 +107,77 @@ export default {
|
|
data() {
|
|
data() {
|
|
return {
|
|
return {
|
|
msgCount:0,
|
|
msgCount:0,
|
|
|
|
+ previousMsgCount: 0, // 保存上一次的消息计数
|
|
msg:{
|
|
msg:{
|
|
open:false,
|
|
open:false,
|
|
title:'通知消息'
|
|
title:'通知消息'
|
|
},
|
|
},
|
|
|
|
+ msgPollingTimer: null, // 轮询定时器
|
|
}
|
|
}
|
|
},
|
|
},
|
|
created() {
|
|
created() {
|
|
this.getMsgCount();
|
|
this.getMsgCount();
|
|
|
|
+ // 启动消息轮询,每30秒检查一次新消息
|
|
|
|
+ this.startMsgPolling();
|
|
|
|
+ },
|
|
|
|
+ beforeDestroy() {
|
|
|
|
+ // 组件销毁前清除定时器
|
|
|
|
+ this.stopMsgPolling();
|
|
},
|
|
},
|
|
methods: {
|
|
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(){
|
|
- // 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(){
|
|
openMsg(){
|
|
console.log(11);
|
|
console.log(11);
|
|
this.msg.open=true;
|
|
this.msg.open=true;
|
|
|
|
+ // 每次打开消息界面时刷新数据
|
|
|
|
+ if (this.$refs.msg) {
|
|
|
|
+ this.$refs.msg.getMsg();
|
|
|
|
+ }
|
|
|
|
+ // 打开消息后重新获取消息计数来更新角标
|
|
|
|
+ this.getMsgCount();
|
|
},
|
|
},
|
|
toggleSideBar() {
|
|
toggleSideBar() {
|
|
this.$store.dispatch('app/toggleSideBar')
|
|
this.$store.dispatch('app/toggleSideBar')
|