KeFuConversationList.vue 8.0 KB

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