wujingjing
2024-10-31 404dfd588ca780dbafea362880d185d3643ebf8b
src/components/chat/hooks/useScrollLoad.ts
@@ -1,11 +1,11 @@
import { Ref, ShallowRef, nextTick, onBeforeUnmount, ref } from 'vue';
import { Ref, ShallowRef, nextTick, onBeforeUnmount, ref, unref } from 'vue';
import { LOAD_CHAT_LIMIT } from '../constants';
import { AnswerType, ChatContent, ChatMessage, RoleEnum } from '../model/types';
import { GetHistoryAnswer, QueryHistoryDetail } from '/@/api/ai/chat';
type UseScrollLoadOption = {
   container: ShallowRef<HTMLDivElement>;
   historyGroupId: string;
   historyGroupId: string | Ref<string>;
   messageList: Ref<ChatMessage[]>;
   parseAnswerContent: (res: any) => ChatContent;
};
@@ -33,7 +33,8 @@
    * 获取用户回复数据,并插入到对话当中去
    */
   const loadReplyData = async (userMsg: any[]) => {
      const sectionAIdMap = new Map();
      const userItemIdMap = new Map();
      // 用户消息
      const tmpMessageList: ChatMessage[] = userMsg.map((item) => {
         return {
            historyId: item.history_id,
@@ -46,7 +47,7 @@
      });
      const resList = await Promise.all(
         (userMsg ?? []).map((item) => {
            sectionAIdMap.set(item.history_id, item.section_a_id);
            userItemIdMap.set(item.history_id, item);
            return getAnswerById(item.history_id);
         })
      );
@@ -55,6 +56,8 @@
         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)
         tmpMessageList.splice(
            insertIndex,
            0,
@@ -65,7 +68,8 @@
                     role: RoleEnum.assistant,
                     content: parseAnswerContent(item.answer),
                     state: item.answer_state,
                     sectionAId: sectionAIdMap.get(item.answer.history_id),
                     sectionAId: mapUser?.section_a_id,
                     createTime:mapUser?.create_time
                 }
         );
         i++;
@@ -79,7 +83,7 @@
    */
   const loadRangeData = async (lastEnd = nextUserMsgEndIndex.value) => {
      const res = await QueryHistoryDetail({
         history_group_id: historyGroupId,
         history_group_id: unref(historyGroupId),
         last_end: lastEnd,
         last_count: LOAD_CHAT_LIMIT,
      });
@@ -97,7 +101,6 @@
   //滚动监听
   async function onChatListScroll() {
      if (container.value.scrollTop == 0) {
         console.log('滚动到顶部了');
         // 更多数据正在加载时
         if (moreIsLoading.value) {