KeFuChatBox.vue 12 KB

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