From 6a0c9cc8b94b809e345ca384935aec89456868b6 Mon Sep 17 00:00:00 2001 From: Evan Date: Tue, 19 Sep 2023 23:47:44 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=8F=91=E8=B5=B7?= =?UTF-8?q?=E8=81=8A=E5=A4=A9=E6=97=B6=EF=BC=8C=E4=BC=9A=E8=AF=9D=E6=9C=AA?= =?UTF-8?q?=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/services/apis.ts | 3 ++ src/services/urls.ts | 1 + src/stores/chat.ts | 38 +++++++++++++++++-- .../Chat/components/ChatBox/SendBar/index.vue | 2 +- .../Home/Chat/components/SideBar/index.vue | 13 +++++-- .../Home/Chat/components/SideBar/styles.scss | 3 +- src/views/Home/Chat/index.vue | 4 -- .../components/ContactList/Content/index.vue | 19 +++++++--- 8 files changed, 63 insertions(+), 20 deletions(-) diff --git a/src/services/apis.ts b/src/services/apis.ts index 6387f9bd..5ec4e2c6 100644 --- a/src/services/apis.ts +++ b/src/services/apis.ts @@ -105,4 +105,7 @@ export default { deleteRequest(urls.inviteGroupMember, params), /** 群组详情 */ groupDetail: (params: { id: number }) => getRequest(urls.groupDetail, { params }), + /** 会话详情(联系人列表发消息用) */ + sessionDetail: (params: { uid: number }) => + getRequest(urls.sessionDetail, { params }), } diff --git a/src/services/urls.ts b/src/services/urls.ts index 5d9d79c3..cbc4d023 100644 --- a/src/services/urls.ts +++ b/src/services/urls.ts @@ -33,4 +33,5 @@ export default { getGroupUserList: `${prefix}/capi/room/public/group/member/page`, inviteGroupMember: `${prefix}/capi/room/group/member`, // 邀请群成员 groupDetail: `${prefix}/capi/room/public/group`, // 群组详情 + sessionDetail: `${prefix}/capi/chat/public/contact/detail/friend`, // 会话详情(联系人列表发消息用) } diff --git a/src/stores/chat.ts b/src/stores/chat.ts index 4d2015e4..7a133ea5 100644 --- a/src/stores/chat.ts +++ b/src/stores/chat.ts @@ -1,5 +1,6 @@ import { ref, reactive, computed, watch } from 'vue' import { defineStore } from 'pinia' +import cloneDeep from 'lodash/cloneDeep' import { useRoute } from 'vue-router' import apis from '@/services/apis' import type { MessageType, MarkItemType, RevokedMsgType, SessionItem } from '@/services/types' @@ -196,7 +197,7 @@ export const useChatStore = defineStore('chat', () => { const data = await apis .getSessionList({ params: { - pageSize: sessionList.length > pageSize ? sessionList.length : pageSize, + pageSize: sessionList.length > pageSize ? sessionList.length : 10, cursor: isFresh || !sessionOptions.cursor ? undefined : sessionOptions.cursor, }, }) @@ -212,6 +213,8 @@ export const useChatStore = defineStore('chat', () => { sessionOptions.isLast = data.isLast sessionOptions.isLoading = false + sortAndUniqueSessionList() + if (!isFirstInit) { isFirstInit = true globalStore.currentSession.roomId = data.list[0].roomId @@ -227,6 +230,32 @@ export const useChatStore = defineStore('chat', () => { } } + /** 会话列表去重并排序 */ + const sortAndUniqueSessionList = () => { + const temp: Record = {} + sessionList.forEach((item) => (temp[item.roomId] = item)) + sessionList.splice(0, sessionList.length, ...Object.values(temp)) + sessionList.sort((pre, cur) => cur.activeTime - pre.activeTime) + } + + const updateSession = (roomId: number, roomProps: Partial) => { + const session = sessionList.find((item) => item.roomId === roomId) + session && roomProps && Object.assign(session, roomProps) + sortAndUniqueSessionList() + } + + const updateSessionLastActiveTime = (roomId: number, room?: SessionItem) => { + const session = sessionList.find((item) => item.roomId === roomId) + if (session) { + Object.assign(session, { activeTime: Date.now() }) + } else if (room) { + const newItem = cloneDeep(room) + newItem.activeTime = Date.now() + sessionList.unshift(newItem) + } + sortAndUniqueSessionList() + } + const pushMsg = (msg: MessageType) => { const current = messageMap.get(msg.message.roomId) current?.set(msg.message.id, msg) @@ -240,7 +269,8 @@ export const useChatStore = defineStore('chat', () => { // 发完消息就要刷新会话列表, // 如果当前会话已经置顶了,可以不用刷新 if (globalStore.currentSession && globalStore.currentSession.roomId !== msg.message.roomId) { - getSessionList(true) + // getSessionList(true) + updateSessionLastActiveTime(msg.message.roomId) } // 如果收到的消息里面是艾特自己的就发送系统通知 @@ -267,7 +297,7 @@ export const useChatStore = defineStore('chat', () => { } // 如果当前路由不是聊天,就开始计数 - if (route?.path !== '/') { + if (route?.path && route?.path !== '/') { globalStore.unReadMark.newMsgUnreadCount++ } @@ -390,6 +420,8 @@ export const useChatStore = defineStore('chat', () => { sessionList, sessionOptions, getSessionList, + updateSession, + updateSessionLastActiveTime, markSessionRead, } }) diff --git a/src/views/Home/Chat/components/ChatBox/SendBar/index.vue b/src/views/Home/Chat/components/ChatBox/SendBar/index.vue index 74f22870..48734d25 100644 --- a/src/views/Home/Chat/components/ChatBox/SendBar/index.vue +++ b/src/views/Home/Chat/components/ChatBox/SendBar/index.vue @@ -86,7 +86,7 @@ const send = (msgType: MsgEnum, body: any) => { // 发完消息就要刷新会话列表, // FIXME 如果当前会话已经置顶了,可以不用刷新 - chatStore.getSessionList(true) + chatStore.updateSessionLastActiveTime(globalStore.currentSession.roomId) }) .finally(() => { isSending.value = false diff --git a/src/views/Home/Chat/components/SideBar/index.vue b/src/views/Home/Chat/components/SideBar/index.vue index 444fd401..114caa48 100644 --- a/src/views/Home/Chat/components/SideBar/index.vue +++ b/src/views/Home/Chat/components/SideBar/index.vue @@ -44,11 +44,16 @@ const onSelectSelectSession = (roomId: number, roomType: RoomTypeEnum) => { globalStore.currentSession.roomId = roomId globalStore.currentSession.type = roomType } + +// 加载更多 +const load = () => { + chatStore.getSessionList() +}