index.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { useCallback, useEffect, useState } from 'react'
  2. import { Tag, message } from 'antd'
  3. import { groupBy } from 'lodash-es'
  4. import { useModel } from 'umi'
  5. import NoticeIcon from './NoticeIcon'
  6. import styles from './index.less'
  7. import dayjs from 'dayjs'
  8. import { queryNoticeList, updateNotice } from '@/services/login'
  9. import consts from '@/consts'
  10. export type GlobalHeaderRightProps = {
  11. fetchingNotices?: boolean
  12. onNoticeVisibleChange?: (visible: boolean) => void
  13. onNoticeClear?: (tabName?: string) => void
  14. }
  15. const getNoticeData = notices => {
  16. if (!notices || notices.length === 0 || !Array.isArray(notices)) {
  17. return {}
  18. }
  19. const newNotices = notices.map(notice => {
  20. const newNotice = { ...notice }
  21. if (newNotice.datetime) {
  22. newNotice.createTime = dayjs(notice.datetime as string).diff(dayjs(), 'date')
  23. }
  24. if (newNotice.id) {
  25. newNotice.key = newNotice.id
  26. }
  27. if (newNotice.extra && newNotice.status) {
  28. const color = {
  29. todo: '',
  30. processing: 'blue',
  31. urgent: 'red',
  32. doing: 'gold'
  33. }[newNotice.status]
  34. newNotice.extra = (
  35. <Tag
  36. color={color}
  37. style={{
  38. marginRight: 0
  39. }}
  40. >
  41. {newNotice.extra}
  42. </Tag>
  43. ) as any
  44. }
  45. return newNotice
  46. })
  47. return groupBy(newNotices, 'msgType')
  48. }
  49. const getUnreadData = (noticeData: Record<string, API.NoticeIconItem[]>) => {
  50. const unreadMsg: Record<string, number> = {}
  51. Object.keys(noticeData).forEach(key => {
  52. const value = noticeData[key]
  53. if (!unreadMsg[key]) {
  54. unreadMsg[key] = 0
  55. }
  56. if (Array.isArray(value)) {
  57. unreadMsg[key] = value.filter(item => !item.read).length
  58. }
  59. })
  60. return unreadMsg
  61. }
  62. const NoticeIconView = () => {
  63. const { initialState } = useModel('@@initialState')
  64. const { currentUser } = initialState || {}
  65. const [notices, setNotices] = useState([])
  66. const getNotices = useCallback(async () => {
  67. const { code = -1, data } = await queryNoticeList()
  68. if (code === consts.RET_CODE.SUCCESS) {
  69. setNotices(data.message)
  70. }
  71. }, [])
  72. useEffect(() => {
  73. getNotices()
  74. }, [])
  75. const noticeData = getNoticeData(notices)
  76. const unreadMsg = getUnreadData(noticeData || {})
  77. const changeReadState = async (id: string) => {
  78. const { code = -1 } = await updateNotice({ id })
  79. if (code === consts.RET_CODE.SUCCESS) {
  80. setNotices(
  81. notices.map(item => {
  82. const notice = { ...item }
  83. if (notice.id === id) {
  84. notice.isRead = true
  85. }
  86. return notice
  87. })
  88. )
  89. }
  90. }
  91. const clearReadState = (title: string, key: string) => {
  92. setNotices(
  93. notices.map(item => {
  94. const notice = { ...item }
  95. if (notice.msgType === key) {
  96. notice.isRead = true
  97. }
  98. return notice
  99. })
  100. )
  101. message.success(`${'清空了'} ${title}`)
  102. }
  103. return (
  104. <NoticeIcon
  105. className={styles.action}
  106. count={currentUser && currentUser.unreadCount}
  107. onItemClick={item => {
  108. changeReadState(item.id!)
  109. }}
  110. onClear={(title: string, key: string) => clearReadState(title, key)}
  111. loading={false}
  112. clearText="清空"
  113. viewMoreText="查看更多"
  114. onViewMore={() => message.info('Click on view more')}
  115. clearClose
  116. >
  117. <NoticeIcon.Tab
  118. tabKey="notification"
  119. count={unreadMsg.notification}
  120. list={noticeData.notification}
  121. title="通知"
  122. emptyText="你已查看所有通知"
  123. showViewMore
  124. />
  125. <NoticeIcon.Tab
  126. tabKey="message"
  127. count={unreadMsg.message}
  128. list={noticeData.message}
  129. title="消息"
  130. emptyText="您已读完所有消息"
  131. showViewMore
  132. />
  133. <NoticeIcon.Tab
  134. tabKey="event"
  135. title="待办"
  136. emptyText="你已完成所有待办"
  137. count={unreadMsg.event}
  138. list={noticeData.event}
  139. showViewMore
  140. />
  141. </NoticeIcon>
  142. )
  143. }
  144. export default NoticeIconView