KeFuConversationList.vue 7.2 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-[#989EA6]">
  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-[#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 { formatPast } from '@/utils/formatTime'
  72. import { KeFuMessageContentTypeEnum } from './tools/constants'
  73. import { useAppStore } from '@/store/modules/app'
  74. defineOptions({ name: 'KeFuConversationList' })
  75. const message = useMessage() // 消息弹窗
  76. const appStore = useAppStore()
  77. const { replaceEmoji } = useEmoji()
  78. const conversationList = ref<KeFuConversationRespVO[]>([]) // 会话列表
  79. const activeConversationId = ref(-1) // 选中的会话
  80. const collapse = computed(() => appStore.getCollapse) // 折叠菜单
  81. /** 加载会话列表 */
  82. const getConversationList = async () => {
  83. const list = await KeFuConversationApi.getConversationList()
  84. list.sort((a: KeFuConversationRespVO, _) => (a.adminPinned ? -1 : 1))
  85. conversationList.value = list
  86. }
  87. defineExpose({ getConversationList })
  88. /** 打开右侧的消息列表 */
  89. const emits = defineEmits<{
  90. (e: 'change', v: KeFuConversationRespVO): void
  91. }>()
  92. const openRightMessage = (item: KeFuConversationRespVO) => {
  93. activeConversationId.value = item.id
  94. emits('change', item)
  95. }
  96. /** 获得消息类型 */
  97. const getConversationDisplayText = computed(
  98. () => (lastMessageContentType: number, lastMessageContent: string) => {
  99. switch (lastMessageContentType) {
  100. case KeFuMessageContentTypeEnum.SYSTEM:
  101. return '[系统消息]'
  102. case KeFuMessageContentTypeEnum.VIDEO:
  103. return '[视频消息]'
  104. case KeFuMessageContentTypeEnum.IMAGE:
  105. return '[图片消息]'
  106. case KeFuMessageContentTypeEnum.PRODUCT:
  107. return '[商品消息]'
  108. case KeFuMessageContentTypeEnum.ORDER:
  109. return '[订单消息]'
  110. case KeFuMessageContentTypeEnum.VOICE:
  111. return '[语音消息]'
  112. case KeFuMessageContentTypeEnum.TEXT:
  113. return replaceEmoji(lastMessageContent)
  114. default:
  115. return ''
  116. }
  117. }
  118. )
  119. //======================= 右键菜单 =======================
  120. const showRightMenu = ref(false) // 显示右键菜单
  121. const rightMenuStyle = ref<any>({}) // 右键菜单 Style
  122. const rightClickConversation = ref<KeFuConversationRespVO>({} as KeFuConversationRespVO) // 右键选中的会话对象
  123. /** 打开右键菜单 */
  124. const rightClick = (mouseEvent: PointerEvent, item: KeFuConversationRespVO) => {
  125. rightClickConversation.value = item
  126. // 显示右键菜单
  127. showRightMenu.value = true
  128. rightMenuStyle.value = {
  129. top: mouseEvent.clientY - 110 + 'px',
  130. left: collapse.value ? mouseEvent.clientX - 80 + 'px' : mouseEvent.clientX - 210 + 'px'
  131. }
  132. }
  133. /** 关闭右键菜单 */
  134. const closeRightMenu = () => {
  135. showRightMenu.value = false
  136. }
  137. /** 置顶会话 */
  138. const updateConversationPinned = async (adminPinned: boolean) => {
  139. // 1. 会话置顶/取消置顶
  140. await KeFuConversationApi.updateConversationPinned({
  141. id: rightClickConversation.value.id,
  142. adminPinned
  143. })
  144. message.notifySuccess(adminPinned ? '置顶成功' : '取消置顶成功')
  145. // 2. 关闭右键菜单,更新会话列表
  146. closeRightMenu()
  147. await getConversationList()
  148. }
  149. /** 删除会话 */
  150. const deleteConversation = async () => {
  151. // 1. 删除会话
  152. await message.confirm('您确定要删除该会话吗?')
  153. await KeFuConversationApi.deleteConversation(rightClickConversation.value.id)
  154. // 2. 关闭右键菜单,更新会话列表
  155. closeRightMenu()
  156. await getConversationList()
  157. }
  158. /** 监听右键菜单的显示状态,添加点击事件监听器 */
  159. watch(showRightMenu, (val) => {
  160. if (val) {
  161. document.body.addEventListener('click', closeRightMenu)
  162. } else {
  163. document.body.removeEventListener('click', closeRightMenu)
  164. }
  165. })
  166. </script>
  167. <style lang="scss" scoped>
  168. .kefu {
  169. &-conversation {
  170. height: 60px;
  171. padding: 10px;
  172. background-color: #fff;
  173. transition: border-left 0.05s ease-in-out; /* 设置过渡效果 */
  174. .username {
  175. min-width: 0;
  176. max-width: 60%;
  177. overflow: hidden;
  178. text-overflow: ellipsis;
  179. display: -webkit-box;
  180. -webkit-box-orient: vertical;
  181. -webkit-line-clamp: 1;
  182. }
  183. .last-message {
  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: #eff0f1;
  193. }
  194. .pinned {
  195. background-color: #eff0f1;
  196. }
  197. .right-menu-ul {
  198. position: absolute;
  199. background-color: #fff;
  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: #e0e0e0; /* 悬停时的背景颜色 */
  213. }
  214. }
  215. }
  216. }
  217. </style>