serviceCenterNoticeService.js
2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* 服务中心公告:角标拉取(TTL + in-flight)与首条未读(status:0,无本地门禁)
*/
import { noticeSort } from './enum/serviceCenter.js'
const CACHE_TTL_MS = 60 * 1000
let unreadCache = { value: null, ts: 0, pending: null }
let popupProbeCache = { value: null, ts: 0, pending: null }
let listApiOverride = null
function getListApi() {
if (listApiOverride) return listApiOverride
const mod = require('~/api/serviceCenter/index')
return mod.getCompanyNoticeList
}
function readCacheEntry(entry) {
if (entry.value === null) return null
if (Date.now() - entry.ts > CACHE_TTL_MS) {
entry.value = null
entry.ts = 0
return null
}
return entry.value
}
async function probeNoticeLists() {
const cached = readCacheEntry(popupProbeCache)
if (cached) return cached
if (popupProbeCache.pending) return popupProbeCache.pending
const getCompanyNoticeList = getListApi()
popupProbeCache.pending = (async () => {
const probes = []
for (const moduleType of noticeSort) {
const { data } = await getCompanyNoticeList({
pageNum: 1,
pageSize: 3,
createdTimeSortType: 0,
moduleType,
status: 0
})
const list = data?.list || []
probes.push({ moduleType, list, unreadCount: list.length })
}
popupProbeCache.value = probes
popupProbeCache.ts = Date.now()
return probes
})()
try {
return await popupProbeCache.pending
} finally {
popupProbeCache.pending = null
}
}
export async function fetchServiceCenterUnread() {
const cached = readCacheEntry(unreadCache)
if (cached !== null) return cached
if (unreadCache.pending) return unreadCache.pending
unreadCache.pending = (async () => {
const probes = await probeNoticeLists()
let count = 0
for (const p of probes) {
if (p.unreadCount > 0) {
count = p.unreadCount
break
}
}
unreadCache.value = count
unreadCache.ts = Date.now()
return count
})()
try {
return await unreadCache.pending
} finally {
unreadCache.pending = null
}
}
/** 按 noticeSort 返回首条未读公告(V5.8.1:有未读即弹,依赖服务端 status) */
export async function resolveFirstUnreadNotice() {
const probes = await probeNoticeLists()
for (const { moduleType, list } of probes) {
if (!list?.length) continue
return { moduleType, noticeItem: list[0] }
}
return null
}
export function clearServiceCenterNoticeCache() {
unreadCache = { value: null, ts: 0, pending: null }
popupProbeCache = { value: null, ts: 0, pending: null }
}
export function __setListApiForTest(fn) {
listApiOverride = fn
}