KeFuConversationList.vue 7.5 KB

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