wujingjing
2025-03-14 4e57cc40899cb6dcaeec1eda0a2325a74e44f9db
src/stores/chatRoom.ts
@@ -1,11 +1,16 @@
import { reject } from 'lodash-es';
import { computed, ref } from 'vue';
import { CreateHistoryGroup, GetHistoryGroups, getSectionList, getSelectSample, getUserTemplateList } from '../api/ai/chat';
import { PostLogin } from '../api/ai/user';
import { PingLogin } from '../api/system';
import { LOGIN_CLIENT, STORED_ACCOUNT_KEY, handleAfterLogin } from '../layout/component/login/login';
import type { ChatRoomItem } from '../layout/component/sidebar/components/types';
import { router } from '../router';
import { decrypt } from '../utils/cypto';
import emitter from '../utils/mitt';
import { gotoRoute } from '../utils/route';
import { Local } from '../utils/storage';
import { Local, LocalPlus } from '../utils/storage';
import { getCurrentPosition } from '../utils/brower';
/**
 * Room 关联的一些配置
 */
@@ -60,7 +65,7 @@
/** @description 当前聊天室 groupType */
export const activeGroupType = computed({
   get: () => {
      const result = getRoomConfig(activeRoomId.value, 'activeGroupType') ?? '业务场景';
      const result = getRoomConfig(activeRoomId.value, 'activeGroupType') ?? groupTypeList.value.at(-1);
      return result;
   },
   set: (value) => {
@@ -90,8 +95,8 @@
};
const getSelectListSample = async (res1, res2) => {
   try {
      const samples = res1?.samples ?? [];
      const templateSamples = (res2?.templates ?? []).map((item) => ({
      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,
@@ -117,10 +122,10 @@
 * 获取全局所有数据
 */
export const getAllData = async () => {
   Promise.all([getSectionList(), getSelectSample({}), getUserTemplateList()])
   Promise.allSettled([getSectionList(), getSelectSample({}), getUserTemplateList()])
      .then((res) => {
         let [sectionList, selectSample, userTemplateList] = res;
         sceneGroupList.value = sectionList?.groups ?? [];
         const [sectionList, selectSample, userTemplateList] = res;
         sceneGroupList.value = sectionList?.value?.groups ?? [];
         getSelectListSample(selectSample, userTemplateList);
      })
      .catch((err) => {});
@@ -142,13 +147,13 @@
export const newChatRoomClick = async () => {
   const res = await CreateHistoryGroup({
      group_title: '新建对话开始',
      group_title: '新建对话',
   });
   const newRoom = {
      id: res.history_group_id,
      isInitial: true,
      title: '新建对话开始',
      title: '新建对话',
   };
   if (!chatRoomList.value) {
      chatRoomList.value = [newRoom];
@@ -196,36 +201,88 @@
   },
};
let getHistoryChatRoomsPromise: Promise<any>;
//历史对话
const getHistoryChatRooms = () => {
   return new Promise((resolve, reject) => {
      GetHistoryGroups()
         .then((res) => {
            const resData = (res?.groups || []) as any[];
            chatRoomList.value = resData
               .toSorted((a, b) => b.create_time.localeCompare(a.create_time))
               .map((item) => ({
                  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;
               gotoAnswerPage(toClickRoom);
            }
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);
         })
         .then(() => {
            resolve(true);
         })
         .catch((error) => {
            reject(error);
         .map((item) => {
            return {
               id: item.group_id,
               title: item.group_title,
               createTime: item.create_time,
               isInitial: Number(item.chat_count) === 0,
            };
         });
   }).catch((error) => {
      reject(error);
      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);
      }
   });
};
export const pingLogin = async () => {
   // 5分钟
   const interval = 1000 * 60 * 5;
   // const interval = 1000 *2;
   const timer = setInterval(async () => {
      const res = await PingLogin();
      if (!res?.is_login) {
         clearInterval(timer);
      }
   }, interval);
   return timer;
};
/**
 * 自动登录,从本地获取登录信息
 * @returns
 */
export const autoLogin = async () => {
   const account = LocalPlus.get(STORED_ACCOUNT_KEY);
   if (!account) return;
   const accountInfo = decrypt(account);
   if (!accountInfo) return;
   const res: any = await PostLogin({
      user: accountInfo.username,
      pass: accountInfo.password,
      client: LOGIN_CLIENT,
   });
   if (!res.json_ok || !res.hswatersession) {
      return;
   }
   handleAfterLogin(res);
};
export const currentPosition = ref<Position>(null);
export const getGlobalPosition = () => {
   getCurrentPosition();
   setInterval(() => {
      getCurrentPosition();
   }, 1000 * 60 * 60);
};