KeFuConversationList.vue 7.4 KB

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