KeFuConversationList.vue 7.4 KB

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