gerson
2025-01-21 c9071fc1d8ae01496f4715adda6989a71b503a3d
src/stores/chatRoom.ts
@@ -6,6 +6,10 @@
import emitter from '../utils/mitt';
import { gotoRoute } from '../utils/route';
import { Local } from '../utils/storage';
import { PingLogin } from '../api/system';
import { SSEClient } from '../utils/sse/SSEClient';
import { accessSessionKey } from '../utils/request';
import { MAIN_URL } from '../constants';
/**
 * Room 关联的一些配置
@@ -125,6 +129,7 @@
         getSelectListSample(selectSample, userTemplateList);
      })
      .catch((err) => {});
   getHistoryChatRooms();
};
//#endregion
@@ -195,3 +200,61 @@
      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);
      }
   });
};
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;
};