【功能完善】商城: 客服会话消息缓存

This commit is contained in:
puhui999 2024-11-09 17:29:49 +08:00
parent e342e91f79
commit df34f65df8
3 changed files with 22 additions and 12 deletions

View File

@ -4,7 +4,6 @@ import { KeFuConversationApi, KeFuConversationRespVO } from '@/api/mall/promotio
import { KeFuMessageRespVO } from '@/api/mall/promotion/kefu/message' import { KeFuMessageRespVO } from '@/api/mall/promotion/kefu/message'
import { isEmpty } from '@/utils/is' import { isEmpty } from '@/utils/is'
// TODO puhui999: 待优化完善
interface MallKefuInfoVO { interface MallKefuInfoVO {
conversationList: KeFuConversationRespVO[] // 会话列表 conversationList: KeFuConversationRespVO[] // 会话列表
conversationMessageList: Map<number, KeFuMessageRespVO[]> // 会话消息 conversationMessageList: Map<number, KeFuMessageRespVO[]> // 会话消息
@ -19,11 +18,17 @@ export const useMallKefuStore = defineStore('mall-kefu', {
getConversationList(): KeFuConversationRespVO[] { getConversationList(): KeFuConversationRespVO[] {
return this.conversationList return this.conversationList
}, },
getConversationMessageList(): Map<number, KeFuMessageRespVO[]> { getConversationMessageList(): (conversationId: number) => KeFuMessageRespVO[] | undefined {
return this.conversationMessageList return (conversationId: number) => this.conversationMessageList.get(conversationId)
} }
}, },
actions: { actions: {
//======================= 会话消息相关 =======================
/** 缓存历史消息 */
saveMessageList(conversationId: number, messageList: KeFuMessageRespVO[]) {
this.conversationMessageList.set(conversationId, messageList)
},
//======================= 会话相关 =======================
/** 加载会话缓存列表 */ /** 加载会话缓存列表 */
async setConversationList() { async setConversationList() {
this.conversationList = await KeFuConversationApi.getConversationList() this.conversationList = await KeFuConversationApi.getConversationList()

View File

@ -93,6 +93,10 @@ const emits = defineEmits<{
(e: 'change', v: KeFuConversationRespVO): void (e: 'change', v: KeFuConversationRespVO): void
}>() }>()
const openRightMessage = (item: KeFuConversationRespVO) => { const openRightMessage = (item: KeFuConversationRespVO) => {
//
if (activeConversationId.value === item.id) {
return
}
activeConversationId.value = item.id activeConversationId.value = item.id
emits('change', item) emits('change', item)
} }

View File

@ -236,19 +236,20 @@ const refreshMessageList = async (message?: any) => {
} }
} }
/** 获得新会话的消息列表 */ /** 获得新会话的消息列表, 点击切换时读取缓存然后异步获取新消息merge 下; */
// TODO @puhui999 list merge
const getNewMessageList = async (val: KeFuConversationRespVO) => { const getNewMessageList = async (val: KeFuConversationRespVO) => {
// , // 1.
messageList.value = [] kefuStore.saveMessageList(conversation.value.id, messageList.value)
total.value = 0 // 2.1 ,
messageList.value = kefuStore.getConversationMessageList(val.id) || []
total.value = messageList.value.length || 0
loadHistory.value = false loadHistory.value = false
refreshContent.value = false refreshContent.value = false
// // 2.2
conversation.value = val conversation.value = val
queryParams.conversationId = val.id queryParams.conversationId = val.id
queryParams.createTime = undefined queryParams.createTime = undefined
// // 3.
await refreshMessageList() await refreshMessageList()
} }
defineExpose({ getNewMessageList, refreshMessageList }) defineExpose({ getNewMessageList, refreshMessageList })
@ -299,8 +300,8 @@ const sendMessage = async (msg: any) => {
message.value = '' message.value = ''
// //
await refreshMessageList() await refreshMessageList()
// //
kefuStore.updateConversation(conversation.value.id) await kefuStore.updateConversation(conversation.value.id)
} }
/** 滚动到底部 */ /** 滚动到底部 */