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([]) let timer: number | null = null async function fetchUnread(): Promise { unreadCount.value = await getUnreadCount() } async function fetchList(): Promise { const r = await listMyNotices({ page: 1, pageSize: 10 }) list.value = r.list } async function readOne(id: string): Promise { await markRead(id) await Promise.all([fetchUnread(), fetchList()]) } async function readAll(): Promise { 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 } })