Merge branch 'main' of github.com:nagisa77/OpenIsle

This commit is contained in:
Tim
2025-07-29 17:04:08 +08:00
4 changed files with 31 additions and 12 deletions

View File

@@ -45,7 +45,7 @@
<script> <script>
import { authState, clearToken, loadCurrentUser } from '../utils/auth' import { authState, clearToken, loadCurrentUser } from '../utils/auth'
import { watch, nextTick } from 'vue' import { watch, nextTick } from 'vue'
import { fetchUnreadCount } from '../utils/notification' import { fetchUnreadCount, notificationState } from '../utils/notification'
import DropdownMenu from './DropdownMenu.vue' import DropdownMenu from './DropdownMenu.vue'
import SearchDropdown from './SearchDropdown.vue' import SearchDropdown from './SearchDropdown.vue'
import { isMobile } from '../utils/screen' import { isMobile } from '../utils/screen'
@@ -62,8 +62,7 @@ export default {
data() { data() {
return { return {
avatar: '', avatar: '',
showSearch: false, showSearch: false
unreadCount: 0
} }
}, },
computed: { computed: {
@@ -79,6 +78,9 @@ export default {
{ text: '个人主页', onClick: this.goToProfile }, { text: '个人主页', onClick: this.goToProfile },
{ text: '退出', onClick: this.goToLogout } { text: '退出', onClick: this.goToLogout }
] ]
},
unreadCount() {
return notificationState.unreadCount
} }
}, },
async mounted() { async mounted() {
@@ -92,9 +94,9 @@ export default {
} }
const updateUnread = async () => { const updateUnread = async () => {
if (authState.loggedIn) { if (authState.loggedIn) {
this.unreadCount = await fetchUnreadCount() await fetchUnreadCount()
} else { } else {
this.unreadCount = 0 notificationState.unreadCount = 0
} }
} }

View File

@@ -111,7 +111,7 @@
<script> <script>
import { themeState, cycleTheme, ThemeMode } from '../utils/theme' import { themeState, cycleTheme, ThemeMode } from '../utils/theme'
import { authState } from '../utils/auth' import { authState } from '../utils/auth'
import { fetchUnreadCount } from '../utils/notification' import { fetchUnreadCount, notificationState } from '../utils/notification'
import { watch } from 'vue' import { watch } from 'vue'
import { API_BASE_URL } from '../main' import { API_BASE_URL } from '../main'
import { hatch } from 'ldrs' import { hatch } from 'ldrs'
@@ -127,7 +127,6 @@ export default {
}, },
data() { data() {
return { return {
unreadCount: 0,
categories: [], categories: [],
tags: [], tags: [],
categoryOpen: true, categoryOpen: true,
@@ -147,6 +146,9 @@ export default {
return 'fas fa-desktop' return 'fas fa-desktop'
} }
}, },
unreadCount() {
return notificationState.unreadCount
},
showUnreadCount() { showUnreadCount() {
return this.unreadCount > 99 ? '99+' : this.unreadCount return this.unreadCount > 99 ? '99+' : this.unreadCount
}, },
@@ -157,9 +159,9 @@ export default {
async mounted() { async mounted() {
const updateCount = async () => { const updateCount = async () => {
if (authState.loggedIn) { if (authState.loggedIn) {
this.unreadCount = await fetchUnreadCount() await fetchUnreadCount()
} else { } else {
this.unreadCount = 0 notificationState.unreadCount = 0
} }
} }
await updateCount() await updateCount()

View File

@@ -1,17 +1,30 @@
import { API_BASE_URL } from '../main' import { API_BASE_URL } from '../main'
import { getToken } from './auth' import { getToken } from './auth'
import { reactive } from 'vue'
export const notificationState = reactive({
unreadCount: 0
})
export async function fetchUnreadCount() { export async function fetchUnreadCount() {
try { try {
const token = getToken() const token = getToken()
if (!token) return 0 if (!token) {
notificationState.unreadCount = 0
return 0
}
const res = await fetch(`${API_BASE_URL}/api/notifications/unread-count`, { const res = await fetch(`${API_BASE_URL}/api/notifications/unread-count`, {
headers: { Authorization: `Bearer ${token}` } headers: { Authorization: `Bearer ${token}` }
}) })
if (!res.ok) return 0 if (!res.ok) {
notificationState.unreadCount = 0
return 0
}
const data = await res.json() const data = await res.json()
notificationState.unreadCount = data.count
return data.count return data.count
} catch (e) { } catch (e) {
notificationState.unreadCount = 0
return 0 return 0
} }
} }

View File

@@ -238,7 +238,7 @@ import BaseTimeline from '../components/BaseTimeline.vue'
import BasePlaceholder from '../components/BasePlaceholder.vue' import BasePlaceholder from '../components/BasePlaceholder.vue'
import NotificationContainer from '../components/NotificationContainer.vue' import NotificationContainer from '../components/NotificationContainer.vue'
import { getToken, authState } from '../utils/auth' import { getToken, authState } from '../utils/auth'
import { markNotificationsRead } from '../utils/notification' import { markNotificationsRead, fetchUnreadCount } from '../utils/notification'
import { toast } from '../main' import { toast } from '../main'
import { stripMarkdown } from '../utils/markdown' import { stripMarkdown } from '../utils/markdown'
import TimeManager from '../utils/time' import TimeManager from '../utils/time'
@@ -265,6 +265,7 @@ export default {
if (ok) { if (ok) {
const n = notifications.value.find(n => n.id === id) const n = notifications.value.find(n => n.id === id)
if (n) n.read = true if (n) n.read = true
await fetchUnreadCount()
} }
} }
@@ -276,6 +277,7 @@ export default {
notifications.value.forEach(n => { notifications.value.forEach(n => {
if (n.type !== 'REGISTER_REQUEST') n.read = true if (n.type !== 'REGISTER_REQUEST') n.read = true
}) })
await fetchUnreadCount()
toast.success('已读所有消息(注册请求除外)') toast.success('已读所有消息(注册请求除外)')
} }
} }