潘志宝
2025-02-26 b3a43e63d2c2fa854d676676d3f8072c0d943d13
提交 | 用户 | 时间
6bf4f9 1 import { store } from '@/store'
H 2 import { defineStore } from 'pinia'
3 import { KeFuConversationApi, KeFuConversationRespVO } from '@/api/mall/promotion/kefu/conversation'
4 import { KeFuMessageRespVO } from '@/api/mall/promotion/kefu/message'
5 import { isEmpty } from '@/utils/is'
6
7 interface MallKefuInfoVO {
8   conversationList: KeFuConversationRespVO[] // 会话列表
9   conversationMessageList: Map<number, KeFuMessageRespVO[]> // 会话消息
10 }
11
12 export const useMallKefuStore = defineStore('mall-kefu', {
13   state: (): MallKefuInfoVO => ({
14     conversationList: [],
15     conversationMessageList: new Map<number, KeFuMessageRespVO[]>() // key 会话,value 会话消息列表
16   }),
17   getters: {
18     getConversationList(): KeFuConversationRespVO[] {
19       return this.conversationList
20     },
21     getConversationMessageList(): (conversationId: number) => KeFuMessageRespVO[] | undefined {
22       return (conversationId: number) => this.conversationMessageList.get(conversationId)
23     }
24   },
25   actions: {
26     // ======================= 会话消息相关 =======================
27     /** 缓存历史消息 */
28     saveMessageList(conversationId: number, messageList: KeFuMessageRespVO[]) {
29       this.conversationMessageList.set(conversationId, messageList)
30     },
31
32     // ======================= 会话相关 =======================
33     /** 加载会话缓存列表 */
34     async setConversationList() {
35       this.conversationList = await KeFuConversationApi.getConversationList()
36       this.conversationSort()
37     },
38     /** 更新会话缓存已读 */
39     async updateConversationStatus(conversationId: number) {
40       if (isEmpty(this.conversationList)) {
41         return
42       }
43       const conversation = this.conversationList.find((item) => item.id === conversationId)
44       conversation && (conversation.adminUnreadMessageCount = 0)
45     },
46     /** 更新会话缓存 */
47     async updateConversation(conversationId: number) {
48       if (isEmpty(this.conversationList)) {
49         return
50       }
51
52       const conversation = await KeFuConversationApi.getConversation(conversationId)
53       this.deleteConversation(conversationId)
54       conversation && this.conversationList.push(conversation)
55       this.conversationSort()
56     },
57     /** 删除会话缓存 */
58     deleteConversation(conversationId: number) {
59       const index = this.conversationList.findIndex((item) => item.id === conversationId)
60       // 存在则删除
61       if (index > -1) {
62         this.conversationList.splice(index, 1)
63       }
64     },
65     conversationSort() {
66       // 按置顶属性和最后消息时间排序
67       this.conversationList.sort((a, b) => {
68         // 按照置顶排序,置顶的会在前面
69         if (a.adminPinned !== b.adminPinned) {
70           return a.adminPinned ? -1 : 1
71         }
72         // 按照最后消息时间排序,最近的会在前面
73         return (b.lastMessageTime as unknown as number) - (a.lastMessageTime as unknown as number)
74       })
75     }
76   }
77 })
78
79 export const useMallKefuStoreWithOut = () => {
80   return useMallKefuStore(store)
81 }