msgBadge.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { http } from '@/common/service/service.js'
  2. import { ACCESS_TOKEN } from '@/common/util/constants.js'
  3. /** 消息 tab 在 tabBar.list 中的下标 */
  4. export const MSG_TAB_INDEX = 2
  5. /** 是否未读:status === '0' */
  6. export function isUnreadNotice(item) {
  7. return item && String(item.status) === '0'
  8. }
  9. /**
  10. * 设置消息 tab 角标(仅未读;已读不显示)
  11. * @param {number|string} count 未读数
  12. */
  13. export function setMsgTabBadge(count) {
  14. const n = Number(count) || 0
  15. if (n > 0) {
  16. uni.setTabBarBadge({
  17. index: MSG_TAB_INDEX,
  18. text: n > 99 ? '99+' : String(n),
  19. fail: () => {}
  20. })
  21. } else {
  22. uni.removeTabBarBadge({
  23. index: MSG_TAB_INDEX,
  24. fail: () => {}
  25. })
  26. uni.hideTabBarRedDot({
  27. index: MSG_TAB_INDEX,
  28. fail: () => {}
  29. })
  30. }
  31. }
  32. /**
  33. * 从分类列表统计未读数(不含已读)
  34. */
  35. function countUnreadFromList(list) {
  36. const unread = (list || []).filter(isUnreadNotice)
  37. if (!unread.length) return 0
  38. const hasCount = unread.some(
  39. (i) => i.unreadCount != null || i.unReadCount != null
  40. )
  41. if (hasCount) {
  42. return unread.reduce(
  43. (sum, i) => sum + Number(i.unreadCount || i.unReadCount || 0),
  44. 0
  45. )
  46. }
  47. return unread.length
  48. }
  49. /**
  50. * 拉取未读消息数并更新消息 tab 角标(只统计没看过的)
  51. */
  52. export function updateMsgTabBadge() {
  53. const token = uni.getStorageSync(ACCESS_TOKEN)
  54. if (!token) {
  55. setMsgTabBadge(0)
  56. return Promise.resolve(0)
  57. }
  58. // 先按未读拉分页,客户端再过滤一次,避免拿到已读/总数据
  59. return http
  60. .post('/localUserNotice/localUserNotice/getPageListNew', {
  61. pageNo: 1,
  62. pageSize: 200,
  63. userType: 1,
  64. status: '0'
  65. })
  66. .then((res) => {
  67. const result = (res.data && res.data.result) || {}
  68. const records = Array.isArray(result.records)
  69. ? result.records
  70. : result.page && Array.isArray(result.page.records)
  71. ? result.page.records
  72. : []
  73. const unreadRecords = records.filter(isUnreadNotice)
  74. // 若返回记录全是未读,且接口给了 total,可用 total;否则只用未读条数
  75. const allUnread =
  76. records.length > 0 && unreadRecords.length === records.length
  77. const apiTotal =
  78. result.total != null
  79. ? result.total
  80. : result.page && result.page.total != null
  81. ? result.page.total
  82. : null
  83. let count = unreadRecords.length
  84. if (allUnread && apiTotal != null && Number(apiTotal) >= count) {
  85. count = Number(apiTotal)
  86. }
  87. setMsgTabBadge(count)
  88. return count
  89. })
  90. .catch(() => {
  91. return http
  92. .get('/localUserNotice/localUserNotice/getListNew', {
  93. params: { userType: 1 }
  94. })
  95. .then((res) => {
  96. const count = countUnreadFromList(
  97. (res.data && res.data.result) || []
  98. )
  99. setMsgTabBadge(count)
  100. return count
  101. })
  102. .catch(() => {
  103. setMsgTabBadge(0)
  104. return 0
  105. })
  106. })
  107. }