|
|
@@ -3,48 +3,71 @@ import { defineStore } from 'pinia'
|
|
|
interface NotificationMessageState {
|
|
|
notificationMsgList: string[] // 页面中监听的未读消息id
|
|
|
readCardMap: string[] // 已读的消息id
|
|
|
- requestedReadList: string[] // 已请求置为已读的消息id
|
|
|
+ hasNewMsg: boolean
|
|
|
}
|
|
|
|
|
|
export const useNotificationMessage = defineStore('notificationMessage', {
|
|
|
state: (): NotificationMessageState => ({
|
|
|
notificationMsgList: JSON.parse(localStorage.getItem('notificationMsgList')) || [],
|
|
|
readCardMap: JSON.parse(localStorage.getItem('readCardMap')) || [],
|
|
|
- requestedReadList: JSON.parse(localStorage.getItem('requestedReadList')) || []
|
|
|
+ hasNewMsg: JSON.parse(localStorage.getItem('hasNewMsg')) || false
|
|
|
}),
|
|
|
getters: {},
|
|
|
actions: {
|
|
|
- pushNotificationMessage(array: any[]) {
|
|
|
- this.notificationMsgList = [...this.notificationMsgList, ...array]
|
|
|
+ concatNotificationMsgList(array: any[]) {
|
|
|
+ this.notificationMsgList = [...new Set([...this.notificationMsgList, ...array])]
|
|
|
+ localStorage.setItem('notificationMsgList', JSON.stringify(this.notificationMsgList))
|
|
|
+ },
|
|
|
+ removeNotificationMsgList(array: any[]) {
|
|
|
+ this.notificationMsgList = this.notificationMsgList.filter((item) => !array.includes(item))
|
|
|
localStorage.setItem('notificationMsgList', JSON.stringify(this.notificationMsgList))
|
|
|
},
|
|
|
setReadCardMap(id: string) {
|
|
|
- if (this.readCardMap.includes(id) || this.requestedReadList.includes(id)) return
|
|
|
+ if (this.readCardMap.includes(id)) return
|
|
|
this.readCardMap.push(id)
|
|
|
localStorage.setItem('readCardMap', JSON.stringify(this.readCardMap))
|
|
|
+ this.notificationMsgList = this.notificationMsgList.filter((item) => {
|
|
|
+ return !this.readCardMap.includes(item)
|
|
|
+ })
|
|
|
+ localStorage.setItem('notificationMsgList', JSON.stringify(this.notificationMsgList))
|
|
|
+ },
|
|
|
+ hasUnreadMessages() {
|
|
|
+ $api.hasUnreadMessages().then((res) => {
|
|
|
+ if (res.code === 200) {
|
|
|
+ // this.hasNewMsg = res.data.has_message
|
|
|
+ this.hasNewMsg = false
|
|
|
+ localStorage.setItem('hasNewMsg', JSON.stringify(this.hasNewMsg))
|
|
|
+ }
|
|
|
+ })
|
|
|
},
|
|
|
markMessageAsRead() {
|
|
|
if (this.readCardMap.length === 0) return
|
|
|
- $api.setMessageRead({ id: this.readCardMap }).then((res) => {
|
|
|
+
|
|
|
+ // this.readCardMap = []
|
|
|
+ // localStorage.setItem('readCardMap', JSON.stringify(this.readCardMap))
|
|
|
+
|
|
|
+ // { id: this.readCardMap }
|
|
|
+ $api.setMessageRead().then((res) => {
|
|
|
if (res.code === 200) {
|
|
|
- this.requestedReadList = this.readCardMap
|
|
|
- localStorage.setItem('requestedReadList', JSON.stringify(this.requestedReadList))
|
|
|
this.readCardMap = []
|
|
|
localStorage.setItem('readCardMap', JSON.stringify(this.readCardMap))
|
|
|
|
|
|
this.notificationMsgList = this.notificationMsgList.filter(
|
|
|
(item) => !this.readCardMap.includes(item.id)
|
|
|
)
|
|
|
+ localStorage.setItem('notificationMsgList', JSON.stringify(this.notificationMsg))
|
|
|
+ // 在将消息标记为已读后,再次检查是否有新消息
|
|
|
+ this.hasUnreadMessages()
|
|
|
}
|
|
|
})
|
|
|
},
|
|
|
clearData() {
|
|
|
this.notificationMsgList = []
|
|
|
this.readCardMap = []
|
|
|
- this.requestedReadList = []
|
|
|
+ this.hasNewMsg = false
|
|
|
+ localStorage.removeItem('hasNewMsg')
|
|
|
localStorage.removeItem('notificationMsgList')
|
|
|
localStorage.removeItem('readCardMap')
|
|
|
- localStorage.removeItem('requestedReadList')
|
|
|
}
|
|
|
}
|
|
|
})
|