messageList.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <template>
  2. <!-- 聊天虚拟列表 -->
  3. <z-paging ref="pagingRef" v-model="messageList" use-chat-record-mode use-virtual-list cell-height-mode="dynamic"
  4. default-page-size="20" :auto-clean-list-when-reload="false" safe-area-inset-bottom bottom-bg-color="#f8f8f8"
  5. :back-to-top-style="backToTopStyle" :auto-show-back-to-top="showNewMessageTip" @backToTopClick="onBackToTopClick"
  6. @scrolltoupper="onScrollToUpper" @query="queryList">
  7. <template #top>
  8. <!-- 撑一下顶部导航 -->
  9. <view :style="{ height: sys_navBar + 'px' }"></view>
  10. </template>
  11. <!-- style="transform: scaleY(-1)"必须写,否则会导致列表倒置!!! -->
  12. <!-- 注意不要直接在chat-item组件标签上设置style,因为在微信小程序中是无效的,请包一层view -->
  13. <template #cell="{ item, index }">
  14. <view style="transform: scaleY(-1)">
  15. <!-- 消息渲染 -->
  16. <MessageListItem :message="item" :message-index="index" :message-list="messageList"></MessageListItem>
  17. </view>
  18. </template>
  19. <!-- 底部聊天输入框 -->
  20. <template #bottom>
  21. <slot name="bottom"></slot>
  22. </template>
  23. <!-- 查看最新消息 -->
  24. <template #backToTop>
  25. <text>有新消息</text>
  26. </template>
  27. </z-paging>
  28. </template>
  29. <script setup>
  30. import MessageListItem from '@/pages/chat/components/messageListItem.vue';
  31. import { reactive, ref } from 'vue';
  32. import KeFuApi from '@/sheep/api/promotion/kefu';
  33. import { isEmpty } from '@/sheep/helper/utils';
  34. import sheep from '@/sheep';
  35. import { formatDate } from '@/sheep/util';
  36. import { onLoad } from '@dcloudio/uni-app';
  37. const sys_navBar = sheep.$platform.navbar;
  38. const messageList = ref([]); // 消息列表
  39. const showNewMessageTip = ref(false); // 显示有新消息提示
  40. const refreshMessage = ref(false); // 更新消息列表
  41. const backToTopStyle = reactive({
  42. width: '100px',
  43. 'background-color': '#fff',
  44. 'border-radius': '30px',
  45. 'box-shadow': '0 2px 4px rgba(0, 0, 0, 0.1)',
  46. display: 'flex',
  47. justifyContent: 'center',
  48. alignItems: 'center',
  49. }); // 返回顶部样式
  50. const queryParams = reactive({
  51. no: 1, // 查询次数,只用于触底计算
  52. limit: 20,
  53. createTime: undefined,
  54. });
  55. onLoad((options) => {
  56. if (options.conversationId) {
  57. queryParams.conversationId = options.conversationId
  58. } else {
  59. queryParams.conversationId = '1'
  60. }
  61. })
  62. const pagingRef = ref(null); // 虚拟列表
  63. const queryList = async (no, limit) => {
  64. // 组件加载时会自动触发此方法,因此默认页面加载时会自动触发,无需手动调用
  65. queryParams.no = no;
  66. queryParams.limit = limit;
  67. await getMessageList();
  68. };
  69. // 获得消息分页列表
  70. const getMessageList = async () => {
  71. const { data } = await KeFuApi.getKefuMessageList(queryParams);
  72. if (isEmpty(data)) {
  73. pagingRef.value.completeByNoMore([], true);
  74. return;
  75. }
  76. if (queryParams.no > 1 && refreshMessage.value) {
  77. const newMessageList = [];
  78. for (const message of data) {
  79. if (messageList.value.some((val) => val.id === message.id)) {
  80. continue;
  81. }
  82. newMessageList.push(message);
  83. }
  84. // 新消息追加到开头
  85. messageList.value = [...newMessageList, ...messageList.value];
  86. pagingRef.value.updateCache(); // 更新缓存
  87. refreshMessage.value = false; // 更新好后重置状态
  88. return;
  89. }
  90. if (data.slice(-1).length > 0) {
  91. // 设置最后一次历史查询的最后一条消息的 createTime
  92. queryParams.createTime = formatDate(data.slice(-1)[0].createTime);
  93. }
  94. pagingRef.value.completeByNoMore(data, false);
  95. };
  96. /** 刷新消息列表 */
  97. const refreshMessageList = async (message = undefined) => {
  98. if (typeof message !== 'undefined') {
  99. // 追加数据
  100. pagingRef.value.addChatRecordData([message], false);
  101. } else {
  102. queryParams.createTime = undefined;
  103. refreshMessage.value = true;
  104. await getMessageList();
  105. }
  106. // 若已是第一页则不做处理
  107. if (queryParams.no > 1) {
  108. showNewMessageTip.value = true;
  109. } else {
  110. onScrollToUpper();
  111. }
  112. };
  113. /** 滚动到最新消息 */
  114. const onBackToTopClick = (event) => {
  115. event(false); // 禁用默认操作
  116. pagingRef.value.scrollToBottom();
  117. };
  118. /** 监听滚动到底部事件(因为 scroll 翻转了顶就是底) */
  119. const onScrollToUpper = () => {
  120. // 若已是第一页则不做处理
  121. if (queryParams.no === 1) {
  122. return;
  123. }
  124. showNewMessageTip.value = false;
  125. };
  126. // 暴露方法 修改消息列表指定的消息
  127. const updateMessage = (items, messageId = '') => {
  128. messageList.value = messageList.value.map((item) => {
  129. if (item.ids == 1) {
  130. delete item.ids
  131. item.content = items.content
  132. item.messageId = items.messageId
  133. item.isAi = true
  134. return item;
  135. } else {
  136. if (messageId == "") {
  137. if (item.messageId == items.messageId) {
  138. item.content += items.content
  139. }
  140. } else {
  141. if (item.messageId == messageId) {
  142. item.isAi = false
  143. }
  144. }
  145. return item;
  146. }
  147. });
  148. }
  149. // 通过id 获取对应的头像
  150. const getAvatar = (id) => {
  151. return messageList.value[messageList.value.length - 1]
  152. }
  153. defineExpose({ getMessageList, refreshMessageList, updateMessage, messageList, getAvatar });
  154. </script>