kefu.ts 3.0 KB

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