feat: add paginated notifications and unread endpoint

This commit is contained in:
Tim
2025-08-19 18:20:26 +08:00
parent 77856ff9af
commit 73168c1859
7 changed files with 96 additions and 26 deletions

View File

@@ -118,7 +118,9 @@ export async function updateNotificationPreference(type, enabled) {
function createFetchNotifications() {
const notifications = ref([])
const isLoadingMessage = ref(false)
const fetchNotifications = async () => {
const page = ref(0)
const currentUnread = ref(false)
const fetchNotifications = async ({ unread = false, reset = false } = {}) => {
const config = useRuntimeConfig()
const API_BASE_URL = config.public.apiBaseUrl
if (isLoadingMessage && notifications && markRead) {
@@ -126,11 +128,18 @@ function createFetchNotifications() {
const token = getToken()
if (!token) {
toast.error('请先登录')
return
return false
}
if (reset) {
notifications.value = []
page.value = 0
currentUnread.value = unread
}
isLoadingMessage.value = true
notifications.value = []
const res = await fetch(`${API_BASE_URL}/api/notifications`, {
const endpoint = currentUnread.value
? `/api/notifications/unread?page=${page.value}`
: `/api/notifications?page=${page.value}`
const res = await fetch(`${API_BASE_URL}${endpoint}`, {
headers: {
Authorization: `Bearer ${token}`,
},
@@ -138,7 +147,7 @@ function createFetchNotifications() {
isLoadingMessage.value = false
if (!res.ok) {
toast.error('获取通知失败')
return
return true
}
const data = await res.json()
@@ -284,10 +293,14 @@ function createFetchNotifications() {
})
}
}
page.value++
return data.length < 50
} catch (e) {
console.error(e)
return true
}
}
return true
}
const markRead = async (id) => {
@@ -335,7 +348,6 @@ function createFetchNotifications() {
markRead,
notifications,
isLoadingMessage,
markRead,
markAllRead,
}
}