yangyin
2024-11-14 a12818479aac56a0c08206ce4dcf4ede092e9376
src/stores/chatRoom.ts
@@ -1,7 +1,8 @@
import { computed, ref } from 'vue';
import { CreateHistoryGroup, getSectionList, getSelectSample, getUserTemplateList } from '../api/ai/chat';
import type { ChatRoomItem } from '../layout/component/sidebar/components/types';
import { getSectionList } from '../api/ai/chat';
import { router } from '../router';
import { gotoRoute } from '../utils/route';
/**
 * Room 关联的一些配置
 */
@@ -56,7 +57,7 @@
/** @description 当前聊天室 groupType */
export const activeGroupType = computed({
   get: () => {
      const result = getRoomConfig(activeRoomId.value, 'activeGroupType') ??'业务场景';
      const result = getRoomConfig(activeRoomId.value, 'activeGroupType') ?? '业务场景';
      return result;
   },
   set: (value) => {
@@ -68,28 +69,112 @@
 * 全局使用的 ref
 */
export const sectionAList = ref([]);
export const isShowLogin = ref(false);
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))));
// 办公/模板 列表
export const exampleSceneList = ref([]);
export const officeList = ref([]);
export const groupTypeMapIcon = {
   办公助手: 'ywicon-bangong',
   知识库: '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 randomHexColor = () => {
   return `#${Math.floor(Math.random() * 16777215)
      .toString(16)
      .padEnd(6, '0')}`;
};
/**
 * 获取全局所有数据
 */
export const getAllData = async () => {
   getSceneGroupList();
   getSelectListSample();
};
//#endregion
/** @description 聊天室展示模式 */
export type ChatMode = 'share' | 'default';
/** @description 聊天室展示模式 */
// export const chatDisplayMode = ref<ChatMode>('default');
export const SHARE_PAGE_NAME = 'ShareAnswer';
export const isSharePage = computed(() => {
   const isShare = router.currentRoute.value.name === SHARE_PAGE_NAME;
   return isShare;
});
export const newChatRoomClick = async () => {
   const res = await CreateHistoryGroup({
      group_title: '新建对话开始',
   });
   const newRoom = {
      id: res.history_group_id,
      isInitial: true,
      title: '新建对话开始',
   };
   if (!chatRoomList.value) {
      chatRoomList.value = [newRoom];
   } else {
      chatRoomList.value.unshift(newRoom);
   }
   activeRoomId.value = newRoom.id;
   gotoAnswerPage(newRoom);
};
export const gotoAnswerPage = (room: ChatRoomItem) => {
   if (room.isInitial) {
      gotoRoute({
         name: 'Home',
         query: {
            id: room.id,
         },
      });
   } else {
      gotoRoute({
         name: 'AskAnswer',
         query: {
            id: room.id,
         },
      });
   }
};
// 是否已经展示引导
export const hadShowFirstGuide = ref(false);
//是否是新老用户
export const isNewOldUser = ref(null);