chatview优化

This commit is contained in:
runny committed 2026-05-14 17:52:21 +08:00
1 parent e2510c0565
commit bf5f735404
3 files changed
+155 -44

No files matched your search

+46 -17
View File
@@ -10,10 +10,10 @@
* - 支持从首页携带预设问题快速发起对话
* - 在回答结束后允许用户对本次回答进行评分与意见反馈
*/
import { ref, onMounted, onBeforeUnmount, computed } from 'vue';
import { ref, onMounted, onBeforeUnmount, computed, watch, nextTick } from 'vue';
import { useI18n } from 'vue-i18n';
import { useChat } from '../composables/useChat';
import { Send, Bot, Loader2, Plus, Copy, Edit2, RotateCw, Check, X, PanelLeftClose, PanelLeftOpen, Heart, Bell, Mail, Settings, ChevronDown, Sparkles, User, Database, Mic, LogOut, MessageSquareText, Globe, ExternalLink } from 'lucide-vue-next';
import { Send, Loader2, Plus, Copy, Edit2, RotateCw, Check, X, PanelLeftClose, PanelLeftOpen, Heart, Bell, Mail, Settings, ChevronDown, Sparkles, Database, Mic, LogOut, MessageSquareText, Globe, ExternalLink } from 'lucide-vue-next';
import { marked } from 'marked';
import { useRoute, useRouter } from 'vue-router';
import { config } from '../config';
@@ -32,6 +32,8 @@ const selectedKb = ref('企业知识库');
const showKbDropdown = ref(false);
const kbMenuRef = ref<HTMLElement | null>(null);
const kbOptions = ['企业知识库', 'ERP知识库'];
const chatScrollContainer = ref<HTMLElement | null>(null);
let scrollToBottomFrame: number | null = null;
marked.setOptions({
gfm: true,
@@ -672,6 +674,26 @@ const browserPanelStyle = computed(() => {
return { width: `${browserPanelWidth.value}px` };
});
const scrollChatToBottom = async () => {
await nextTick();
const container = chatScrollContainer.value;
if (!container) return;
container.scrollTo({
top: container.scrollHeight,
behavior: 'auto',
});
};
const scheduleScrollChatToBottom = () => {
if (scrollToBottomFrame !== null) {
window.cancelAnimationFrame(scrollToBottomFrame);
}
scrollToBottomFrame = window.requestAnimationFrame(() => {
scrollToBottomFrame = null;
void scrollChatToBottom();
});
};
const normalizeBrowserInput = (value: string) => {
const raw = (value || '').trim();
if (!raw) return '';
@@ -1039,6 +1061,7 @@ onMounted(async () => {
}
// Use config from config.ts instead of API
showChatRating.value = config.workspace?.showChatRating ?? true;
scheduleScrollChatToBottom();
});
onBeforeUnmount(() => {
@@ -1046,8 +1069,26 @@ onBeforeUnmount(() => {
document.removeEventListener('touchstart', handleDocumentPointerDown);
window.removeEventListener('resize', syncViewportWidth);
stopBrowserResize();
if (scrollToBottomFrame !== null) {
window.cancelAnimationFrame(scrollToBottomFrame);
scrollToBottomFrame = null;
}
});
watch(
() => ({
messageCount: messages.value.length,
lastMessageContent: messages.value[messages.value.length - 1]?.content || '',
lastMessageRole: messages.value[messages.value.length - 1]?.role || '',
isThinking: isThinking.value,
conversationId: conversationId.value || '',
}),
() => {
scheduleScrollChatToBottom();
},
{ deep: false },
);
/**
* 反馈相关状态
* showChatRating:是否启用评分功能(后续可接入配置)
@@ -1220,6 +1261,7 @@ const showChatRating = ref(false);
<div class="flex min-h-0 flex-1">
<div class="flex min-h-0 flex-1 flex-col">
<main
ref="chatScrollContainer"
class="flex-1 p-3 md:p-8"
:class="messages.length === 0 ? 'overflow-hidden md:overflow-y-auto' : 'space-y-2 overflow-y-auto scrollbar-thin md:space-y-6'"
>
@@ -1233,14 +1275,9 @@ const showChatRating = ref(false);
<template v-for="(msg, index) in messages" :key="index">
<div
v-if="shouldRenderMessage(msg)"
class="group mx-auto flex max-w-4xl gap-4 animate-slide-up mb-6"
class="group mx-auto flex max-w-4xl animate-slide-up mb-6"
:class="msg.role === 'user' ? 'justify-end' : 'justify-start'"
>
<!-- AI Avatar -->
<div v-if="msg.role === 'assistant'" class="mt-1 flex h-8 w-8 shrink-0 items-center justify-center rounded-full bg-gradient-to-br from-brand-500 via-brand-600 to-sky-600 text-white shadow-sm shadow-brand-500/20">
<Sparkles class="h-4 w-4" />
</div>
<div
class="flex flex-col"
:class="msg.role === 'user' ? 'max-w-[88%] md:max-w-[74%] items-end' : 'w-full'"
@@ -1530,18 +1567,10 @@ const showChatRating = ref(false);
</div>
</div>
<!-- User Avatar -->
<div v-if="msg.role === 'user'" class="mt-1 flex h-8 w-8 shrink-0 items-center justify-center rounded-full bg-brand-600 text-white shadow-sm shadow-brand-500/20">
<User class="h-4 w-4" />
</div>
</div>
</template>
<div v-if="shouldShowThinkingBubble" class="mx-auto flex max-w-5xl gap-4">
<div class="mt-1 flex h-10 w-10 shrink-0 items-center justify-center rounded-xl bg-gradient-to-br from-brand-500 via-brand-600 to-sky-600 shadow-md shadow-brand-500/25">
<Bot class="h-5 w-5 text-white" />
</div>
<div v-if="shouldShowThinkingBubble" class="mx-auto flex max-w-5xl">
<div class="flex items-center gap-3 rounded-2xl rounded-tl-md border border-slate-200 bg-white/95 px-5 py-3 shadow-md dark:border-slate-700 dark:bg-slate-900/85">
<div class="flex gap-1">
<span class="h-2 w-2 animate-bounce rounded-full bg-brand-400"></span>