KeFuConversationBox.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <div class="kefu">
  3. <div
  4. v-for="(item, index) in conversationList"
  5. :key="item.id"
  6. :class="{ active: index === activeConversationIndex }"
  7. class="kefu-conversation flex justify-between items-center"
  8. @click="openRightMessage(item, index)"
  9. >
  10. <div class="flex justify-center items-center w-100%">
  11. <el-avatar :src="item.userAvatar" alt="avatar" />
  12. <div class="ml-10px w-100%">
  13. <div class="flex justify-between items-center w-100%">
  14. <span>{{ item.userNickname }}</span>
  15. <span class="color-[#989EA6]">
  16. {{ formatDate(item.lastMessageTime) }}
  17. </span>
  18. </div>
  19. <!-- 文本消息 -->
  20. <template v-if="KeFuMessageContentTypeEnum.TEXT === item.lastMessageContentType">
  21. <div
  22. v-dompurify-html="replaceEmoji(item.lastMessageContent)"
  23. class="last-message flex items-center color-[#989EA6]"
  24. ></div>
  25. </template>
  26. <!-- 图片消息 -->
  27. <template v-if="KeFuMessageContentTypeEnum.IMAGE === item.lastMessageContentType">
  28. <div class="last-message flex items-center color-[#989EA6]">【图片消息】</div>
  29. </template>
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. </template>
  35. <script lang="ts" setup>
  36. import { KeFuConversationApi, KeFuConversationRespVO } from '@/api/mall/promotion/kefu/conversation'
  37. import { replaceEmoji } from '@/views/mall/promotion/kefu/components/emoji'
  38. import { formatDate, getNowDateTime } from '@/utils/formatTime'
  39. import { KeFuMessageContentTypeEnum } from '@/views/mall/promotion/kefu/components/constants'
  40. defineOptions({ name: 'KeFuConversationBox' })
  41. const activeConversationIndex = ref(-1) // 选中的会话
  42. const conversationList = ref<KeFuConversationRespVO[]>([]) // 会话列表
  43. const getConversationList = async () => {
  44. conversationList.value = await KeFuConversationApi.getConversationList()
  45. // 测试数据
  46. for (let i = 0; i < 5; i++) {
  47. conversationList.value.push({
  48. id: 1,
  49. userId: 283,
  50. userAvatar:
  51. 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKMezSxtOImrC9lbhwHiazYwck3xwrEcO7VJfG6WQo260whaeVNoByE5RreiaGsGfOMlIiaDhSaA991w/132',
  52. userNickname: '辉辉鸭' + i,
  53. lastMessageTime: getNowDateTime(),
  54. lastMessageContent:
  55. '[爱心][爱心]你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇',
  56. lastMessageContentType: 1,
  57. adminPinned: false,
  58. userDeleted: false,
  59. adminDeleted: false,
  60. adminUnreadMessageCount: 19
  61. })
  62. }
  63. }
  64. defineExpose({ getConversationList })
  65. const emits = defineEmits<{
  66. (e: 'change', v: KeFuConversationRespVO): void
  67. }>()
  68. // 打开右侧消息
  69. const openRightMessage = (item: KeFuConversationRespVO, index: number) => {
  70. activeConversationIndex.value = index
  71. emits('change', item)
  72. }
  73. const poller = ref<any>(null) // TODO puhui999: 轮训定时器,暂时模拟 websocket
  74. onMounted(() => {
  75. // TODO puhui999: 轮训相关,功能完善后移除
  76. if (!poller.value) {
  77. poller.value = setInterval(() => {
  78. getConversationList()
  79. }, 1000)
  80. }
  81. })
  82. // TODO puhui999: 轮训相关,功能完善后移除
  83. onBeforeUnmount(() => {
  84. if (!poller.value) {
  85. return
  86. }
  87. clearInterval(poller.value)
  88. })
  89. </script>
  90. <style lang="scss" scoped>
  91. .kefu {
  92. &-conversation {
  93. height: 60px;
  94. padding: 10px;
  95. background-color: #fff;
  96. transition: border-left 0.05s ease-in-out; /* 设置过渡效果 */
  97. .last-message {
  98. width: 200px;
  99. overflow: hidden; // 隐藏超出的文本
  100. white-space: nowrap; // 禁止换行
  101. text-overflow: ellipsis; // 添加省略号
  102. }
  103. }
  104. .active {
  105. border-left: 5px #3271ff solid;
  106. background-color: #eff0f1;
  107. }
  108. }
  109. </style>