From c56ba92667b2b5dded272fb60ad0c2af4983cd43 Mon Sep 17 00:00:00 2001 From: wujingjing <gersonwu@qq.com> Date: 星期三, 05 三月 2025 17:09:55 +0800 Subject: [PATCH] 修改意见 --- src/components/chat/hooks/useScrollLoad.ts | 149 ++++++------------------------------------------- 1 files changed, 19 insertions(+), 130 deletions(-) diff --git a/src/components/chat/hooks/useScrollLoad.ts b/src/components/chat/hooks/useScrollLoad.ts index c374ca6..656f211 100644 --- a/src/components/chat/hooks/useScrollLoad.ts +++ b/src/components/chat/hooks/useScrollLoad.ts @@ -1,154 +1,42 @@ import moment from 'moment'; -import { Ref, ShallowRef, computed, nextTick, onBeforeUnmount, ref, unref } from 'vue'; +import type { Ref, ShallowRef } from 'vue'; +import { nextTick, onBeforeUnmount, ref, unref } from 'vue'; import { LOAD_CHAT_LIMIT } from '../constants'; -import { AnswerType, ChatContent, ChatMessage, RoleEnum, StepEnum, StepItem } from '../model/types'; -import { GetHistoryAnswer, QueryHistoryDetail, getShareChatJsonByPost } from '/@/api/ai/chat'; -import router from '/@/router'; +import type { ChatMessage } from '../model/types'; +import { useLoadData } from './useLoadData'; +import { QueryHistoryDetail } from '/@/api/ai/chat'; type UseScrollLoadOption = { container: ShallowRef<HTMLDivElement>; historyGroupId: string | Ref<string>; messageList: Ref<ChatMessage[]>; - parseAnswerContent: (res: any) => ChatContent; - isSharePage: Ref<boolean>; + loadReplyData: (res: any) => Promise<ChatMessage[]>; }; -export const convertProcessItem = (processItem: any) => { - switch (processItem.mode) { - case 'begin': - break; - case 'end': - break; - } - return { - status: StepEnum.Success, - title: processItem.value, - }; -}; -export const convertProcessToStep = (process: any[]) => { - const stepList = (process ?? []).map<StepItem>((item) => { - return convertProcessItem(item); - }); - return stepList; -}; /** * 婊氬姩鍔犺浇鏁版嵁 * @returns */ export const useScrollLoad = (option: UseScrollLoadOption) => { - const { container, historyGroupId, messageList, parseAnswerContent, isSharePage } = option; + const { container, historyGroupId, messageList, loadReplyData } = option; const moreIsLoading = ref(false); /** @description 涓嬫闇�瑕佸姞杞界殑鐢ㄦ埛缁撴潫绱㈠紩锛堝�掔潃鏁帮級 */ - const nextUserMsgEndIndex = ref(0); + let nextUserMsgEndIndex = 0; // 鏄惁娌℃湁鏇村鏁版嵁浜� let isNoMore = false; - const getAnswerById = async (historyId: string) => { - return await GetHistoryAnswer({ - history_id: historyId, - }); - }; - const formatShowTimeYear = computed(() => { - return (str) => { - return moment(str).format('MM鏈圖D鏃� HH:mm:ss'); - }; - }); - /** - * 鑾峰彇鐢ㄦ埛鍥炲鏁版嵁锛屽苟鎻掑叆鍒板璇濆綋涓幓 - */ - const loadReplyData = async (userMsg: any[]) => { - const userItemIdMap = new Map(); - // 鐢ㄦ埛娑堟伅 - const tmpMessageList: ChatMessage[] = userMsg.map((item) => { - return { - historyId: item.history_id, - role: RoleEnum.user, - content: { - type: AnswerType.Text, - values: item.question, - }, - isChecked: false, - } as ChatMessage; - }); - const resList = await Promise.all( - (userMsg ?? []).map((item) => { - userItemIdMap.set(item.history_id, item); - return getAnswerById(item.history_id); - }) - ); - let i = 0; - resList.map((item, index) => { - const insertIndex = index + 1 + i; - const currentUserMsg = tmpMessageList[insertIndex - 1]; - currentUserMsg.content.values = item?.answer?.question ?? currentUserMsg.content.values; - - const mapUser = userItemIdMap.get(item.answer?.history_id); - - const answerTime = formatShowTimeYear.value(mapUser?.create_time); - tmpMessageList.splice( - insertIndex, - 0, - item.answer === null - ? null - : { - historyId: item.answer?.history_id, - role: RoleEnum.assistant, - content: parseAnswerContent(item.answer), - state: item.answer_state, - sectionAId: mapUser?.section_a_id, - createTime: answerTime, - stepList: convertProcessToStep(item?.answer?.exec_process), - stepIsShow: false, - conclusion: item?.answer?.conclusion ?? [], - isChecked: false, - } - ); - i++; - }); - - messageList.value.unshift(...tmpMessageList); - }; /** - * 鍔犺浇鍒嗕韩鏁版嵁 + * 鏇存柊鍔犺浇绱㈠紩 */ - const loadShareData = async () => { - const res = await getShareChatJsonByPost({ - share_id: router.currentRoute.value.query.id as string, - }); - - const msgValue = res?.values; - if (!msgValue) return undefined; - const userMsg: ChatMessage = { - historyId: msgValue.history_id, - role: RoleEnum.user, - content: { - type: AnswerType.Text, - values: msgValue.question, - }, - isChecked: false, - }; - - const assistantMsg: ChatMessage = { - historyId: msgValue.history_id, - role: RoleEnum.assistant, - content: parseAnswerContent(msgValue), - stepList: convertProcessToStep(msgValue.exec_process), - stepIsShow: false, - conclusion: msgValue.conclusion ?? [], - isChecked: false, - }; - messageList.value = [userMsg, assistantMsg]; + const updateLoadIndex = (addCount = 1) => { + nextUserMsgEndIndex += addCount; }; /** * 鍔犺浇婊氬姩鑼冨洿鏁版嵁 */ - const loadRangeData = async (lastEnd = nextUserMsgEndIndex.value) => { - if (isSharePage.value) { - loadShareData(); - return; - } + const loadRangeData = async (lastEnd = nextUserMsgEndIndex) => { const res = await QueryHistoryDetail({ history_group_id: unref(historyGroupId), last_end: lastEnd, @@ -156,8 +44,9 @@ }); const result: ChatMessage[] = res.details ?? []; if (result.length) { - nextUserMsgEndIndex.value += result.length; - await loadReplyData(res.details); + nextUserMsgEndIndex += result.length; + const rangeMsgList = await loadReplyData(res.details); + messageList.value.unshift(...rangeMsgList); } else { isNoMore = true; } @@ -177,16 +66,16 @@ return; } - let h1 = container.value.scrollHeight; + const h1 = container.value.scrollHeight; moreIsLoading.value = true; - await loadRangeData(nextUserMsgEndIndex.value).finally(() => { + await loadRangeData(nextUserMsgEndIndex).finally(() => { moreIsLoading.value = false; }); //鏇存柊鍚庯紝绛夊緟椤甸潰娓叉煋瀹屾瘯鍐嶅幓鎷縮crollHeight,鍚﹀垯鎷垮埌鐨勬槸涔嬪墠鐨� nextTick(() => { nextTick(() => { nextTick(() => { - let h2 = container.value.scrollHeight; + const h2 = container.value.scrollHeight; container.value.scrollTo({ //椤堕儴鍦ㄥ師鍏堝熀纭�涓婂線涓嬫粴鍔�50px锛岄湶鍑烘柊鍔犺浇鏁版嵁鐨勪竴鐐� // top: h2 - h1 - 50, @@ -204,9 +93,9 @@ }); return { - nextUserMsgEndIndex, loadRangeData, onChatListScroll, moreIsLoading, + updateLoadIndex, }; }; -- Gitblit v1.9.3