Defer layout realtime notification startup
This commit is contained in:
@@ -1,7 +1,19 @@
|
||||
<script lang="ts" setup>
|
||||
import type { ChatMessage } from '#/api/core/chat';
|
||||
import type {
|
||||
AnnouncementItem,
|
||||
NotificationItem,
|
||||
} from '#/composables/useNotification';
|
||||
import type { WatchStopHandle } from 'vue';
|
||||
|
||||
import { computed, onMounted, onUnmounted, ref, watch } from 'vue';
|
||||
import {
|
||||
computed,
|
||||
defineAsyncComponent,
|
||||
onMounted,
|
||||
onUnmounted,
|
||||
ref,
|
||||
watch,
|
||||
} from 'vue';
|
||||
import { useRouter } from 'vue-router';
|
||||
|
||||
import { AuthenticationLoginExpiredModal } from '@vben/common-ui';
|
||||
@@ -11,29 +23,26 @@ import { $t } from '@vben/locales';
|
||||
import { preferences } from '@vben/preferences';
|
||||
import { useAccessStore, useUserStore } from '@vben/stores';
|
||||
|
||||
import NotificationPopup from '#/components/notification/NotificationPopup.vue';
|
||||
import { getFileUrl } from '#/composables/useFileUrl';
|
||||
import { useNotification } from '#/composables/useNotification';
|
||||
import { useAuthStore } from '#/store';
|
||||
import LoginForm from '#/views/_core/authentication/login.vue';
|
||||
import { useChat } from '#/views/_core/chat/composables/useChat';
|
||||
|
||||
// 使用消息通知 composable
|
||||
const {
|
||||
notifications,
|
||||
messageUnreadCount,
|
||||
announcements,
|
||||
announcementUnreadCount,
|
||||
activeTab,
|
||||
showDot,
|
||||
markAsRead,
|
||||
markAnnouncementAsRead,
|
||||
markAllAsRead,
|
||||
clearReadMessages,
|
||||
viewAllMessages,
|
||||
init: initNotification,
|
||||
cleanup: cleanupNotification,
|
||||
} = useNotification();
|
||||
const LoginForm = defineAsyncComponent(
|
||||
() => import('#/views/_core/authentication/login.vue'),
|
||||
);
|
||||
const NotificationPopup = defineAsyncComponent(
|
||||
() => import('#/components/notification/NotificationPopup.vue'),
|
||||
);
|
||||
|
||||
const notifications = ref<NotificationItem[]>([]);
|
||||
const messageUnreadCount = ref(0);
|
||||
const announcements = ref<AnnouncementItem[]>([]);
|
||||
const announcementUnreadCount = ref(0);
|
||||
const activeTab = ref<'announcement' | 'chat' | 'message'>('message');
|
||||
const showDot = computed(
|
||||
() => messageUnreadCount.value + announcementUnreadCount.value > 0,
|
||||
);
|
||||
const unreadChatMessages = ref<ChatMessage[]>([]);
|
||||
const chatTotalUnread = ref(0);
|
||||
|
||||
const userStore = useUserStore();
|
||||
const authStore = useAuthStore();
|
||||
@@ -42,28 +51,93 @@ const { destroyWatermark, updateWatermark } = useWatermark();
|
||||
|
||||
// 初始化消息通知
|
||||
const router = useRouter();
|
||||
const {
|
||||
connectChat,
|
||||
disconnectChat,
|
||||
loadConversations,
|
||||
loadUnreadChatMessages,
|
||||
unreadChatMessages,
|
||||
totalUnread: chatTotalUnread,
|
||||
} = useChat();
|
||||
let notificationApi: any = null;
|
||||
let chatApi: any = null;
|
||||
let layoutActive = false;
|
||||
let stopRealtimeWatches: WatchStopHandle[] = [];
|
||||
|
||||
onMounted(() => {
|
||||
initNotification();
|
||||
// 全局初始化聊天 WebSocket,确保任何页面都能收到消息通知
|
||||
connectChat();
|
||||
loadConversations();
|
||||
loadUnreadChatMessages();
|
||||
layoutActive = true;
|
||||
initRealtimeFeatures();
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
cleanupNotification();
|
||||
disconnectChat();
|
||||
layoutActive = false;
|
||||
stopRealtimeWatches.forEach((stop) => stop());
|
||||
stopRealtimeWatches = [];
|
||||
notificationApi?.cleanup();
|
||||
chatApi?.disconnectChat();
|
||||
});
|
||||
|
||||
async function initRealtimeFeatures() {
|
||||
const [notificationModule, chatModule] = await Promise.all([
|
||||
import('#/composables/useNotification'),
|
||||
import('#/views/_core/chat/composables/useChat'),
|
||||
]);
|
||||
|
||||
if (!layoutActive) return;
|
||||
|
||||
notificationApi = notificationModule.useNotification();
|
||||
chatApi = chatModule.useChat();
|
||||
|
||||
stopRealtimeWatches = [
|
||||
watch(
|
||||
notificationApi.notifications,
|
||||
(value) => {
|
||||
notifications.value = value;
|
||||
},
|
||||
{ immediate: true },
|
||||
),
|
||||
watch(
|
||||
notificationApi.messageUnreadCount,
|
||||
(value) => {
|
||||
messageUnreadCount.value = value;
|
||||
},
|
||||
{ immediate: true },
|
||||
),
|
||||
watch(
|
||||
notificationApi.announcements,
|
||||
(value) => {
|
||||
announcements.value = value;
|
||||
},
|
||||
{ immediate: true },
|
||||
),
|
||||
watch(
|
||||
notificationApi.announcementUnreadCount,
|
||||
(value) => {
|
||||
announcementUnreadCount.value = value;
|
||||
},
|
||||
{ immediate: true },
|
||||
),
|
||||
watch(
|
||||
notificationApi.activeTab,
|
||||
(value) => {
|
||||
activeTab.value = value;
|
||||
},
|
||||
{ immediate: true },
|
||||
),
|
||||
watch(
|
||||
chatApi.unreadChatMessages,
|
||||
(value) => {
|
||||
unreadChatMessages.value = value;
|
||||
},
|
||||
{ immediate: true },
|
||||
),
|
||||
watch(
|
||||
chatApi.totalUnread,
|
||||
(value) => {
|
||||
chatTotalUnread.value = value;
|
||||
},
|
||||
{ immediate: true },
|
||||
),
|
||||
];
|
||||
|
||||
notificationApi.init();
|
||||
chatApi.connectChat();
|
||||
chatApi.loadConversations();
|
||||
chatApi.loadUnreadChatMessages();
|
||||
}
|
||||
|
||||
const menus = computed(() => [
|
||||
// {
|
||||
// handler: () => {
|
||||
@@ -120,27 +194,30 @@ async function handleLogout() {
|
||||
}
|
||||
|
||||
function handleNoticeClear() {
|
||||
clearReadMessages();
|
||||
notificationApi?.clearReadMessages();
|
||||
}
|
||||
|
||||
function handleMakeAll() {
|
||||
markAllAsRead();
|
||||
notificationApi?.markAllAsRead();
|
||||
}
|
||||
|
||||
function handleNoticeRead(item: any) {
|
||||
markAsRead(item);
|
||||
notificationApi?.markAsRead(item);
|
||||
}
|
||||
|
||||
function handleAnnouncementRead(item: any) {
|
||||
markAnnouncementAsRead(item);
|
||||
notificationApi?.markAnnouncementAsRead(item);
|
||||
}
|
||||
|
||||
function handleViewAll() {
|
||||
viewAllMessages();
|
||||
notificationApi?.viewAllMessages();
|
||||
}
|
||||
|
||||
function handleTabChange(tab: 'announcement' | 'chat' | 'message') {
|
||||
activeTab.value = tab;
|
||||
if (notificationApi) {
|
||||
notificationApi.activeTab.value = tab;
|
||||
}
|
||||
}
|
||||
|
||||
function handleChatClick(msg: ChatMessage) {
|
||||
|
||||
Reference in New Issue
Block a user