diff --git a/pages/chat/ChatMainList.vue b/pages/chat/ChatMainList.vue index bbbf814..028d055 100644 --- a/pages/chat/ChatMainList.vue +++ b/pages/chat/ChatMainList.vue @@ -85,15 +85,24 @@ import OneFeelMK002 from '../module/OneFeelMK002.vue'; import { agentChatStream } from '../../request/api/AgentChatStream'; import { mainPageData } from '../../request/api/MainPageData'; + import { conversationMsgList } from '../../request/api/ConversationMsgList'; + import { recentConversation } from '../../request/api/RecentConversation'; - // 导航栏相关 + /// 导航栏相关 const statusBarHeight = ref(20); const timer = ref(null) - const holdKeyboard = ref(false) // focus时,点击页面的时候不收起键盘 - const holdKeyboardFlag = ref(true) // 是否在键盘弹出,点击界面时关闭键盘 + /// focus时,点击页面的时候不收起键盘 + const holdKeyboard = ref(false) + /// 是否在键盘弹出,点击界面时关闭键盘 + const holdKeyboardFlag = ref(true) + ///(控制滚动位置) + const scrollTop = ref(99999); + + /// 会话列表 const chatMsgList = ref([]) + /// 输入口的输入消息 const inputMessage = ref('') /// 从个渠道获取如二维,没有的时候就返回首页的数据 @@ -101,11 +110,12 @@ /// agentId 首页接口中获取 const agentId = ref('1') /// 会话ID 历史数据接口中获取 - const conversationId = ref('1931957498711957505') + const conversationId = ref('') + /// 首页的数据 const mainPageDataModel = ref({}) - //(控制滚动位置) - const scrollTop = ref(99999); + // 会话进行中标志 + let isSessionActive = false; // 打开抽屉 @@ -135,6 +145,7 @@ // #endif } + /// 滚动到底部 const scrollToBottom = () => { nextTick(() => { nextTick(() => { @@ -142,19 +153,35 @@ }); }); } + /// 延迟在滚到底 + const setTimeoutScrollToBottom = () => { + setTimeout(() => { + scrollToBottom() + }, 100) + } /// 发送普通消息 const handleReply = (text) => { sendMessage(text) - setTimeout(() => { - scrollToBottom() - }, 100) + setTimeoutScrollToBottom() }; /// 是发送指令 const handleReplyInstruct = (text) => { sendMessage(text, true) - scrollToBottom() + setTimeoutScrollToBottom() + } + + /// 输入区的发送消息事件 + const sendMessageAction = (inputText) => { + console.log("输入消息:", inputText) + if (!inputText.trim()) return; + handleNoHideKeyboard() + sendMessage(inputText) + if(!isSessionActive) { + inputMessage.value = '' + } + setTimeoutScrollToBottom() } onLoad(() => { @@ -165,10 +192,26 @@ }); }); - onMounted(() => { + onMounted( async() => { getMainPageData() + await loadRecentConversation() + loadConversationMsgList() }) + /// 获取最近一次的会话id + const loadRecentConversation = async() => { + const res = await recentConversation() + if(res.code === 0) { + conversationId.value = res.data.conversationId + } + } + + /// 加载历史消息的数据 + let historyCurrentPageNum = 1 + const loadConversationMsgList = async() => { + const args = { pageNum: historyCurrentPageNum++, pageSize : 10, conversationId: conversationId.value } + const res = await conversationMsgList(args) + } /// 获取首页数据 const getMainPageData = async() => { @@ -181,7 +224,7 @@ } } - /// 初始化数据 + /// 初始化数据 首次数据加载的时候 const initData = () => { const msg = { msgId: `msg_${0}`, @@ -192,17 +235,16 @@ } - /// 输入区的发送消息事件 - const sendMessageAction = (inputText) => { - console.log("输入消息:", inputText) - if (!inputText.trim()) return; - handleNoHideKeyboard() - sendMessage(inputText) - inputMessage.value = '' - scrollToBottom() - } - + /// 发送消息的参数拼接 const sendMessage = (message, isInstruct = false) => { + if (isSessionActive) { + uni.showToast({ + title: '请等待当前回复完成', + icon: 'none' + }); + return; + } + isSessionActive = true; const newMsg = { msgId: `msg_${chatMsgList.value.length}`, msgType: MessageRole.ME, @@ -217,7 +259,7 @@ console.log("发送的新消息:",JSON.stringify(newMsg)) } - + /// 打字机效果实现的变量 let loadingTimer = null; let typeWriterTimer = null; let aiMsgBuffer = ''; // 全局缓冲区 @@ -260,9 +302,19 @@ typeWriterTimer = null; } - // 2. 流式接收内容 + // 流式接收内容 agentChatStream(args, (chunk) => { console.log('分段内容:', chunk) + if (chunk.error) { + chatMsgList.value[aiMsgIndex].msg = '请求错误,请重试'; + clearInterval(finishInterval); + isTyping = false; + typeWriterTimer = null; + isSessionActive = false; // 出错也允许再次发送 + console.error('流式错误:', chunk.message, chunk.detail); + return; + } + if (chunk && chunk.content) { // 收到内容,停止动画 if (loadingTimer) { @@ -285,10 +337,14 @@ if (aiMsgBuffer.length === 0) { clearInterval(finishInterval); isTyping = false; + isSessionActive = false; // 会话结束,允许再次发送 scrollToBottom(); } }, 50); } + }).catch(e => { + isSessionActive = false; // 出错也允许再次发送 + console.log('error:', e) }); // 打字机函数 diff --git a/pages/chat/ChatTopWelcome.vue b/pages/chat/ChatTopWelcome.vue index fc5fc05..ab32f11 100644 --- a/pages/chat/ChatTopWelcome.vue +++ b/pages/chat/ChatTopWelcome.vue @@ -4,7 +4,7 @@ - {{ currentDate }} 多云 -3~6℃ + {{ currentDate }} 多云 -3~6℃ gg diff --git a/request/api/AgentChatStream.js b/request/api/AgentChatStream.js index f8e4285..ef24b3e 100644 --- a/request/api/AgentChatStream.js +++ b/request/api/AgentChatStream.js @@ -30,8 +30,19 @@ function agentChatStream(params, onChunk) { resolve(res.data); }, fail(err) { + console.log("====> ", JSON.stringify(err)) reject(err); - } + }, + complete(res) { + if(res.statusCode === 500) { + console.log("====> ", JSON.stringify(res)) + + if (onChunk) { + onChunk({ error: true, message: '服务器错误', detail: res }); + } + reject(res); + } + } }); requestTask.onHeadersReceived(res => { diff --git a/request/api/ConversationMsgList.js b/request/api/ConversationMsgList.js new file mode 100644 index 0000000..8748ef4 --- /dev/null +++ b/request/api/ConversationMsgList.js @@ -0,0 +1,7 @@ +import request from "../base/request"; + +function conversationMsgList(args) { + return request.post('/hotelBiz/chat/conversationMessageList', args); +} + +export { conversationMsgList } diff --git a/request/api/RecentConversation.js b/request/api/RecentConversation.js new file mode 100644 index 0000000..bc45937 --- /dev/null +++ b/request/api/RecentConversation.js @@ -0,0 +1,7 @@ +import request from "../base/request"; + +function recentConversation() { + return request.get('/hotelBiz/chat/recentConversation'); +} + +export{ recentConversation } \ No newline at end of file diff --git a/uni_modules/zero-markdown-view/components/zero-markdown-view/zero-markdown-view.vue b/uni_modules/zero-markdown-view/components/zero-markdown-view/zero-markdown-view.vue index 7cceffa..0e03521 100644 --- a/uni_modules/zero-markdown-view/components/zero-markdown-view/zero-markdown-view.vue +++ b/uni_modules/zero-markdown-view/components/zero-markdown-view/zero-markdown-view.vue @@ -306,7 +306,7 @@ export default { \ No newline at end of file