useUnreadBadge.js 660 B

12345678910111213141516171819202122232425
  1. /**
  2. * 底部「消息」Tab 角标:统一走 GET /messages/unread-count
  3. */
  4. import { getUnreadCount, getToken } from '../utils/api'
  5. import { chatStore } from '../store/chat'
  6. export async function fetchUnreadCountAndUpdateTabBar() {
  7. const token = getToken()
  8. if (!token) {
  9. chatStore.setTabBarUnreadFromServer(0)
  10. return
  11. }
  12. try {
  13. const raw = await getUnreadCount(token)
  14. const n =
  15. typeof raw === 'number'
  16. ? raw
  17. : raw != null && typeof raw === 'object'
  18. ? Number(raw.count ?? raw.unread_count ?? raw.total ?? 0)
  19. : Number(raw) || 0
  20. chatStore.setTabBarUnreadFromServer(n)
  21. } catch (e) {
  22. // 网络失败时保留当前角标
  23. }
  24. }