KeFuConversationList.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <div class="kefu">
  3. <div
  4. v-for="item in conversationList"
  5. :key="item.id"
  6. :class="{ active: item.id === activeConversationId, pinned: item.adminPinned }"
  7. class="kefu-conversation flex items-center"
  8. @click="openRightMessage(item)"
  9. @contextmenu.prevent="rightClick($event as PointerEvent, item)"
  10. >
  11. <div class="flex justify-center items-center w-100%">
  12. <div class="flex justify-center items-center w-50px h-50px">
  13. <!-- 头像 + 未读 -->
  14. <el-badge
  15. :hidden="item.adminUnreadMessageCount === 0"
  16. :max="99"
  17. :value="item.adminUnreadMessageCount"
  18. >
  19. <el-avatar :src="item.userAvatar" alt="avatar" />
  20. </el-badge>
  21. </div>
  22. <div class="ml-10px w-100%">
  23. <div class="flex justify-between items-center w-100%">
  24. <span>{{ item.userNickname }}</span>
  25. <span class="color-[#989EA6]">
  26. {{ formatDate(item.lastMessageTime) }}
  27. </span>
  28. </div>
  29. <!-- 最后聊天内容 -->
  30. <div
  31. v-dompurify-html="
  32. getConversationDisplayText(item.lastMessageContentType, item.lastMessageContent)
  33. "
  34. class="last-message flex items-center color-[#989EA6]"
  35. ></div>
  36. </div>
  37. </div>
  38. </div>
  39. <!-- 右键,进行操作(类似微信) -->
  40. <ul v-show="showRightMenu" :style="rightMenuStyle" class="right-menu-ul">
  41. <li
  42. v-show="!rightClickConversation.adminPinned"
  43. class="flex items-center"
  44. @click.stop="updateConversationPinned(true)"
  45. >
  46. <Icon class="mr-5px" icon="ep:top" />
  47. 置顶会话
  48. </li>
  49. <li
  50. v-show="rightClickConversation.adminPinned"
  51. class="flex items-center"
  52. @click.stop="updateConversationPinned(false)"
  53. >
  54. <Icon class="mr-5px" icon="ep:bottom" />
  55. 取消置顶
  56. </li>
  57. <li class="flex items-center" @click.stop="deleteConversation">
  58. <Icon class="mr-5px" color="red" icon="ep:delete" />
  59. 删除会话
  60. </li>
  61. <li class="flex items-center" @click.stop="closeRightMenu">
  62. <Icon class="mr-5px" color="red" icon="ep:close" />
  63. 取消
  64. </li>
  65. </ul>
  66. </div>
  67. </template>
  68. <script lang="ts" setup>
  69. import { KeFuConversationApi, KeFuConversationRespVO } from '@/api/mall/promotion/kefu/conversation'
  70. import { useEmoji } from './tools/emoji'
  71. import { formatDate } from '@/utils/formatTime'
  72. import { KeFuMessageContentTypeEnum } from './tools/constants'
  73. defineOptions({ name: 'KeFuConversationBox' })
  74. const message = useMessage() // 消息弹窗
  75. const { replaceEmoji } = useEmoji()
  76. const conversationList = ref<KeFuConversationRespVO[]>([]) // 会话列表
  77. const activeConversationId = ref(-1) // 选中的会话
  78. /** 加载会话列表 */
  79. const getConversationList = async () => {
  80. conversationList.value = await KeFuConversationApi.getConversationList()
  81. }
  82. defineExpose({ getConversationList })
  83. /** 打开右侧的消息列表 */
  84. const emits = defineEmits<{
  85. (e: 'change', v: KeFuConversationRespVO): void
  86. }>()
  87. const openRightMessage = (item: KeFuConversationRespVO) => {
  88. activeConversationId.value = item.id
  89. emits('change', item)
  90. }
  91. /** 获得消息类型 */
  92. const getConversationDisplayText = computed(
  93. () => (lastMessageContentType: number, lastMessageContent: string) => {
  94. switch (lastMessageContentType) {
  95. case KeFuMessageContentTypeEnum.SYSTEM:
  96. return '[系统消息]'
  97. case KeFuMessageContentTypeEnum.VIDEO:
  98. return '[视频消息]'
  99. case KeFuMessageContentTypeEnum.IMAGE:
  100. return '[图片消息]'
  101. case KeFuMessageContentTypeEnum.PRODUCT:
  102. return '[商品消息]'
  103. case KeFuMessageContentTypeEnum.ORDER:
  104. return '[订单消息]'
  105. case KeFuMessageContentTypeEnum.VOICE:
  106. return '[语音消息]'
  107. case KeFuMessageContentTypeEnum.TEXT:
  108. return replaceEmoji(lastMessageContent)
  109. default:
  110. return ''
  111. }
  112. }
  113. )
  114. //======================= 右键菜单 =======================
  115. const showRightMenu = ref(false) // 显示右键菜单
  116. const rightMenuStyle = ref<any>({}) // 右键菜单 Style
  117. const rightClickConversation = ref<KeFuConversationRespVO>({} as KeFuConversationRespVO) // 右键选中的会话对象
  118. /** 打开右键菜单 */
  119. const rightClick = (mouseEvent: PointerEvent, item: KeFuConversationRespVO) => {
  120. rightClickConversation.value = item
  121. // 显示右键菜单
  122. showRightMenu.value = true
  123. rightMenuStyle.value = {
  124. top: mouseEvent.clientY - 110 + 'px',
  125. left: mouseEvent.clientX - 80 + 'px'
  126. }
  127. }
  128. /** 关闭右键菜单 */
  129. const closeRightMenu = () => {
  130. showRightMenu.value = false
  131. }
  132. /** 置顶会话 */
  133. const updateConversationPinned = async (adminPinned: boolean) => {
  134. // 1. 会话置顶/取消置顶
  135. await KeFuConversationApi.updateConversationPinned({
  136. id: rightClickConversation.value.id,
  137. adminPinned
  138. })
  139. message.notifySuccess(adminPinned ? '置顶成功' : '取消置顶成功')
  140. // 2. 关闭右键菜单,更新会话列表
  141. closeRightMenu()
  142. await getConversationList()
  143. }
  144. /** 删除会话 */
  145. const deleteConversation = async () => {
  146. // 1. 删除会话
  147. await message.confirm('您确定要删除该会话吗?')
  148. await KeFuConversationApi.deleteConversation(rightClickConversation.value.id)
  149. // 2. 关闭右键菜单,更新会话列表
  150. closeRightMenu()
  151. await getConversationList()
  152. }
  153. /** 监听右键菜单的显示状态,添加点击事件监听器 */
  154. watch(showRightMenu, (val) => {
  155. if (val) {
  156. document.body.addEventListener('click', closeRightMenu)
  157. } else {
  158. document.body.removeEventListener('click', closeRightMenu)
  159. }
  160. })
  161. </script>
  162. <style lang="scss" scoped>
  163. .kefu {
  164. &-conversation {
  165. height: 60px;
  166. padding: 10px;
  167. background-color: #fff;
  168. transition: border-left 0.05s ease-in-out; /* 设置过渡效果 */
  169. .last-message {
  170. width: 200px;
  171. overflow: hidden; // 隐藏超出的文本
  172. white-space: nowrap; // 禁止换行
  173. text-overflow: ellipsis; // 添加省略号
  174. }
  175. }
  176. .active {
  177. border-left: 5px #3271ff solid;
  178. background-color: #eff0f1;
  179. }
  180. .pinned {
  181. background-color: #eff0f1;
  182. }
  183. .right-menu-ul {
  184. position: absolute;
  185. background-color: #fff;
  186. padding: 10px;
  187. margin: 0;
  188. list-style-type: none; /* 移除默认的项目符号 */
  189. border-radius: 12px;
  190. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 阴影效果 */
  191. width: 130px;
  192. li {
  193. padding: 8px 16px;
  194. cursor: pointer;
  195. border-radius: 12px;
  196. transition: background-color 0.3s; /* 平滑过渡 */
  197. &:hover {
  198. background-color: #e0e0e0; /* 悬停时的背景颜色 */
  199. }
  200. }
  201. }
  202. }
  203. </style>