From 4e57cc40899cb6dcaeec1eda0a2325a74e44f9db Mon Sep 17 00:00:00 2001 From: wujingjing <gersonwu@qq.com> Date: 星期五, 14 三月 2025 18:41:27 +0800 Subject: [PATCH] 微信修改 --- src/stores/chatRoom.ts | 185 ++++++++++++++++++++++++++++++++++++--------- 1 files changed, 147 insertions(+), 38 deletions(-) diff --git a/src/stores/chatRoom.ts b/src/stores/chatRoom.ts index 3a9b65c..abf64f8 100644 --- a/src/stores/chatRoom.ts +++ b/src/stores/chatRoom.ts @@ -1,8 +1,16 @@ 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 { 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, LocalPlus } from '../utils/storage'; +import { getCurrentPosition } from '../utils/brower'; + /** * Room 鍏宠仈鐨勪竴浜涢厤缃� */ @@ -57,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) => { @@ -69,9 +77,11 @@ * 鍏ㄥ眬浣跨敤鐨� 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)))); @@ -83,31 +93,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 = () => { @@ -119,8 +122,14 @@ * 鑾峰彇鍏ㄥ眬鎵�鏈夋暟鎹� */ export const getAllData = async () => { - getSceneGroupList(); - getSelectListSample(); + Promise.allSettled([getSectionList(), getSelectSample({}), getUserTemplateList()]) + .then((res) => { + const [sectionList, selectSample, userTemplateList] = res; + sceneGroupList.value = sectionList?.value?.groups ?? []; + getSelectListSample(selectSample, userTemplateList); + }) + .catch((err) => {}); + getHistoryChatRooms(); }; //#endregion @@ -130,25 +139,21 @@ /** @description 鑱婂ぉ瀹ゅ睍绀烘ā寮� */ // export const chatDisplayMode = ref<ChatMode>('default'); -export const SHARE_PAGE_NAME = 'ShareAnswer' +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: '鏂板缓瀵硅瘽寮�濮�', + group_title: '鏂板缓瀵硅瘽', }); const newRoom = { id: res.history_group_id, isInitial: true, - title: '鏂板缓瀵硅瘽寮�濮�', + title: '鏂板缓瀵硅瘽', }; if (!chatRoomList.value) { chatRoomList.value = [newRoom]; @@ -175,5 +180,109 @@ }, }); } + setTimeout(() => { + emitter.emit('isShowHomePage', room.isInitial); + }, 300); +}; -}; \ No newline at end of file +// 鐢ㄦ埛淇℃伅 +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); + } + }); +}; + +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); +}; -- Gitblit v1.9.3