Skip to content

Commit

Permalink
fix: 修复发起聊天时,会话未创建
Browse files Browse the repository at this point in the history
  • Loading branch information
Evansy committed Sep 19, 2023
1 parent d627811 commit 6a0c9cc
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 20 deletions.
3 changes: 3 additions & 0 deletions src/services/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,7 @@ export default {
deleteRequest(urls.inviteGroupMember, params),
/** 群组详情 */
groupDetail: (params: { id: number }) => getRequest<GroupDetailReq>(urls.groupDetail, { params }),
/** 会话详情(联系人列表发消息用) */
sessionDetail: (params: { uid: number }) =>
getRequest<SessionItem>(urls.sessionDetail, { params }),
}
1 change: 1 addition & 0 deletions src/services/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`, // 会话详情(联系人列表发消息用)
}
38 changes: 35 additions & 3 deletions src/stores/chat.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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,
},
})
Expand All @@ -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
Expand All @@ -227,6 +230,32 @@ export const useChatStore = defineStore('chat', () => {
}
}

/** 会话列表去重并排序 */
const sortAndUniqueSessionList = () => {
const temp: Record<string, SessionItem> = {}
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<SessionItem>) => {
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)
Expand All @@ -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)
}

// 如果收到的消息里面是艾特自己的就发送系统通知
Expand All @@ -267,7 +297,7 @@ export const useChatStore = defineStore('chat', () => {
}

// 如果当前路由不是聊天,就开始计数
if (route?.path !== '/') {
if (route?.path && route?.path !== '/') {
globalStore.unReadMark.newMsgUnreadCount++
}

Expand Down Expand Up @@ -390,6 +420,8 @@ export const useChatStore = defineStore('chat', () => {
sessionList,
sessionOptions,
getSessionList,
updateSession,
updateSessionLastActiveTime,
markSessionRead,
}
})
2 changes: 1 addition & 1 deletion src/views/Home/Chat/components/ChatBox/SendBar/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const send = (msgType: MsgEnum, body: any) => {
// 发完消息就要刷新会话列表,
// FIXME 如果当前会话已经置顶了,可以不用刷新
chatStore.getSessionList(true)
chatStore.updateSessionLastActiveTime(globalStore.currentSession.roomId)
})
.finally(() => {
isSending.value = false
Expand Down
13 changes: 9 additions & 4 deletions src/views/Home/Chat/components/SideBar/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,16 @@ const onSelectSelectSession = (roomId: number, roomType: RoomTypeEnum) => {
globalStore.currentSession.roomId = roomId
globalStore.currentSession.type = roomType
}
// 加载更多
const load = () => {
chatStore.getSessionList()
}
</script>

<template>
<div class="chat-message">
<div
<ul class="chat-message" v-infinite-scroll="load">
<li
v-for="(item, index) in sessionList"
:key="index"
:class="['chat-message-item ', { active: currentSession.roomId === item.roomId }]"
Expand All @@ -65,8 +70,8 @@ const onSelectSelectSession = (roomId: number, roomType: RoomTypeEnum) => {
<div class="message-message">{{ item.lastMsg }}</div>
</div>
<span class="message-time">{{ item.lastMsgTime }}</span>
</div>
</div>
</li>
</ul>
</template>

<style lang="scss" src="./styles.scss" scoped />
3 changes: 1 addition & 2 deletions src/views/Home/Chat/components/SideBar/styles.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.chat-message {
display: none;

// padding: 4px;
padding: 0;
margin: 20px 10px;
overflow-y: auto;

Expand Down
4 changes: 0 additions & 4 deletions src/views/Home/Chat/index.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
<script setup lang="ts">
import { useChatStore } from '@/stores/chat'
import { useGlobalStore } from '@/stores/global'
import SideBar from './components/SideBar/index.vue'
import ChatBox from './components/ChatBox/index.vue'
const chatStore = useChatStore()
const globalStore = useGlobalStore()
// 默认执行一次
chatStore.getSessionList(true)
globalStore.unReadMark.newMsgUnreadCount = 0
</script>

Expand Down
19 changes: 13 additions & 6 deletions src/views/Home/Contacts/components/ContactList/Content/index.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<script setup lang="ts" name="ContactSide">
import Router from '@/router'
import { computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useContactStore } from '@/stores/contacts'
import { useChatStore } from '@/stores/chat'
import { useGlobalStore } from '@/stores/global'
import { useUserInfo } from '@/hooks/useCached'
import { RequestFriendAgreeStatus } from '@/services/types'
import type { RequestFriendItem } from '@/services/types'
import { RoomTypeEnum } from '@/enums'
import apis from '@/services/apis'
const contactStore = useContactStore()
const chatStore = useChatStore()
const globalStore = useGlobalStore()
const selectedContact = computed(() => globalStore.currentSelectedContact as RequestFriendItem)
Expand All @@ -32,9 +36,12 @@ const onDeleteContact = (uid: number) => {
//
})
}
const onStartSession = (roomId: number) => {
globalStore.currentSession.roomId = roomId
const onStartSession = async (uid: number) => {
const result = await apis.sessionDetail({ uid }).send()
globalStore.currentSession.roomId = result.roomId
globalStore.currentSession.type = RoomTypeEnum.Single
chatStore.updateSessionLastActiveTime(result.roomId, result)
Router.push('/')
}
</script>

Expand Down Expand Up @@ -72,10 +79,10 @@ const onStartSession = (roomId: number) => {
>接受</ElButton
>
<template v-else>
<ElButton type="primary" @click="onStartSession(selectedContact.roomId)">发消息</ElButton>
<ElButton type="danger" @click="onDeleteContact(selectedContact.uid)"
>删除联系人</ElButton
>
<ElButton type="primary" @click="onStartSession(selectedContact.uid)">发消息</ElButton>
<ElButton type="danger" @click="onDeleteContact(selectedContact.uid)">
删除联系人
</ElButton>
</template>
</div>
</div>
Expand Down

0 comments on commit 6a0c9cc

Please sign in to comment.