mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-06-02 07:57:38 +08:00
feat: add initial Nuxt frontend with SSR
This commit is contained in:
48
frontend_nuxt/utils/notification.js
Normal file
48
frontend_nuxt/utils/notification.js
Normal file
@@ -0,0 +1,48 @@
|
||||
import { API_BASE_URL } from '~/main'
|
||||
import { getToken } from './auth'
|
||||
import { reactive } from 'vue'
|
||||
|
||||
export const notificationState = reactive({
|
||||
unreadCount: 0
|
||||
})
|
||||
|
||||
export async function fetchUnreadCount() {
|
||||
try {
|
||||
const token = getToken()
|
||||
if (!token) {
|
||||
notificationState.unreadCount = 0
|
||||
return 0
|
||||
}
|
||||
const res = await fetch(`${API_BASE_URL}/api/notifications/unread-count`, {
|
||||
headers: { Authorization: `Bearer ${token}` }
|
||||
})
|
||||
if (!res.ok) {
|
||||
notificationState.unreadCount = 0
|
||||
return 0
|
||||
}
|
||||
const data = await res.json()
|
||||
notificationState.unreadCount = data.count
|
||||
return data.count
|
||||
} catch (e) {
|
||||
notificationState.unreadCount = 0
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
export async function markNotificationsRead(ids) {
|
||||
try {
|
||||
const token = getToken()
|
||||
if (!token || !ids || ids.length === 0) return false
|
||||
const res = await fetch(`${API_BASE_URL}/api/notifications/read`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ ids })
|
||||
})
|
||||
return res.ok
|
||||
} catch (e) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user