feat(im): 添加 IM 聊天模块的全屏容器组件,并添加相关 review 注释

im
YunaiV 2026-04-26 22:02:02 +08:00
parent 9570f25cdc
commit 1e02a40ec4
1 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,97 @@
<!-- TODO @AI文件名是不是应该小写 -->
<template>
<!--
IM 外层容器聊天模块的全屏沉浸式壳
- 左侧 ToolBar头像 + Tab消息/好友/群聊+ 底部设置
- 右侧 <router-view>按路由渲染 MessagePage / FriendPage / GroupPage
- 挂载全局弹层UserInfoCard / ContextMenu / ImageViewer
-->
<div class="flex w-full h-full overflow-hidden">
<ToolBar />
<!--
keep-alive 缓存子页面
- Tab 不重建组件ChatPanel 滚动位置输入框草稿等 UI 状态不丢
- Vue 3 keep-alive 不能直接包 <router-view>会有警告必须走 v-slot Component
-->
<router-view v-slot="{ Component }">
<keep-alive>
<component :is="Component" />
</keep-alive>
</router-view>
<!-- 全局弹层 useImUiStore 统一调度 -->
<!-- TODO @AI是不是没必要大图预览改为 <el-image :preview-src-list> 在调用方就地承接不再全局挂 -->
<UserInfoCard />
<ContextMenu />
</div>
</template>
<script lang="ts" setup>
import { onMounted, onUnmounted, watch } from 'vue'
import { useConversationStore } from './store/conversationStore'
import { useImWebSocketStore } from './store/websocketStore'
import { useFriendStore } from './store/friendStore'
import { useGroupStore } from './store/groupStore'
import { useMessagePuller } from './composables/useMessagePuller'
import { useMessageSender } from './composables/useMessageSender'
import { ImConversationType } from '../utils/constants'
import ToolBar from './components/ToolBar.vue'
import UserInfoCard from './components/UserInfoCard.vue'
import ContextMenu from './components/ContextMenu.vue'
defineOptions({ name: 'ImIndex' })
const conversationStore = useConversationStore()
const wsStore = useImWebSocketStore()
const friendStore = useFriendStore()
const groupStore = useGroupStore()
const { pullOnce } = useMessagePuller()
const { readActive, syncPrivateReadStatus } = useMessageSender()
// TODO @AI
onMounted(async () => {
// 1. IndexedDB await
await conversationStore.loadConversations()
// 2. WebSocket Tab
wsStore.connect()
// 3. / awaitpullOnce friendStore / groupStore senderNickName name/avatar
await Promise.all([
friendStore.loadFriends().catch((e) => console.warn('[IM] 预拉好友失败', e)),
groupStore.loadGroups().catch((e) => console.warn('[IM] 预拉群列表失败', e))
])
// 4. 线 + 使 minId
await pullOnce()
// 5. Tab
const sorted = conversationStore.getSortedConversations
if (sorted.length > 0 && !conversationStore.activeConversation) {
conversationStore.setActiveConversation(sorted[0])
}
})
// TODO @AI
onUnmounted(() => {
wsStore.disconnect()
})
/**
* 会话切换时自动标记为已读 + 私聊下拉对方已读位置
* - 立刻清零本地未读
* - 同步后端已读状态服务端会广播 READ/RECEIPT 事件通知其它端与对方
* - 私聊额外补一次对方已读到哪条弥补离线 / 多端漏掉的 RECEIPT 推送
*/
watch(
() => conversationStore.activeConversation?.targetId,
async (targetId) => {
if (!targetId) {
return
}
// TODO @AI
await readActive()
// TODO @AI
if (conversationStore.activeConversation?.type === ImConversationType.PRIVATE) {
void syncPrivateReadStatus(targetId)
}
}
)
</script>