wujingjing
2025-01-06 d7ae67486dc0793fe0596140ca6f13a1b48f848c
src/stores/chatRoom.ts
@@ -1,3 +1,4 @@
import { reject } from 'lodash-es';
import { computed, ref } from 'vue';
import { CreateHistoryGroup, GetHistoryGroups, getSectionList, getSelectSample, getUserTemplateList } from '../api/ai/chat';
import type { ChatRoomItem } from '../layout/component/sidebar/components/types';
@@ -5,6 +6,7 @@
import emitter from '../utils/mitt';
import { gotoRoute } from '../utils/route';
import { Local } from '../utils/storage';
/**
 * Room 关联的一些配置
 */
@@ -75,7 +77,7 @@
export const isLoginStatus = ref(false);
//#region ====================== 全局使用数据 ======================
// group 列表
// 主场景 列表
export const sceneGroupList = ref([]);
// groupType 列表
export const groupTypeList = computed(() => Array.from(new Set(sceneGroupList.value.map((item) => item.group_type))));
@@ -87,31 +89,24 @@
   知识库: 'ywicon-changyonggongjuzhishisuoyin',
   业务场景: 'ywicon-yewu',
};
//获取场景选择列表
const getSceneGroupList = async () => {
   const res = await getSectionList();
   sceneGroupList.value = res?.groups ?? [];
};
const getSelectListSample = async () => {
   const res1 = getSelectSample({});
   const res2 = getUserTemplateList();
   const [sampleListPromise, templateListPromise] = await Promise.allSettled([res1, res2]);
   const samples = (sampleListPromise as any).value?.samples ?? [];
   const templateSamples = ((templateListPromise as any).value?.templates ?? []).map((item) => ({
      group_id: item.template_group,
      sample_id: item.template_id,
      sample_title: item.template_title,
      sample_question: item.template_value,
      //#region ====================== template 特有字段 ======================
      template_create_time: item.create_time,
      template_type: item.template_type,
      isTemplate: true,
      //#endregion
   }));
   exampleSceneList.value = samples
      .concat(templateSamples)
      .map((item) => ({ ...item, Icon: '/static/images/wave/ChatImg.png', BgColor: randomHexColor() }));
const getSelectListSample = async (res1, res2) => {
   try {
      const samples = res1?.value?.samples ?? [];
      const templateSamples = (res2?.value?.templates ?? []).map((item) => ({
         group_id: item.template_group,
         sample_id: item.template_id,
         sample_title: item.template_title,
         sample_question: item.template_value,
         template_create_time: item.create_time,
         template_type: item.template_type,
         isTemplate: true,
      }));
      exampleSceneList.value = samples
         .concat(templateSamples)
         .map((item) => ({ ...item, Icon: '/static/images/wave/ChatImg.png', BgColor: randomHexColor() }));
   } catch (error) {
      console.error('获取选择列表样本时出错:', error);
   }
};
//随机生成颜色
const randomHexColor = () => {
@@ -123,8 +118,13 @@
 * 获取全局所有数据
 */
export const getAllData = async () => {
   getSceneGroupList();
   getSelectListSample();
   Promise.allSettled([getSectionList(), getSelectSample({}), getUserTemplateList()])
      .then((res) => {
         let [sectionList, selectSample, userTemplateList] = res;
         sceneGroupList.value = sectionList?.value?.groups ?? [];
         getSelectListSample(selectSample, userTemplateList);
      })
      .catch((err) => {});
   getHistoryChatRooms();
};
@@ -197,30 +197,44 @@
   },
};
let getHistoryChatRoomsPromise: Promise<any>;
//历史对话
const getHistoryChatRooms = async () => {
   const res = await GetHistoryGroups();
   const resData = (res?.groups || []) as any[];
   chatRoomList.value = resData; // 按最晚时间到最早时间
   chatRoomList.value = resData
      ?.toSorted((a, b) => {
         return b.create_time.localeCompare(a.create_time);
      })
      .map((item) => {
         return {
            id: item.group_id,
            title: item.group_title,
            createTime: item.create_time,
            isInitial: Number(item.chat_count) === 0,
         };
      });
   getHistoryChatRoomsPromise = new Promise(async (resolve, reject) => {
      if (isSharePage.value) return resolve(null);
      const res = await GetHistoryGroups();
      const resData = (res?.groups || []) as any[];
      // 按最晚时间到最早时间
      chatRoomList.value = resData
         ?.toSorted((a, b) => {
            return b.create_time.localeCompare(a.create_time);
         })
         .map((item) => {
            return {
               id: item.group_id,
               title: item.group_title,
               createTime: item.create_time,
               isInitial: Number(item.chat_count) === 0,
            };
         });
   if (!chatRoomList.value || chatRoomList.value.length === 0) {
      newChatRoomClick();
   } else {
      const toClickRoom = activeChatRoom.value ?? chatRoomList.value[0];
      activeRoomId.value = toClickRoom.id;
      resolve(chatRoomList.value);
   });
};
      gotoAnswerPage(toClickRoom);
   }
const roomClick = (room: ChatRoomItem) => {
   activeRoomId.value = room.id;
   gotoAnswerPage(room);
};
export const selectFirstRoom = () => {
   getHistoryChatRoomsPromise.then(() => {
      if (!chatRoomList.value || chatRoomList.value.length === 0) {
         newChatRoomClick();
      } else {
         const toClickRoom = activeChatRoom.value ?? chatRoomList.value[0];
         roomClick(toClickRoom);
      }
   });
};