KeFuConversationBox.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 { useEmoji } from './tools/emoji'
  38. import { formatDate } from '@/utils/formatTime'
  39. import { KeFuMessageContentTypeEnum } from './tools/constants'
  40. defineOptions({ name: 'KeFuConversationBox' })
  41. const { replaceEmoji } = useEmoji()
  42. const activeConversationIndex = ref(-1) // 选中的会话
  43. const conversationList = ref<KeFuConversationRespVO[]>([]) // 会话列表
  44. const getConversationList = async () => {
  45. conversationList.value = await KeFuConversationApi.getConversationList()
  46. }
  47. defineExpose({ getConversationList })
  48. const emits = defineEmits<{
  49. (e: 'change', v: KeFuConversationRespVO): void
  50. }>()
  51. // 打开右侧消息
  52. const openRightMessage = (item: KeFuConversationRespVO, index: number) => {
  53. activeConversationIndex.value = index
  54. emits('change', item)
  55. }
  56. </script>
  57. <style lang="scss" scoped>
  58. .kefu {
  59. &-conversation {
  60. height: 60px;
  61. padding: 10px;
  62. background-color: #fff;
  63. transition: border-left 0.05s ease-in-out; /* 设置过渡效果 */
  64. .last-message {
  65. width: 200px;
  66. overflow: hidden; // 隐藏超出的文本
  67. white-space: nowrap; // 禁止换行
  68. text-overflow: ellipsis; // 添加省略号
  69. }
  70. }
  71. .active {
  72. border-left: 5px #3271ff solid;
  73. background-color: #eff0f1;
  74. }
  75. }
  76. </style>