KeFuMessageList.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <template>
  2. <el-container v-if="showKeFuMessageList" class="kefu">
  3. <el-header>
  4. <div class="kefu-title">{{ conversation.userNickname }}</div>
  5. </el-header>
  6. <el-main class="kefu-content overflow-visible">
  7. <el-scrollbar ref="scrollbarRef" always height="calc(100vh - 495px)" @scroll="handleScroll">
  8. <div v-if="refreshContent" ref="innerRef" class="w-[100%] pb-3px">
  9. <!-- 消息列表 -->
  10. <div v-for="(item, index) in getMessageList0" :key="item.id" class="w-[100%]">
  11. <div class="flex justify-center items-center mb-20px">
  12. <!-- 日期 -->
  13. <div
  14. v-if="
  15. item.contentType !== KeFuMessageContentTypeEnum.SYSTEM && showTime(item, index)
  16. "
  17. class="date-message"
  18. >
  19. {{ formatDate(item.createTime) }}
  20. </div>
  21. <!-- 系统消息 -->
  22. <div
  23. v-if="item.contentType === KeFuMessageContentTypeEnum.SYSTEM"
  24. class="system-message"
  25. >
  26. {{ item.content }}
  27. </div>
  28. </div>
  29. <div
  30. :class="[
  31. item.senderType === UserTypeEnum.MEMBER
  32. ? `ss-row-left`
  33. : item.senderType === UserTypeEnum.ADMIN
  34. ? `ss-row-right`
  35. : ''
  36. ]"
  37. class="flex mb-20px w-[100%]"
  38. >
  39. <el-avatar
  40. v-if="item.senderType === UserTypeEnum.MEMBER"
  41. :src="conversation.userAvatar"
  42. alt="avatar"
  43. />
  44. <div
  45. :class="{ 'kefu-message': KeFuMessageContentTypeEnum.TEXT === item.contentType }"
  46. class="p-10px"
  47. >
  48. <!-- 文本消息 -->
  49. <TextMessageItem :message="item" />
  50. <!-- 图片消息 -->
  51. <ImageMessageItem :message="item" />
  52. <!-- 商品消息 -->
  53. <ProductMessageItem :message="item" />
  54. <!-- 订单消息 -->
  55. <OrderMessageItem :message="item" />
  56. </div>
  57. <el-avatar
  58. v-if="item.senderType === UserTypeEnum.ADMIN"
  59. :src="item.senderAvatar"
  60. alt="avatar"
  61. />
  62. </div>
  63. </div>
  64. </div>
  65. </el-scrollbar>
  66. <div
  67. v-show="showNewMessageTip"
  68. class="newMessageTip flex items-center cursor-pointer"
  69. @click="handleToNewMessage"
  70. >
  71. <span>有新消息</span>
  72. <Icon class="ml-5px" icon="ep:bottom" />
  73. </div>
  74. </el-main>
  75. <el-footer height="230px">
  76. <div class="h-[100%]">
  77. <div class="chat-tools flex items-center">
  78. <EmojiSelectPopover @select-emoji="handleEmojiSelect" />
  79. <PictureSelectUpload
  80. class="ml-15px mt-3px cursor-pointer"
  81. @send-picture="handleSendPicture"
  82. />
  83. </div>
  84. <el-input v-model="message" :rows="6" style="border-style: none" type="textarea" />
  85. <div class="h-45px flex justify-end">
  86. <el-button class="mt-10px" type="primary" @click="handleSendMessage">发送</el-button>
  87. </div>
  88. </div>
  89. </el-footer>
  90. </el-container>
  91. <el-empty v-else description="请选择左侧的一个会话后开始" />
  92. </template>
  93. <script lang="ts" setup>
  94. import { ElScrollbar as ElScrollbarType } from 'element-plus'
  95. import { KeFuMessageApi, KeFuMessageRespVO } from '@/api/mall/promotion/kefu/message'
  96. import { KeFuConversationRespVO } from '@/api/mall/promotion/kefu/conversation'
  97. import EmojiSelectPopover from './tools/EmojiSelectPopover.vue'
  98. import PictureSelectUpload from './tools/PictureSelectUpload.vue'
  99. import TextMessageItem from './message/TextMessageItem.vue'
  100. import ImageMessageItem from './message/ImageMessageItem.vue'
  101. import ProductMessageItem from './message/ProductMessageItem.vue'
  102. import OrderMessageItem from './message/OrderMessageItem.vue'
  103. import { Emoji } from './tools/emoji'
  104. import { KeFuMessageContentTypeEnum } from './tools/constants'
  105. import { isEmpty } from '@/utils/is'
  106. import { UserTypeEnum } from '@/utils/constants'
  107. import { formatDate } from '@/utils/formatTime'
  108. import dayjs from 'dayjs'
  109. import relativeTime from 'dayjs/plugin/relativeTime'
  110. dayjs.extend(relativeTime)
  111. defineOptions({ name: 'KeFuMessageList' })
  112. const message = ref('') // 消息弹窗
  113. const messageTool = useMessage()
  114. const messageList = ref<KeFuMessageRespVO[]>([]) // 消息列表
  115. const conversation = ref<KeFuConversationRespVO>({} as KeFuConversationRespVO) // 用户会话
  116. const showNewMessageTip = ref(false) // 显示有新消息提示
  117. const queryParams = reactive({
  118. pageNo: 1,
  119. pageSize: 10,
  120. conversationId: 0
  121. })
  122. const total = ref(0) // 消息总条数
  123. const refreshContent = ref(false) // 内容刷新,主要解决会话消息页面高度不一致导致的滚动功能精度失效
  124. /** 获得消息列表 */
  125. const getMessageList = async (val: KeFuConversationRespVO, conversationChange: boolean) => {
  126. // 会话切换,重置相关参数
  127. if (conversationChange) {
  128. queryParams.pageNo = 1
  129. messageList.value = []
  130. total.value = 0
  131. loadHistory.value = false
  132. refreshContent.value = false
  133. }
  134. conversation.value = val
  135. queryParams.conversationId = val.id
  136. const res = await KeFuMessageApi.getKeFuMessagePage(queryParams)
  137. total.value = res.total
  138. // 情况一:加载最新消息
  139. if (queryParams.pageNo === 1) {
  140. messageList.value = res.list
  141. } else {
  142. // 情况二:加载历史消息
  143. for (const item of res.list) {
  144. if (messageList.value.some((val) => val.id === item.id)) {
  145. continue
  146. }
  147. messageList.value.push(item)
  148. }
  149. }
  150. refreshContent.value = true
  151. await scrollToBottom()
  152. }
  153. /** 按照时间倒序,获取消息列表 */
  154. const getMessageList0 = computed(() => {
  155. messageList.value.sort((a: any, b: any) => a.createTime - b.createTime)
  156. return messageList.value
  157. })
  158. /** 刷新消息列表 */
  159. const refreshMessageList = async () => {
  160. if (!conversation.value) {
  161. return
  162. }
  163. queryParams.pageNo = 1
  164. await getMessageList(conversation.value, false)
  165. if (loadHistory.value) {
  166. // 右下角显示有新消息提示
  167. showNewMessageTip.value = true
  168. }
  169. }
  170. defineExpose({ getMessageList, refreshMessageList })
  171. const showKeFuMessageList = computed(() => !isEmpty(conversation.value)) // 是否显示聊天区域
  172. const skipGetMessageList = computed(() => {
  173. // 已加载到最后一页的话则不触发新的消息获取
  174. return total.value > 0 && Math.ceil(total.value / queryParams.pageSize) === queryParams.pageNo
  175. }) // 跳过消息获取
  176. /** 处理表情选择 */
  177. const handleEmojiSelect = (item: Emoji) => {
  178. message.value += item.name
  179. }
  180. /** 处理图片发送 */
  181. const handleSendPicture = async (picUrl: string) => {
  182. // 组织发送消息
  183. const msg = {
  184. conversationId: conversation.value.id,
  185. contentType: KeFuMessageContentTypeEnum.IMAGE,
  186. content: picUrl
  187. }
  188. await sendMessage(msg)
  189. }
  190. /** 发送文本消息 */
  191. const handleSendMessage = async () => {
  192. // 1. 校验消息是否为空
  193. if (isEmpty(unref(message.value))) {
  194. messageTool.notifyWarning('请输入消息后再发送哦!')
  195. return
  196. }
  197. // 2. 组织发送消息
  198. const msg = {
  199. conversationId: conversation.value.id,
  200. contentType: KeFuMessageContentTypeEnum.TEXT,
  201. content: message.value
  202. }
  203. await sendMessage(msg)
  204. }
  205. /** 真正发送消息 【共用】*/
  206. const sendMessage = async (msg: any) => {
  207. // 发送消息
  208. await KeFuMessageApi.sendKeFuMessage(msg)
  209. message.value = ''
  210. // 加载消息列表
  211. await getMessageList(conversation.value, false)
  212. // 滚动到最新消息处
  213. await scrollToBottom()
  214. }
  215. /** 滚动到底部 */
  216. const innerRef = ref<HTMLDivElement>()
  217. const scrollbarRef = ref<InstanceType<typeof ElScrollbarType>>()
  218. const scrollToBottom = async () => {
  219. // 1. 首次加载时滚动到最新消息,如果加载的是历史消息则不滚动
  220. if (loadHistory.value) {
  221. return
  222. }
  223. // 2.1 滚动到最新消息,关闭新消息提示
  224. await nextTick()
  225. scrollbarRef.value!.setScrollTop(innerRef.value!.clientHeight)
  226. showNewMessageTip.value = false
  227. // 2.2 消息已读
  228. await KeFuMessageApi.updateKeFuMessageReadStatus(conversation.value.id)
  229. }
  230. /** 查看新消息 */
  231. const handleToNewMessage = async () => {
  232. loadHistory.value = false
  233. await scrollToBottom()
  234. }
  235. /** 加载历史消息 */
  236. const loadHistory = ref(false) // 加载历史消息
  237. const handleScroll = async ({ scrollTop }) => {
  238. if (skipGetMessageList.value) {
  239. return
  240. }
  241. // 触顶自动加载下一页数据
  242. if (scrollTop === 0) {
  243. await handleOldMessage()
  244. }
  245. }
  246. const handleOldMessage = async () => {
  247. // 记录已有页面高度
  248. const oldPageHeight = innerRef.value?.clientHeight
  249. if (!oldPageHeight) {
  250. return
  251. }
  252. loadHistory.value = true
  253. // 加载消息列表
  254. queryParams.pageNo += 1
  255. await getMessageList(conversation.value, false)
  256. // 等页面加载完后,获得上一页最后一条消息的位置,控制滚动到它所在位置
  257. scrollbarRef.value!.setScrollTop(innerRef.value!.clientHeight - oldPageHeight)
  258. }
  259. /**
  260. * 是否显示时间
  261. *
  262. * @param {*} item - 数据
  263. * @param {*} index - 索引
  264. */
  265. const showTime = computed(() => (item: KeFuMessageRespVO, index: number) => {
  266. if (unref(messageList.value)[index + 1]) {
  267. let dateString = dayjs(unref(messageList.value)[index + 1].createTime).fromNow()
  268. return dateString !== dayjs(unref(item).createTime).fromNow()
  269. }
  270. return false
  271. })
  272. </script>
  273. <style lang="scss" scoped>
  274. .kefu {
  275. &-title {
  276. border-bottom: #e4e0e0 solid 1px;
  277. height: 60px;
  278. line-height: 60px;
  279. }
  280. &-content {
  281. position: relative;
  282. .newMessageTip {
  283. position: absolute;
  284. bottom: 35px;
  285. right: 35px;
  286. background-color: #fff;
  287. padding: 10px;
  288. border-radius: 30px;
  289. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 阴影效果 */
  290. }
  291. .ss-row-left {
  292. justify-content: flex-start;
  293. .kefu-message {
  294. margin-left: 20px;
  295. position: relative;
  296. &::before {
  297. content: '';
  298. width: 10px;
  299. height: 10px;
  300. left: -19px;
  301. top: calc(50% - 10px);
  302. position: absolute;
  303. border-left: 5px solid transparent;
  304. border-bottom: 5px solid transparent;
  305. border-top: 5px solid transparent;
  306. border-right: 5px solid #ffffff;
  307. }
  308. }
  309. }
  310. .ss-row-right {
  311. justify-content: flex-end;
  312. .kefu-message {
  313. margin-right: 20px;
  314. position: relative;
  315. &::after {
  316. content: '';
  317. width: 10px;
  318. height: 10px;
  319. right: -19px;
  320. top: calc(50% - 10px);
  321. position: absolute;
  322. border-left: 5px solid #ffffff;
  323. border-bottom: 5px solid transparent;
  324. border-top: 5px solid transparent;
  325. border-right: 5px solid transparent;
  326. }
  327. }
  328. }
  329. // 消息气泡
  330. .kefu-message {
  331. color: #333;
  332. border-radius: 5px;
  333. box-shadow: 3px 5px 15px rgba(0, 0, 0, 0.2);
  334. padding: 5px 10px;
  335. width: auto;
  336. max-width: 50%;
  337. text-align: left;
  338. display: inline-block !important;
  339. position: relative;
  340. word-break: break-all;
  341. background-color: #ffffff;
  342. transition: all 0.2s;
  343. &:hover {
  344. transform: scale(1.03);
  345. }
  346. }
  347. .date-message,
  348. .system-message {
  349. width: fit-content;
  350. border-radius: 12rpx;
  351. padding: 8rpx 16rpx;
  352. margin-bottom: 16rpx;
  353. background-color: #e8e8e8;
  354. color: #999;
  355. font-size: 24rpx;
  356. }
  357. }
  358. .chat-tools {
  359. width: 100%;
  360. border: #e4e0e0 solid 1px;
  361. border-radius: 10px;
  362. height: 44px;
  363. }
  364. ::v-deep(textarea) {
  365. resize: none;
  366. }
  367. }
  368. </style>