feat: 历史消息的加载

This commit is contained in:
zoujing
2026-02-03 16:34:16 +08:00
parent aff9233ce2
commit ff5355855f
5 changed files with 158 additions and 114 deletions

View File

@@ -88,12 +88,13 @@ import { Session } from '../../utils/storage';
import userAvatar from '@assets/images/login/user_icon.png';
import aiAvatar from '@assets/images/login/blue_logo.png';
import { createConversation } from '../../api/ConversationApi';
import { createConversation, conversationMessageList } from '../../api/ConversationApi';
import { ElMessage, ElLoading } from 'element-plus'
// 支持外部通过 prop 控制是否为引导页
const props = defineProps({
guide: { type: Boolean, default: true }
guide: { type: Boolean, default: true },
conversationId: { type: String, default: '' }
});
const emit = defineEmits(['update:guide']);
@@ -127,7 +128,16 @@ const isSendingMessage = ref(false);
/// agentId 首页接口中获取 1953462165250859010
const agentId = ref("1953462165250859010");
/// 会话ID 历史数据接口中获取
const conversationId = ref("");
const conversationId = ref(props.conversationId);
// 监听 conversationId prop 变化,只有当有值时(选择历史消息)才请求消息列表
watch(() => props.conversationId, (newId) => {
if (newId) {
conversationId.value = newId;
loadConversationMessages(newId);
}
});
// 会话进行中标志
const isSessionActive = ref(false);
/// 指令通用消息类型
@@ -285,9 +295,35 @@ const checkToken = async () => {
};
// 调用接口创建新会话
const createConversationRequest = async () => {
const createConversationRequest = async (): Promise<string | null> => {
const res = await createConversation();
conversationId.value = res.conversationId;
if (res && res.conversationId) {
conversationId.value = res.conversationId;
console.log("创建新会话ID:", conversationId.value);
return res.conversationId;
} else {
console.log("创建会话失败,接口返回异常");
return null;
}
};
// 加载历史会话消息
const loadConversationMessages = async (convId: string) => {
try {
const res = await conversationMessageList({ conversationId: convId, pageSize: 50, pageNum: 1 });
// 将消息转换为 ChatMessage 格式
chatMsgList.value = res.records.map((msg: any) => ({
messageId: msg.messageId,
messageRole: msg.messageSenderRole === 'user' ? MessageRole.ME : MessageRole.AI,
messageContent: msg.messageContent,
finished: true, // 历史消息已完成
}));
console.log("加载历史消息:", chatMsgList.value);
// 加载历史消息后滚动到底部
nextTick(() => scrollToBottom());
} catch (error) {
console.error("加载历史消息失败:", error);
}
};
/// =============对话↓================