- ty-notice 铃铛(未读角标 + 列表 + 已读)+ notice store 轮询 - 通知管理页(ty-crud) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import { ref } from 'vue'
|
|
import { defineStore } from 'pinia'
|
|
import { getUnreadCount, listMyNotices, markAllRead, markRead, type MyNotice } from '@/api/notice'
|
|
|
|
/** 站内通知:未读数 + 最近列表 + 轮询。 */
|
|
export const useNoticeStore = defineStore('noticeStore', () => {
|
|
const unreadCount = ref(0)
|
|
const list = ref<MyNotice[]>([])
|
|
let timer: number | null = null
|
|
|
|
async function fetchUnread(): Promise<void> {
|
|
unreadCount.value = await getUnreadCount()
|
|
}
|
|
|
|
async function fetchList(): Promise<void> {
|
|
const r = await listMyNotices({ page: 1, pageSize: 10 })
|
|
list.value = r.list
|
|
}
|
|
|
|
async function readOne(id: string): Promise<void> {
|
|
await markRead(id)
|
|
await Promise.all([fetchUnread(), fetchList()])
|
|
}
|
|
|
|
async function readAll(): Promise<void> {
|
|
await markAllRead()
|
|
await Promise.all([fetchUnread(), fetchList()])
|
|
}
|
|
|
|
function startPolling(interval = 30000): void {
|
|
stopPolling()
|
|
timer = window.setInterval(() => {
|
|
void fetchUnread()
|
|
}, interval)
|
|
}
|
|
|
|
function stopPolling(): void {
|
|
if (timer != null) {
|
|
clearInterval(timer)
|
|
timer = null
|
|
}
|
|
}
|
|
|
|
return { unreadCount, list, fetchUnread, fetchList, readOne, readAll, startPolling, stopPolling }
|
|
})
|