notificationMessage.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { defineStore } from 'pinia'
  2. interface NotificationMessageState {
  3. notificationMsgList: string[] // 页面中监听的未读消息id
  4. readCardMap: string[] // 已读的消息id
  5. requestedReadList: string[] // 已请求置为已读的消息id
  6. }
  7. export const useNotificationMessage = defineStore('notificationMessage', {
  8. state: (): NotificationMessageState => ({
  9. notificationMsgList: JSON.parse(localStorage.getItem('notificationMsgList')) || [],
  10. readCardMap: JSON.parse(localStorage.getItem('readCardMap')) || [],
  11. requestedReadList: JSON.parse(localStorage.getItem('requestedReadList')) || []
  12. }),
  13. getters: {},
  14. actions: {
  15. pushNotificationMessage(array: any[]) {
  16. this.notificationMsgList = [...this.notificationMsgList, ...array]
  17. localStorage.setItem('notificationMsgList', JSON.stringify(this.notificationMsgList))
  18. },
  19. setReadCardMap(id: string) {
  20. if (this.readCardMap.includes(id) || this.requestedReadList.includes(id)) return
  21. this.readCardMap.push(id)
  22. localStorage.setItem('readCardMap', JSON.stringify(this.readCardMap))
  23. },
  24. markMessageAsRead() {
  25. if (this.readCardMap.length === 0) return
  26. $api.setMessageRead({ id: this.readCardMap }).then((res) => {
  27. if (res.code === 200) {
  28. this.requestedReadList = this.readCardMap
  29. localStorage.setItem('requestedReadList', JSON.stringify(this.requestedReadList))
  30. this.readCardMap = []
  31. localStorage.setItem('readCardMap', JSON.stringify(this.readCardMap))
  32. this.notificationMsgList = this.notificationMsgList.filter(
  33. (item) => !this.readCardMap.includes(item.id)
  34. )
  35. }
  36. })
  37. },
  38. clearData() {
  39. this.notificationMsgList = []
  40. this.readCardMap = []
  41. this.requestedReadList = []
  42. localStorage.removeItem('notificationMsgList')
  43. localStorage.removeItem('readCardMap')
  44. localStorage.removeItem('requestedReadList')
  45. }
  46. }
  47. })