messageList.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <template>
  2. <view class="chat-box" :style="{ height: pageHeight + 'px' }">
  3. <scroll-view
  4. :style="{ height: pageHeight + 'px' }"
  5. class="scroll"
  6. :scroll-y="true"
  7. :scroll-top="currentTop"
  8. @scroll="handleScroll"
  9. >
  10. <view ref="innerRef" v-if="refreshContent" style="width: 100%; padding-bottom: 10rpx">
  11. <!-- 消息渲染 -->
  12. <view class="message-item ss-flex-col scroll-item" v-for="(item, index) in getMessageList0" :key="item.id">
  13. <view class="ss-flex ss-row-center ss-col-center">
  14. <!-- 日期 -->
  15. <view v-if="item.contentType !== KeFuMessageContentTypeEnum.SYSTEM && showTime(item, index)"
  16. class="date-message">
  17. {{ formatDate(item.createTime) }}
  18. </view>
  19. <!-- 系统消息 -->
  20. <view v-if="item.contentType === KeFuMessageContentTypeEnum.SYSTEM" class="system-message">
  21. {{ item.content }}
  22. </view>
  23. </view>
  24. <!-- 消息体渲染管理员消息和用户消息并左右展示 -->
  25. <view
  26. v-if="item.contentType !== KeFuMessageContentTypeEnum.SYSTEM"
  27. class="ss-flex ss-col-top"
  28. :class="[
  29. item.senderType === UserTypeEnum.ADMIN
  30. ? `ss-row-left`
  31. : item.senderType === UserTypeEnum.MEMBER
  32. ? `ss-row-right`
  33. : '',
  34. ]"
  35. >
  36. <!-- 客服头像 -->
  37. <image
  38. v-show="item.senderType === UserTypeEnum.ADMIN"
  39. class="chat-avatar ss-m-r-24"
  40. :src="
  41. sheep.$url.cdn(item.senderAvatar) ||
  42. sheep.$url.static('/static/img/shop/chat/default.png')
  43. "
  44. mode="aspectFill"
  45. ></image>
  46. <!-- 内容 -->
  47. <template v-if="item.contentType === KeFuMessageContentTypeEnum.TEXT">
  48. <view class="message-box" :class="{'admin': item.senderType === UserTypeEnum.ADMIN}">
  49. <mp-html :content="replaceEmoji(item.content)" />
  50. </view>
  51. </template>
  52. <template v-if="item.contentType === KeFuMessageContentTypeEnum.IMAGE">
  53. <view class="message-box" :class="{'admin': item.senderType === UserTypeEnum.ADMIN}"
  54. :style="{ width: '200rpx' }">
  55. <su-image
  56. class="message-img"
  57. isPreview
  58. :previewList="[sheep.$url.cdn(item.content)]"
  59. :current="0"
  60. :src="sheep.$url.cdn(item.content)"
  61. :height="200"
  62. :width="200"
  63. mode="aspectFill"
  64. ></su-image>
  65. </view>
  66. </template>
  67. <template v-if="item.contentType === KeFuMessageContentTypeEnum.PRODUCT">
  68. <GoodsItem
  69. :goodsData="getMessageContent(item)"
  70. @tap="
  71. sheep.$router.go('/pages/goods/index', {
  72. id: getMessageContent(item).id,
  73. })
  74. "
  75. />
  76. </template>
  77. <template v-if="item.contentType === KeFuMessageContentTypeEnum.ORDER">
  78. <OrderItem
  79. :orderData="getMessageContent(item)"
  80. @tap="
  81. sheep.$router.go('/pages/order/detail', {
  82. id: getMessageContent(item).id,
  83. })
  84. "
  85. />
  86. </template>
  87. <!-- user头像 -->
  88. <image
  89. v-if="item.senderType === UserTypeEnum.MEMBER"
  90. class="chat-avatar ss-m-l-24"
  91. :src="sheep.$url.cdn(item.senderAvatar) ||
  92. sheep.$url.static('/static/img/shop/chat/default.png')"
  93. mode="aspectFill"
  94. >
  95. </image>
  96. </view>
  97. </view>
  98. </view>
  99. </scroll-view>
  100. <!-- 查看最新消息 -->
  101. <view
  102. v-if="showNewMessageTip"
  103. class="newMessageTip"
  104. @click="handleToNewMessage"
  105. >
  106. <text>有新消息</text>
  107. </view>
  108. </view>
  109. </template>
  110. <script setup>
  111. import { KeFuMessageContentTypeEnum, UserTypeEnum } from '@/pages/chat/util/constants';
  112. import sheep from '@/sheep';
  113. import KeFuApi from '@/sheep/api/promotion/kefu';
  114. import { formatDate } from '@/sheep/util';
  115. import GoodsItem from '@/pages/chat/components/goods.vue';
  116. import OrderItem from '@/pages/chat/components/order.vue';
  117. import { computed, nextTick, reactive, ref, unref } from 'vue';
  118. import dayjs from 'dayjs';
  119. import { emojiList } from '@/pages/chat/util/emoji';
  120. import { isEmpty } from '@/sheep/helper/utils';
  121. const { safeArea } = sheep.$platform.device;
  122. const pageHeight = safeArea.height - 44 - 35 - 50;
  123. const getMessageContent = computed(() => (item) => JSON.parse(item.content)); // 解析消息内容
  124. const messageList = ref([]); // 消息列表
  125. const currentTop = ref(0); // 当前距顶位置
  126. const showNewMessageTip = ref(true); // 显示有新消息提示
  127. const queryParams = reactive({
  128. pageNo: 1,
  129. pageSize: 10,
  130. });
  131. const total = ref(0); // 消息总条数
  132. const refreshContent = ref(false); // 内容刷新,主要解决会话消息页面高度不一致导致的滚动功能精度失效
  133. const skipGetMessageList = computed(() => {
  134. // 已加载到最后一页的话则不触发新的消息获取
  135. return total.value > 0 && Math.ceil(total.value / queryParams.pageSize) === queryParams.pageNo;
  136. }); // 跳过消息获取
  137. /** 按照时间倒序,获取消息列表 */
  138. const getMessageList0 = computed(() => {
  139. messageList.value.sort((a, b) => a.createTime - b.createTime);
  140. return messageList.value;
  141. });
  142. // 获得消息分页列表
  143. const getMessageList = async () => {
  144. const { data } = await KeFuApi.getKefuMessagePage({
  145. pageNo: queryParams.pageNo,
  146. });
  147. if (isEmpty(data.list)) {
  148. return;
  149. }
  150. total.value = data.total;
  151. // 情况一:加载最新消息
  152. if (queryParams.pageNo === 1 && !loadHistory.value) {
  153. messageList.value = data.list;
  154. } else {
  155. // 情况二:加载历史消息
  156. for (const item of data.list) {
  157. if (messageList.value.some((val) => val.id === item.id)) {
  158. continue;
  159. }
  160. messageList.value.push(item);
  161. }
  162. }
  163. refreshContent.value = true;
  164. await scrollToBottom();
  165. };
  166. /** 刷新消息列表 */
  167. const refreshMessageList = async () => {
  168. queryParams.pageNo = 1;
  169. await getMessageList();
  170. if (loadHistory.value) {
  171. // 右下角显示有新消息提示
  172. showNewMessageTip.value = true;
  173. }
  174. };
  175. defineExpose({ getMessageList, refreshMessageList });
  176. /** 滚动到底部 */
  177. const innerRef = ref();
  178. const scrollToBottom = async () => {
  179. // 1. 首次加载时滚动到最新消息,如果加载的是历史消息则不滚动
  180. if (loadHistory.value) {
  181. return;
  182. }
  183. // 2. 滚动到最新消息,关闭新消息提示
  184. await nextTick();
  185. currentTop.value = innerRef.value.$el.clientHeight;
  186. showNewMessageTip.value = false;
  187. };
  188. /** 查看新消息 */
  189. const handleToNewMessage = async () => {
  190. loadHistory.value = false;
  191. await scrollToBottom();
  192. };
  193. /** 加载历史消息 */
  194. const loadHistory = ref(false); // 加载历史消息
  195. const handleScroll = async (event) => {
  196. if (skipGetMessageList.value) {
  197. return;
  198. }
  199. // 触顶自动加载下一页数据
  200. if (event.detail.scrollTop === 0) {
  201. await handleOldMessage();
  202. }
  203. };
  204. const handleOldMessage = async () => {
  205. // 记录已有页面高度
  206. const oldPageHeight = innerRef.value.$el.clientHeight;
  207. if (!oldPageHeight) {
  208. return;
  209. }
  210. loadHistory.value = true;
  211. // 加载消息列表
  212. queryParams.pageNo += 1;
  213. await getMessageList();
  214. // 等页面加载完后,获得上一页最后一条消息的位置,控制滚动到它所在位置
  215. currentTop.value = innerRef.value.$el.clientHeight - oldPageHeight;
  216. };
  217. //======================= 工具 =======================
  218. const showTime = computed(() => (item, index) => {
  219. if (unref(messageList.value)[index + 1]) {
  220. let dateString = dayjs(unref(messageList.value)[index + 1].createTime).fromNow();
  221. return dateString !== dayjs(unref(item).createTime).fromNow();
  222. }
  223. return false;
  224. });
  225. // 处理表情
  226. function replaceEmoji(data) {
  227. let newData = data;
  228. if (typeof newData !== 'object') {
  229. let reg = /\[(.+?)]/g; // [] 中括号
  230. let zhEmojiName = newData.match(reg);
  231. if (zhEmojiName) {
  232. zhEmojiName.forEach((item) => {
  233. let emojiFile = selEmojiFile(item);
  234. newData = newData.replace(
  235. item,
  236. `<img class="chat-img" style="width: 24px;height: 24px;margin: 0 3px;" src="${sheep.$url.cdn(
  237. '/static/img/chat/emoji/' + emojiFile,
  238. )}"/>`,
  239. );
  240. });
  241. }
  242. }
  243. return newData;
  244. }
  245. function selEmojiFile(name) {
  246. for (let index in emojiList) {
  247. if (emojiList[index].name === name) {
  248. return emojiList[index].file;
  249. }
  250. }
  251. return false;
  252. }
  253. </script>
  254. <style scoped lang="scss">
  255. .newMessageTip {
  256. position: absolute;
  257. bottom: 35rpx;
  258. right: 35rpx;
  259. background-color: #fff;
  260. padding: 10rpx;
  261. border-radius: 30rpx;
  262. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 阴影效果 */
  263. }
  264. .chat-box {
  265. padding: 0 20rpx 0;
  266. position: relative;
  267. .message-item {
  268. margin-bottom: 33rpx;
  269. }
  270. .date-message,
  271. .system-message {
  272. width: fit-content;
  273. border-radius: 12rpx;
  274. padding: 8rpx 16rpx;
  275. margin-bottom: 16rpx;
  276. background-color: var(--ui-BG-3);
  277. color: #999;
  278. font-size: 24rpx;
  279. }
  280. .chat-avatar {
  281. width: 70rpx;
  282. height: 70rpx;
  283. border-radius: 50%;
  284. }
  285. .send-status {
  286. color: #333;
  287. height: 80rpx;
  288. margin-right: 8rpx;
  289. display: flex;
  290. align-items: center;
  291. .loading {
  292. width: 32rpx;
  293. height: 32rpx;
  294. -webkit-animation: rotating 2s linear infinite;
  295. animation: rotating 2s linear infinite;
  296. @-webkit-keyframes rotating {
  297. 0% {
  298. transform: rotateZ(0);
  299. }
  300. 100% {
  301. transform: rotateZ(360deg);
  302. }
  303. }
  304. @keyframes rotating {
  305. 0% {
  306. transform: rotateZ(0);
  307. }
  308. 100% {
  309. transform: rotateZ(360deg);
  310. }
  311. }
  312. }
  313. .warning {
  314. width: 32rpx;
  315. height: 32rpx;
  316. color: #ff3000;
  317. }
  318. }
  319. .message-box {
  320. max-width: 50%;
  321. font-size: 16px;
  322. line-height: 20px;
  323. white-space: normal;
  324. word-break: break-all;
  325. word-wrap: break-word;
  326. padding: 20rpx;
  327. border-radius: 10rpx;
  328. color: #fff;
  329. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  330. &.admin {
  331. background: #fff;
  332. color: #333;
  333. }
  334. :deep() {
  335. .imgred {
  336. width: 100%;
  337. }
  338. .imgred,
  339. img {
  340. width: 100%;
  341. }
  342. }
  343. }
  344. :deep() {
  345. .goods,
  346. .order {
  347. max-width: 500rpx;
  348. }
  349. }
  350. .message-img {
  351. width: 100px;
  352. height: 100px;
  353. border-radius: 6rpx;
  354. }
  355. .template-wrap {
  356. // width: 100%;
  357. padding: 20rpx 24rpx;
  358. background: #fff;
  359. border-radius: 10rpx;
  360. .title {
  361. font-size: 26rpx;
  362. font-weight: 500;
  363. color: #333;
  364. margin-bottom: 29rpx;
  365. }
  366. .item {
  367. font-size: 24rpx;
  368. color: var(--ui-BG-Main);
  369. margin-bottom: 16rpx;
  370. &:last-of-type {
  371. margin-bottom: 0;
  372. }
  373. }
  374. }
  375. .error-img {
  376. width: 400rpx;
  377. height: 400rpx;
  378. }
  379. #scrollBottom {
  380. height: 120rpx;
  381. }
  382. }
  383. </style>