diff --git a/src/api/mall/promotion/kefu/conversation/index.ts b/src/api/mall/promotion/kefu/conversation/index.ts index 1b8d5389..eb6eb9c9 100644 --- a/src/api/mall/promotion/kefu/conversation/index.ts +++ b/src/api/mall/promotion/kefu/conversation/index.ts @@ -21,6 +21,10 @@ export const KeFuConversationApi = { getConversationList: async () => { return await request.get({ url: '/promotion/kefu-conversation/list' }) }, + // 获得客服会话 + getConversation: async (id: number) => { + return await request.get({ url: `/promotion/kefu-conversation/get?id=` + id }) + }, // 客服会话置顶 updateConversationPinned: async (data: any) => { return await request.put({ @@ -30,6 +34,6 @@ export const KeFuConversationApi = { }, // 删除客服会话 deleteConversation: async (id: number) => { - return await request.delete({ url: `/promotion/kefu-conversation/delete?id=${id}`}) + return await request.delete({ url: `/promotion/kefu-conversation/delete?id=${id}` }) } } diff --git a/src/store/modules/mall/kefu.ts b/src/store/modules/mall/kefu.ts index f3afc03b..fae52f70 100644 --- a/src/store/modules/mall/kefu.ts +++ b/src/store/modules/mall/kefu.ts @@ -2,8 +2,8 @@ import { store } from '@/store' import { defineStore } from 'pinia' import { KeFuConversationApi, KeFuConversationRespVO } from '@/api/mall/promotion/kefu/conversation' import { KeFuMessageRespVO } from '@/api/mall/promotion/kefu/message' +import { isEmpty } from '@/utils/is' -// TODO puhui999: 待优化完善 interface MallKefuInfoVO { conversationList: KeFuConversationRespVO[] // 会话列表 conversationMessageList: Map // 会话消息 @@ -18,20 +18,92 @@ export const useMallKefuStore = defineStore('mall-kefu', { getConversationList(): KeFuConversationRespVO[] { return this.conversationList }, - getConversationMessageList(): Map { - return this.conversationMessageList + getConversationMessageList(): (conversationId: number) => KeFuMessageRespVO[] | undefined { + return (conversationId: number) => this.conversationMessageList.get(conversationId) } }, actions: { + //======================= 会话消息相关 ======================= + /** 缓存历史消息 */ + saveMessageList(conversationId: number, messageList: KeFuMessageRespVO[]) { + this.conversationMessageList.set(conversationId, messageList) + }, + //======================= 会话相关 ======================= + /** 加载会话缓存列表 */ async setConversationList() { - const list = await KeFuConversationApi.getConversationList() - list.sort((a: KeFuConversationRespVO, _) => (a.adminPinned ? -1 : 1)) - this.conversationList = list + this.conversationList = await KeFuConversationApi.getConversationList() + this.conversationSort() + }, + /** 更新会话缓存已读 */ + async updateConversationStatus(conversationId: number) { + if (isEmpty(this.conversationList)) { + return + } + const conversation = this.conversationList.find((item) => item.id === conversationId) + conversation && (conversation.adminUnreadMessageCount = 0) + }, + /** 更新会话缓存 */ + async updateConversation(conversationId: number) { + if (isEmpty(this.conversationList)) { + return + } + + const conversation = await KeFuConversationApi.getConversation(conversationId) + this.deleteConversation(conversationId) + conversation && this.conversationList.push(conversation) + this.conversationSort() + }, + /** 删除会话缓存 */ + deleteConversation(conversationId: number) { + const index = this.conversationList.findIndex((item) => item.id === conversationId) + // 存在则删除 + if (index > -1) { + this.conversationList.splice(index, 1) + } + }, + conversationSort() { + this.conversationList.sort((obj1, obj2) => { + // 如果 obj1.adminPinned 为 true,obj2.adminPinned 为 false,obj1 应该排在前面 + if (obj1.adminPinned && !obj2.adminPinned) return -1 + // 如果 obj1.adminPinned 为 false,obj2.adminPinned 为 true,obj2 应该排在前面 + if (!obj1.adminPinned && obj2.adminPinned) return 1 + + // 如果 obj1.adminPinned 和 obj2.adminPinned 都为 true,比较 adminUnreadMessageCount 的值 + if (obj1.adminPinned && obj2.adminPinned) { + return obj1.adminUnreadMessageCount - obj2.adminUnreadMessageCount + } + + // 如果 obj1.adminPinned 和 obj2.adminPinned 都为 false,比较 adminUnreadMessageCount 的值 + if (!obj1.adminPinned && !obj2.adminPinned) { + return obj1.adminUnreadMessageCount - obj2.adminUnreadMessageCount + } + + // 如果 obj1.adminPinned 为 true,obj2.adminPinned 为 true,且 b 都大于 0,比较 adminUnreadMessageCount 的值 + if ( + obj1.adminPinned && + obj2.adminPinned && + obj1.adminUnreadMessageCount > 0 && + obj2.adminUnreadMessageCount > 0 + ) { + return obj1.adminUnreadMessageCount - obj2.adminUnreadMessageCount + } + + // 如果 obj1.adminPinned 为 false,obj2.adminPinned 为 false,且 b 都大于 0,比较 adminUnreadMessageCount 的值 + if ( + !obj1.adminPinned && + !obj2.adminPinned && + obj1.adminUnreadMessageCount > 0 && + obj2.adminUnreadMessageCount > 0 + ) { + return obj1.adminUnreadMessageCount - obj2.adminUnreadMessageCount + } + + return 0 + }) } - // async setConversationMessageList(conversationId: number) {} } }) -export const useUserStoreWithOut = () => { +export const useMallKefuStoreWithOut = () => { return useMallKefuStore(store) } diff --git a/src/views/mall/promotion/kefu/components/KeFuConversationList.vue b/src/views/mall/promotion/kefu/components/KeFuConversationList.vue index 5e57266f..7afa711e 100644 --- a/src/views/mall/promotion/kefu/components/KeFuConversationList.vue +++ b/src/views/mall/promotion/kefu/components/KeFuConversationList.vue @@ -1,8 +1,10 @@