wujingjing
2024-11-07 b006d0fefbcd38f9fe3d95a46519b2e5e6720d5c
src/components/chat/hooks/useScrollLoad.ts
@@ -1,8 +1,8 @@
import { Ref, ShallowRef, nextTick, onBeforeUnmount, ref, unref } from 'vue';
import moment from 'moment';
import { Ref, ShallowRef, computed, nextTick, onBeforeUnmount, ref, unref } from 'vue';
import { LOAD_CHAT_LIMIT } from '../constants';
import { AnswerType, ChatContent, ChatMessage, RoleEnum } from '../model/types';
import { AnswerType, ChatContent, ChatMessage, RoleEnum, StepEnum, StepItem } from '../model/types';
import { GetHistoryAnswer, QueryHistoryDetail } from '/@/api/ai/chat';
type UseScrollLoadOption = {
   container: ShallowRef<HTMLDivElement>;
   historyGroupId: string | Ref<string>;
@@ -10,6 +10,24 @@
   parseAnswerContent: (res: any) => ChatContent;
};
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
@@ -28,7 +46,11 @@
         history_id: historyId,
      });
   };
   const formatShowTimeYear = computed(() => {
      return (str) => {
         return moment(str).format('MM月DD日 HH:mm:ss');
      };
   });
   /**
    * 获取用户回复数据,并插入到对话当中去
    */
@@ -56,8 +78,10 @@
         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 mapUser = userItemIdMap.get(item.answer.history_id)
         const answerTime = formatShowTimeYear.value(mapUser?.create_time);
         tmpMessageList.splice(
            insertIndex,
            0,
@@ -69,7 +93,10 @@
                     content: parseAnswerContent(item.answer),
                     state: item.answer_state,
                     sectionAId: mapUser?.section_a_id,
                     createTime:mapUser?.create_time
                     createTime: answerTime,
                     stepList: convertProcessToStep(item?.answer?.exec_process),
                     stepIsShow:false,
                     conclusion:item?.answer?.conclusion ??[],
                 }
         );
         i++;
@@ -101,7 +128,6 @@
   //滚动监听
   async function onChatListScroll() {
      if (container.value.scrollTop == 0) {
         // 更多数据正在加载时
         if (moreIsLoading.value) {
            return;
@@ -117,9 +143,9 @@
            moreIsLoading.value = false;
         });
         //更新后,等待页面渲染完毕再去拿scrollHeight,否则拿到的是之前的
         nextTick(()=>{
            nextTick(()=>{
               nextTick(()=>{
         nextTick(() => {
            nextTick(() => {
               nextTick(() => {
                  let h2 = container.value.scrollHeight;
                  container.value.scrollTo({
                     //顶部在原先基础上往下滚动50px,露出新加载数据的一点
@@ -127,10 +153,9 @@
                     top: h2 - h1,
                     behavior: 'instant', //auto-自动滚动 instant-瞬间滚动 smooth-平滑滚动
                  });
               })
            })
         })
               });
            });
         });
      }
   }