GuDingZiChan/frontend/src/stores/notifications.ts

31 lines
738 B
TypeScript

import { defineStore } from 'pinia'
import { getUnreadNotificationCount } from '@/api/notifications'
interface NotificationState {
unreadCount: number
loaded: boolean
}
export const useNotificationStore = defineStore('notifications', {
state: (): NotificationState => ({
unreadCount: 0,
loaded: false
}),
actions: {
setUnreadCount(count: number) {
this.unreadCount = Math.max(0, Number(count) || 0)
this.loaded = true
},
async fetchUnreadCount() {
try {
const response = await getUnreadNotificationCount()
this.setUnreadCount(response.data?.count || 0)
} catch (error) {
console.warn('[notifications] 获取未读数量失败', error)
}
}
}
})