| | |
| | | import { reject } from 'lodash-es'; |
| | | import { computed, ref } from 'vue'; |
| | | import { CreateHistoryGroup, getSectionList, getSelectSample, getUserTemplateList } from '../api/ai/chat'; |
| | | import { CreateHistoryGroup, GetHistoryGroups, getSectionList, getSelectSample, getUserTemplateList } from '../api/ai/chat'; |
| | | import type { ChatRoomItem } from '../layout/component/sidebar/components/types'; |
| | | import { router } from '../router'; |
| | | import emitter from '../utils/mitt'; |
| | | import { gotoRoute } from '../utils/route'; |
| | | import { Local } from '../utils/storage'; |
| | | |
| | | /** |
| | | * Room 关联的一些配置 |
| | | */ |
| | |
| | | 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)))); |
| | |
| | | 知识库: '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 = () => { |
| | |
| | | * 获取全局所有数据 |
| | | */ |
| | | 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(); |
| | | }; |
| | | |
| | | //#endregion |
| | |
| | | }, |
| | | }); |
| | | } |
| | | setTimeout(() => { |
| | | emitter.emit('isShowHomePage', room.isInitial); |
| | | }, 300); |
| | | }; |
| | | // 是否已经展示引导 |
| | | export const hadShowFirstGuide = ref(false); |
| | | |
| | | // 用户信息 |
| | | export const userInfo = { |
| | | get: () => { |
| | | return Local.get('userInfo'); |
| | | }, |
| | | set: (val) => { |
| | | return Local.set('userInfo', val); |
| | | }, |
| | | clear: () => { |
| | | Local.remove('userInfo'); |
| | | }, |
| | | get isNew() { |
| | | return !this.get()?.web_login; |
| | | }, |
| | | }; |
| | | |
| | | let getHistoryChatRoomsPromise: Promise<any>; |
| | | //历史对话 |
| | | const getHistoryChatRooms = async () => { |
| | | 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, |
| | | }; |
| | | }); |
| | | |
| | | resolve(chatRoomList.value); |
| | | }); |
| | | }; |
| | | |
| | | 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); |
| | | } |
| | | }); |
| | | }; |