<template>
|
<div class="chat-page">
|
<Sidebar
|
v-show="sidebarVisible"
|
ref="sidebarRef"
|
@select="handleSelectChat"
|
@created="handleCreated"
|
@deleted="handleDeleted"
|
@open-files="handleOpenFiles"
|
@open-admin-knowledge="handleOpenAdminKnowledge"
|
/>
|
<ChatView :chat-id="chatId" ref="chatViewRef" @toggle-sidebar="sidebarVisible = !sidebarVisible" />
|
</div>
|
</template>
|
|
<script setup>
|
import { onMounted, ref, watch } from 'vue'
|
import { useRoute, useRouter } from 'vue-router'
|
import { useMessage } from 'naive-ui'
|
import Sidebar from '@/components/Sidebar.vue'
|
import ChatView from '@/components/ChatView.vue'
|
import { useAuthStore } from '@/stores/authStore'
|
import { useChatStore } from '@/stores/chatStore'
|
|
const route = useRoute()
|
const router = useRouter()
|
const message = useMessage()
|
const auth = useAuthStore()
|
const chatStore = useChatStore()
|
|
const sidebarRef = ref(null)
|
const chatViewRef = ref(null)
|
const sidebarVisible = ref(true)
|
|
// 会话 id 来自路由参数,字符串需转成数字
|
const chatId = ref(route.params.chatId ? Number(route.params.chatId) : null)
|
|
onMounted(async () => {
|
// 刷新页面后校验 token 是否仍然有效(失效会由拦截器登出)
|
await auth.fetchMe()
|
await sidebarRef.value?.refresh()
|
|
// 没有指定会话时,自动选中最近的一个,避免用户看到空白页
|
if (!chatId.value && chatStore.chats.length > 0) {
|
router.replace(`/c/${chatStore.chats[0].id}`)
|
}
|
})
|
|
// 路由与会话状态双向同步
|
watch(
|
() => route.params.chatId,
|
(value) => {
|
const next = value ? Number(value) : null
|
if (next !== chatId.value) {
|
chatId.value = next
|
}
|
}
|
)
|
|
function handleSelectChat(id) {
|
if (id === chatId.value) return
|
router.push(`/c/${id}`)
|
}
|
|
function handleCreated(id) {
|
router.push(`/c/${id}`)
|
}
|
|
function handleDeleted(id) {
|
// 删掉的正好是当前会话,或者用户登出:回到空白页
|
if (id === null || id === chatId.value) {
|
router.push('/c')
|
}
|
}
|
|
function handleOpenFiles() {
|
router.push('/files')
|
}
|
|
function handleOpenAdminKnowledge() {
|
router.push('/admin/knowledge')
|
}
|
|
// 兜底:路由指向不存在的会话时提示并清理
|
watch(chatId, async (id) => {
|
if (!id) return
|
const exists = chatStore.chats.some((chat) => chat.id === id)
|
if (!exists && chatStore.chats.length > 0) {
|
message.warning('该对话不存在或已被删除')
|
router.replace('/c')
|
}
|
})
|
</script>
|
|
<style scoped>
|
.chat-page {
|
display: flex;
|
height: 100vh;
|
overflow: hidden;
|
}
|
</style>
|