KeFuMessageList.vue 12 KB

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