feat: 完善接口19-顶部通知铃铛与站内通知中心
All checks were successful
构建并部署前端到测试环境 / build-and-deploy (push) Successful in 4m5s
All checks were successful
构建并部署前端到测试环境 / build-and-deploy (push) Successful in 4m5s
This commit is contained in:
@@ -1,22 +1,28 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import { NotificationService } from '@/api/modules/notification'
|
||||
import type { NotificationSummary } from '@/types/api'
|
||||
import type { NotificationItem, NotificationUnreadSummary } from '@/types/api'
|
||||
|
||||
const getCountValue = (data: number | { count?: number; unread_count?: number } | undefined) => {
|
||||
if (typeof data === 'number') return Math.max(0, data)
|
||||
return Math.max(0, data?.unread_count ?? data?.count ?? 0)
|
||||
}
|
||||
const emptySummary = (): NotificationUnreadSummary => ({
|
||||
approval: 0,
|
||||
expiry: 0,
|
||||
sync: 0,
|
||||
system: 0,
|
||||
total: 0
|
||||
})
|
||||
|
||||
export const useNotificationStore = defineStore('notificationStore', () => {
|
||||
const unreadCount = ref(0)
|
||||
const summary = ref<NotificationSummary>({ items: [], categories: {}, total_unread: 0 })
|
||||
const displayCount = ref('0')
|
||||
const summary = ref<NotificationUnreadSummary>(emptySummary())
|
||||
const recentNotifications = ref<NotificationItem[]>([])
|
||||
const loading = ref(false)
|
||||
|
||||
const refreshUnreadCount = async () => {
|
||||
const response = await NotificationService.getUnreadCount()
|
||||
if (response.code === 0) {
|
||||
unreadCount.value = getCountValue(response.data)
|
||||
if (response.code === 0 && response.data) {
|
||||
unreadCount.value = Math.max(0, response.data.count)
|
||||
displayCount.value = response.data.display_count || '0'
|
||||
}
|
||||
return unreadCount.value
|
||||
}
|
||||
@@ -24,15 +30,15 @@ export const useNotificationStore = defineStore('notificationStore', () => {
|
||||
const refreshSummary = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const response = await NotificationService.getUnreadSummary()
|
||||
if (response.code === 0 && response.data) {
|
||||
summary.value = {
|
||||
...response.data,
|
||||
items: (response.data.items || []).slice(0, 10)
|
||||
}
|
||||
if (response.data.total_unread !== undefined) {
|
||||
unreadCount.value = Math.max(0, response.data.total_unread)
|
||||
}
|
||||
const [summaryResponse, listResponse] = await Promise.all([
|
||||
NotificationService.getUnreadSummary(),
|
||||
NotificationService.getNotifications({ page: 1, page_size: 10 })
|
||||
])
|
||||
if (summaryResponse.code === 0 && summaryResponse.data) {
|
||||
summary.value = summaryResponse.data
|
||||
}
|
||||
if (listResponse.code === 0 && listResponse.data) {
|
||||
recentNotifications.value = listResponse.data.items.slice(0, 10)
|
||||
}
|
||||
await refreshUnreadCount()
|
||||
return summary.value
|
||||
@@ -41,46 +47,27 @@ export const useNotificationStore = defineStore('notificationStore', () => {
|
||||
}
|
||||
}
|
||||
|
||||
const markRead = async (id: number | string) => {
|
||||
const markRead = async (id: number) => {
|
||||
const response = await NotificationService.markRead(id)
|
||||
if (response.code === 0) {
|
||||
unreadCount.value = Math.max(0, unreadCount.value - 1)
|
||||
summary.value = {
|
||||
...summary.value,
|
||||
items: summary.value.items.map((item) =>
|
||||
item.id === id ? { ...item, read: true, is_read: true, read_status: 'read' } : item
|
||||
)
|
||||
}
|
||||
await refreshUnreadCount()
|
||||
await refreshSummary()
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
const markAllRead = async () => {
|
||||
const response = await NotificationService.markAllRead()
|
||||
const markAllRead = async (category?: string) => {
|
||||
const response = await NotificationService.markAllRead(category ? { category } : {})
|
||||
if (response.code === 0) {
|
||||
unreadCount.value = 0
|
||||
summary.value = {
|
||||
...summary.value,
|
||||
total_unread: 0,
|
||||
categories: Object.fromEntries(
|
||||
Object.keys(summary.value.categories || {}).map((key) => [key, 0])
|
||||
),
|
||||
items: summary.value.items.map((item) => ({
|
||||
...item,
|
||||
read: true,
|
||||
is_read: true,
|
||||
read_status: 'read'
|
||||
}))
|
||||
}
|
||||
await refreshUnreadCount()
|
||||
await refreshSummary()
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
return {
|
||||
unreadCount,
|
||||
displayCount,
|
||||
summary,
|
||||
recentNotifications,
|
||||
loading,
|
||||
refreshUnreadCount,
|
||||
refreshSummary,
|
||||
|
||||
Reference in New Issue
Block a user