fix: 🐛 导航栏通知优化重构

This commit is contained in:
ray
2025-02-13 21:38:32 +08:00
parent 4b042f2a0b
commit 52157082f5

View File

@@ -24,7 +24,7 @@
size="small" size="small"
class="w200px cursor-pointer !ml-2 !flex-1" class="w200px cursor-pointer !ml-2 !flex-1"
truncated truncated
@click="handleReadNotice(item.id)" @click="readNotice(item.id)"
> >
{{ item.title }} {{ item.title }}
</el-text> </el-text>
@@ -42,7 +42,7 @@
</template> </template>
<el-divider /> <el-divider />
<div class="flex-x-between"> <div class="flex-x-between">
<el-link type="primary" :underline="false" @click="handleViewMore"> <el-link type="primary" :underline="false" @click="viewMoreNotice">
<span class="text-xs">查看更多</span> <span class="text-xs">查看更多</span>
<el-icon class="text-xs"> <el-icon class="text-xs">
<ArrowRight /> <ArrowRight />
@@ -70,7 +70,7 @@
<el-link <el-link
type="primary" type="primary"
class="w200px cursor-pointer !ml-2 !flex-1" class="w200px cursor-pointer !ml-2 !flex-1"
@click="handleReadNotice(item.id)" @click="readNotice(item.id)"
> >
{{ item.title }} {{ item.title }}
</el-link> </el-link>
@@ -92,7 +92,7 @@
v-if="tasks.length > 5" v-if="tasks.length > 5"
type="primary" type="primary"
:underline="false" :underline="false"
@click="handleViewMore" @click="viewMoreNotice"
> >
<span class="text-xs">查看更多</span> <span class="text-xs">查看更多</span>
<el-icon class="text-xs"> <el-icon class="text-xs">
@@ -118,7 +118,7 @@
<el-link <el-link
type="primary" type="primary"
class="w200px cursor-pointer !ml-2 !flex-1" class="w200px cursor-pointer !ml-2 !flex-1"
@click="handleReadNotice(item.id)" @click="readNotice(item.id)"
> >
{{ item.title }} {{ item.title }}
</el-link> </el-link>
@@ -139,7 +139,7 @@
v-if="tasks.length > 5" v-if="tasks.length > 5"
type="primary" type="primary"
:underline="false" :underline="false"
@click="handleViewMore" @click="viewMoreNotice"
> >
<span class="text-xs">查看更多</span> <span class="text-xs">查看更多</span>
<el-icon class="text-xs"> <el-icon class="text-xs">
@@ -176,17 +176,16 @@ const tasks = ref<any[]>([]);
const noticeDetailRef = ref(); const noticeDetailRef = ref();
import { useStomp } from "@/hooks/useStomp"; import { useStomp } from "@/hooks/useStomp";
const { subscribe, unsubscribe, isConnected } = useStomp({
const { subscribe, disconnect } = useStomp({
debug: true, debug: true,
}); });
/** watch(
* 订阅通知消息 () => isConnected.value,
*/ (connected) => {
function subscribeNotice() { if (connected) {
subscribe("/user/queue/message", (message) => { subscribe("/user/queue/message", (message) => {
console.log("收到消息:", message); console.log("收到通知消息:", message);
const data = JSON.parse(message.body); const data = JSON.parse(message.body);
const id = data.id; const id = data.id;
if (!notices.value.some((notice) => notice.id == id)) { if (!notices.value.some((notice) => notice.id == id)) {
@@ -206,6 +205,8 @@ function subscribeNotice() {
} }
}); });
} }
}
);
/** /**
* 获取我的通知公告 * 获取我的通知公告
@@ -217,7 +218,7 @@ function featchMyNotice() {
} }
// 阅读通知公告 // 阅读通知公告
function handleReadNotice(id: string) { function readNotice(id: string) {
noticeDetailRef.value.openNotice(id); noticeDetailRef.value.openNotice(id);
const index = notices.value.findIndex((notice) => notice.id === id); const index = notices.value.findIndex((notice) => notice.id === id);
if (index >= 0) { if (index >= 0) {
@@ -226,7 +227,7 @@ function handleReadNotice(id: string) {
} }
// 查看更多 // 查看更多
function handleViewMore() { function viewMoreNotice() {
router.push({ path: "/myNotice" }); router.push({ path: "/myNotice" });
} }
@@ -237,14 +238,12 @@ function markAllAsRead() {
}); });
} }
// 获取未读消息列表并连接 WebSocket
onMounted(() => { onMounted(() => {
featchMyNotice(); featchMyNotice();
subscribeNotice();
}); });
onBeforeUnmount(() => { onBeforeUnmount(() => {
disconnect(); unsubscribe("/user/queue/message");
}); });
</script> </script>