2026-05-18 09:46:11 +08:00
< template >
< div class = "notifications-page" >
< header class = "page-header" >
< button class = "back-btn" type = "button" @click ="router.back()" > ‹ </ button >
< div >
< h1 > 消息通知 </ h1 >
< p >{{ unreadCount }} 条未读消息 </ p >
</ div >
</ header >
< main class = "message-list" >
< button
v-for = "item in messages"
:key = "item.id"
class = "message-item"
: class = "{ unread: item.readStatus !== 1 }"
type = "button"
@click ="openMessage(item)"
>
< span class = "dot" ></ span >
< span class = "message-main" >
< strong >{{ item . title }}</ strong >
< small >{{ item . publisherName || '系统消息' }} · {{ item . publishTime || item . createTime }}</ small >
< em v-if = "item.imageUrl" > 含图片 </ em >
</ span >
</ button >
< a-empty v-if = "!loading && messages.length === 0" description="暂无消息" />
</ main >
< a-modal v -model:open = "detailVisible" title = "消息详情" @ok ="detailVisible = false" >
< h3 >{{ currentMessage ? . title }}</ h3 >
< p class = "message-meta" >{{ currentMessage ? . publisherName || '系统消息' }} · {{ currentMessage ? . publishTime }}</ p >
< div v-if = "currentMessage?.content" class="message-content" >{{ currentMessage ? .content }}</ div >
< img v-if = "currentMessage?.imageUrl" class="message-image" :src="currentMessage.imageUrl" alt="消息图片" />
</ a-modal >
</ div >
</ template >
< script setup lang = "ts" >
/**
* NotificationsView page script.
* Call guide: the template binds the reactive state below, and the methods are triggered by button clicks, lifecycle hooks, or router events.
*/
import { computed , onMounted , ref } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
2026-05-23 18:44:57 +08:00
/**
* Purpose: SystemMessageVO supports NotificationsView.vue by holding or executing the system message vo page or API role.
* Usage: import, call, read, or update this type from the owning page/module while preserving the surrounding loading and permission flow.
*/
2026-05-18 09:46:11 +08:00
import { userApi , type SystemMessageVO } from '@/api/user'
2026-05-23 21:56:18 +08:00
import { buildSystemMessageActionPath } from '@/utils/system-message-action'
2026-05-18 09:46:11 +08:00
2026-05-23 18:44:57 +08:00
/**
* Purpose: router supports NotificationsView.vue by holding or executing the router page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
2026-05-18 09:46:11 +08:00
const router = useRouter ()
/** loading is a reactive state variable used by the template and event handlers. */
const loading = ref ( false )
2026-05-23 18:44:57 +08:00
/**
* Purpose: messages supports NotificationsView.vue by holding or executing the messages page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
2026-05-18 09:46:11 +08:00
const messages = ref < SystemMessageVO [] >([])
/** detailVisible is a reactive state variable used by the template and event handlers. */
const detailVisible = ref ( false )
2026-05-23 18:44:57 +08:00
/**
* Purpose: currentMessage supports NotificationsView.vue by holding or executing the current message page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
2026-05-18 09:46:11 +08:00
const currentMessage = ref < SystemMessageVO | null >( null )
/** unreadCount is a reactive state variable used by the template and event handlers. */
const unreadCount = computed (() => messages . value . filter ( item => item . readStatus !== 1 ). length )
2026-05-23 18:44:57 +08:00
/**
* Purpose: resolveSystemMessageActionPath supports NotificationsView.vue by holding or executing the resolve system message action path page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
2026-05-23 21:56:18 +08:00
const resolveSystemMessageActionPath = ( item : SystemMessageVO ) => buildSystemMessageActionPath ( item )
2026-05-23 15:16:40 +08:00
2026-05-23 18:44:57 +08:00
/**
* Purpose: markMessageReadIfNeeded supports NotificationsView.vue by holding or executing the mark message read if needed page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
2026-05-23 15:16:40 +08:00
const markMessageReadIfNeeded = async ( item : SystemMessageVO ) => {
if ( item . readStatus === 1 ) {
return
}
await userApi . markMessageRead ( item . id )
item . readStatus = 1
}
2026-05-18 09:46:11 +08:00
/** loadMessages is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const loadMessages = async () => {
loading . value = true
try {
messages . value = await userApi . listMyMessages ()
} catch ( error : any ) {
message . error ( error ? . message || '消息加载失败' )
} finally {
loading . value = false
}
}
/** openMessage is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
const openMessage = async ( item : SystemMessageVO ) => {
2026-05-23 15:16:40 +08:00
try {
await markMessageReadIfNeeded ( item )
} catch ( error : any ) {
message . error ( error ? . message || '标记已读失败' )
return
}
const actionPath = resolveSystemMessageActionPath ( item )
if ( actionPath ) {
await router . push ( actionPath )
return
}
2026-05-18 09:46:11 +08:00
currentMessage . value = item
detailVisible . value = true
}
onMounted ( loadMessages )
</ script >
< style scoped >
. notifications - page {
min - height : 100 vh ;
background : # fff7f8 ;
padding : 18 px ;
}
. page - header {
display : flex ;
gap : 14 px ;
align - items : center ;
margin - bottom : 18 px ;
}
. page - header h1 {
margin : 0 ;
font - size : 24 px ;
}
. page - header p {
margin : 4 px 0 0 ;
color : # 8 a7b83 ;
}
. back - btn {
width : 40 px ;
height : 40 px ;
border : 0 ;
border - radius : 20 px ;
background : # fff ;
font - size : 28 px ;
}
. message - list {
display : grid ;
gap : 10 px ;
}
. message - item {
display : flex ;
align - items : center ;
gap : 12 px ;
width : 100 % ;
padding : 16 px ;
border : 0 ;
border - radius : 8 px ;
background : # fff ;
text - align : left ;
}
. message - item . dot {
width : 8 px ;
height : 8 px ;
border - radius : 50 % ;
background : # d7c9cf ;
}
. message - item . unread . dot {
background : # ff5c8a ;
}
. message - main {
display : grid ;
gap : 4 px ;
}
. message - main em {
width : fit - content ;
padding : 2 px 8 px ;
border - radius : 999 px ;
background : # fff0f4 ;
color : # d74270 ;
font - size : 12 px ;
font - style : normal ;
}
. message - main small ,
. message - meta {
color : # 8 a7b83 ;
}
. message - content {
white - space : pre - wrap ;
line - height : 1.8 ;
}
. message - image {
display : block ;
width : 100 % ;
max - height : 420 px ;
margin - top : 14 px ;
object - fit : contain ;
border - radius : 8 px ;
background : # fff7f8 ;
}
</ style >