chatBox.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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="handle_scroll"
  9. @scrolltolower="upper"
  10. >
  11. <view v-for="(data, index) in scrollData" :key="index" :id="'item-' + index">
  12. <template v-if="includePage(index)">
  13. <!-- 消息渲染 -->
  14. <view class="message-item ss-flex-col scroll-item" v-for="(item, index2) in data" :key="index">
  15. <view class="ss-flex ss-row-center ss-col-center">
  16. <!-- 日期 -->
  17. <view v-if="item.contentType !== KeFuMessageContentTypeEnum.SYSTEM && showTime(item,data, index2)"
  18. class="date-message">
  19. {{ formatDate(item.createTime) }}
  20. </view>
  21. <!-- 系统消息 -->
  22. <view v-if="item.contentType === KeFuMessageContentTypeEnum.SYSTEM" class="system-message">
  23. {{ item.content }}
  24. </view>
  25. </view>
  26. <!-- 消息体渲染管理员消息和用户消息并左右展示 -->
  27. <view
  28. v-if="item.contentType !== KeFuMessageContentTypeEnum.SYSTEM"
  29. class="ss-flex ss-col-top"
  30. :class="[
  31. item.senderType === UserTypeEnum.ADMIN
  32. ? `ss-row-left`
  33. : item.senderType === UserTypeEnum.MEMBER
  34. ? `ss-row-right`
  35. : '',
  36. ]"
  37. >
  38. <!-- 客服头像 -->
  39. <image
  40. v-show="item.senderType === UserTypeEnum.ADMIN"
  41. class="chat-avatar ss-m-r-24"
  42. :src="
  43. sheep.$url.cdn(item?.senderAvatar) ||
  44. sheep.$url.static('/static/img/shop/chat/default.png')
  45. "
  46. mode="aspectFill"
  47. ></image>
  48. <!-- 发送状态 -->
  49. <span
  50. v-if="
  51. item.senderType === UserTypeEnum.MEMBER &&
  52. index == data.length - 1 &&
  53. isSendSuccess !== 0
  54. "
  55. class="send-status"
  56. >
  57. <image
  58. v-if="isSendSuccess == -1"
  59. class="loading"
  60. :src="sheep.$url.static('/static/img/shop/chat/loading.png')"
  61. mode="aspectFill"
  62. ></image>
  63. <!-- <image
  64. v-if="chatData.isSendSuccess == 1"
  65. class="warning"
  66. :src="sheep.$url.static('/static/img/shop/chat/warning.png')"
  67. mode="aspectFill"
  68. @click="onAgainSendMessage(item)"
  69. ></image> -->
  70. </span>
  71. <!-- 内容 -->
  72. <template v-if="item.contentType === KeFuMessageContentTypeEnum.TEXT">
  73. <view class="message-box" :class="{'admin': item.senderType === UserTypeEnum.ADMIN}">
  74. <mp-html :content="replaceEmoji(item.content)" />
  75. </view>
  76. </template>
  77. <template v-if="item.contentType === KeFuMessageContentTypeEnum.IMAGE">
  78. <view class="message-box" :class="{'admin': item.senderType === UserTypeEnum.ADMIN}"
  79. :style="{ width: '200rpx' }">
  80. <su-image
  81. class="message-img"
  82. isPreview
  83. :previewList="[sheep.$url.cdn(item.content)]"
  84. :current="0"
  85. :src="sheep.$url.cdn(item.content)"
  86. :height="200"
  87. :width="200"
  88. mode="aspectFill"
  89. ></su-image>
  90. </view>
  91. </template>
  92. <!-- <template v-if="item.contentType === KeFuMessageContentTypeEnum.PRODUCT">-->
  93. <!-- <GoodsItem-->
  94. <!-- :goodsData="item.content.item"-->
  95. <!-- @tap="-->
  96. <!-- sheep.$router.go('/pages/goods/index', {-->
  97. <!-- id: item.content.item.id,-->
  98. <!-- })-->
  99. <!-- "-->
  100. <!-- />-->
  101. <!-- </template>-->
  102. <!-- <template v-if="item.contentType === KeFuMessageContentTypeEnum.ORDER">-->
  103. <!-- <OrderItem-->
  104. <!-- from="msg"-->
  105. <!-- :orderData="item.content.item"-->
  106. <!-- @tap="-->
  107. <!-- sheep.$router.go('/pages/order/detail', {-->
  108. <!-- id: item.content.item.id,-->
  109. <!-- })-->
  110. <!-- "-->
  111. <!-- />-->
  112. <!-- </template>-->
  113. <!-- user头像 -->
  114. <image
  115. v-if="item.senderType === UserTypeEnum.MEMBER"
  116. class="chat-avatar ss-m-l-24"
  117. :src="sheep.$url.cdn(item?.senderAvatar) ||
  118. sheep.$url.static('/static/img/shop/chat/default.png')"
  119. mode="aspectFill"
  120. >
  121. </image>
  122. </view>
  123. </view>
  124. </template>
  125. <view v-if="!includePage(index)" :style="{ height: pagesHeight[index] }"></view>
  126. </view>
  127. </scroll-view>
  128. <!-- TODO puhui999: 这里还有一点问题 -->
  129. <view v-show="showGoBottom" class="go-back-btn" @click="handle_goBottom">查看最新消息</view>
  130. </view>
  131. </template>
  132. <script setup>
  133. import { nextTick, reactive, ref, unref } from 'vue';
  134. import { onLoad } from '@dcloudio/uni-app';
  135. import sheep from '@/sheep';
  136. import KeFuApi from '@/sheep/api/promotion/kefu';
  137. import { isEmpty } from '@/sheep/helper/utils';
  138. import { KeFuMessageContentTypeEnum, UserTypeEnum } from '@/pages/chat/components/constants';
  139. import { formatDate } from '@/sheep/util';
  140. import dayjs from 'dayjs';
  141. import { emojiList } from '@/pages/chat/emoji';
  142. const { safeArea } = sheep.$platform.device;
  143. const pageHeight = safeArea.height - 44 - 35 - 50;
  144. const currentShowPage = ref(0); // 当前展示的页码
  145. const pagesHeight = reactive([]); // 记录每个页面的高度
  146. const visiblePagesList = ref([-1, 0, 1]);
  147. const scrollData = ref([]);
  148. // 向上滚动
  149. const upper = async () => {
  150. // 页数据满十条后加载下一页
  151. if (currentShowPage.value === 0 || scrollData.value[currentShowPage.value - 1].length === 10) {
  152. currentShowPage.value += 1;
  153. }
  154. await getMessageList();
  155. await nextTick();
  156. setPageHeight();
  157. observer(currentShowPage.value);
  158. };
  159. // 获得消息分页列表
  160. const getMessageList = async (pageNo = undefined) => {
  161. const { data } = await KeFuApi.getKefuMessagePage({
  162. pageNo: pageNo || currentShowPage.value,
  163. });
  164. if (isEmpty(data.list)) {
  165. return;
  166. }
  167. scrollData.value[pageNo ? pageNo - 1 : currentShowPage.value - 1] = data.list;
  168. };
  169. defineExpose({ getMessageList });
  170. const scrollTop = ref(0); // 当前滚动区域距离顶部的距离
  171. const currentTop = ref(0);
  172. const showGoBottom = ref(false);
  173. // 滚动条滚动时触发
  174. const handle_scroll = throttle(event => {
  175. scrollTop.value = event[0].detail.scrollTop;
  176. if (scrollTop > 300) {
  177. showGoBottom.value = true;
  178. }
  179. }, 100);
  180. const handle_goBottom = () => {
  181. currentTop.value = scrollTop.value;
  182. nextTick(() => {
  183. currentTop.value = 0;
  184. });
  185. showGoBottom.value = false;
  186. };
  187. // 获取每页数据的页面高度
  188. const setPageHeight = () => {
  189. let query = uni.createSelectorQuery();
  190. query
  191. .select(`#item-${currentShowPage.value}`)
  192. .boundingClientRect(res => {
  193. console.log(res);
  194. pagesHeight[currentShowPage.value] = res && res.height;
  195. })
  196. .exec();
  197. };
  198. const observer = pageNum => {
  199. const observeView = wx
  200. .createIntersectionObserver()
  201. .relativeTo('#scroll', { top: 0, bottom: 0 });
  202. observeView.observe(`#item-${pageNum}`, res => {
  203. if (res.intersectionRatio > 0) visiblePagesList.value = [pageNum - 1, pageNum, pageNum + 1];
  204. });
  205. };
  206. // 虚拟列表展示可视区域的数据
  207. const includePage = index => {
  208. return visiblePagesList.value.indexOf(index) > -1;
  209. };
  210. // 防抖
  211. function throttle(fnc, delay) {
  212. let timer;
  213. return function() {
  214. let _this = this;
  215. let args = arguments;
  216. if (!timer) {
  217. timer = setTimeout(() => {
  218. fnc.call(_this, args);
  219. timer = null;
  220. }, delay);
  221. }
  222. };
  223. }
  224. const isSendSuccess = ref(0); // 是否发送成功 -1=发送中|0=发送成功|1发送失败
  225. onLoad(() => {
  226. setPageHeight();
  227. observer(currentShowPage.value);
  228. });
  229. //======================= 工具函数 =======================
  230. /**
  231. * 是否显示时间
  232. * @param {*} item - 数据
  233. * @param {*} index - 索引
  234. */
  235. const showTime = (item, data, index) => {
  236. if (unref(data)[index + 1]) {
  237. let dateString = dayjs(unref(data)[index + 1].createTime).fromNow();
  238. return dateString !== dayjs(unref(item).createTime).fromNow();
  239. }
  240. return false;
  241. };
  242. // 处理表情
  243. function replaceEmoji(data) {
  244. let newData = data;
  245. if (typeof newData !== 'object') {
  246. let reg = /\[(.+?)\]/g; // [] 中括号
  247. let zhEmojiName = newData.match(reg);
  248. if (zhEmojiName) {
  249. zhEmojiName.forEach((item) => {
  250. let emojiFile = selEmojiFile(item);
  251. newData = newData.replace(
  252. item,
  253. `<img class="chat-img" style="width: 24px;height: 24px;margin: 0 3px;" src="${sheep.$url.cdn(
  254. '/static/img/chat/emoji/' + emojiFile,
  255. )}"/>`,
  256. );
  257. });
  258. }
  259. }
  260. return newData;
  261. }
  262. function selEmojiFile(name) {
  263. for (let index in emojiList) {
  264. if (emojiList[index].name === name) {
  265. return emojiList[index].file;
  266. }
  267. }
  268. return false;
  269. }
  270. </script>
  271. <style lang="scss" scoped>
  272. // scroll-view 倒置,也就是说顶部是底,底部才是顶,下拉加载旧数据,回到底部就是新数据
  273. .scroll {
  274. transform: rotate(180deg);
  275. & ::-webkit-scrollbar {
  276. display: none;
  277. width: 0;
  278. height: 0;
  279. color: transparent;
  280. }
  281. }
  282. // 内容区域也翻转一下
  283. .scroll-item {
  284. transform: rotate(180deg);
  285. }
  286. .go-back-btn {
  287. width: 100rpx;
  288. height: 60rpx;
  289. line-height: 60rpx;
  290. position: fixed;
  291. bottom: 100rpx;
  292. left: 80%;
  293. transform: translateX(-50%);
  294. text-align: center;
  295. background-color: lightblue;
  296. border-radius: 10rpx;
  297. }
  298. .chat-box {
  299. padding: 0 20rpx 0;
  300. .loadmore-btn {
  301. width: 98%;
  302. height: 40px;
  303. font-size: 12px;
  304. color: #8c8c8c;
  305. .loadmore-icon {
  306. transform: rotate(90deg);
  307. }
  308. }
  309. .message-item {
  310. margin-bottom: 33rpx;
  311. }
  312. .date-message,
  313. .system-message {
  314. width: fit-content;
  315. border-radius: 12rpx;
  316. padding: 8rpx 16rpx;
  317. margin-bottom: 16rpx;
  318. background-color: var(--ui-BG-3);
  319. color: #999;
  320. font-size: 24rpx;
  321. }
  322. .chat-avatar {
  323. width: 70rpx;
  324. height: 70rpx;
  325. border-radius: 50%;
  326. }
  327. .send-status {
  328. color: #333;
  329. height: 80rpx;
  330. margin-right: 8rpx;
  331. display: flex;
  332. align-items: center;
  333. .loading {
  334. width: 32rpx;
  335. height: 32rpx;
  336. -webkit-animation: rotating 2s linear infinite;
  337. animation: rotating 2s linear infinite;
  338. @-webkit-keyframes rotating {
  339. 0% {
  340. transform: rotateZ(0);
  341. }
  342. 100% {
  343. transform: rotateZ(360deg);
  344. }
  345. }
  346. @keyframes rotating {
  347. 0% {
  348. transform: rotateZ(0);
  349. }
  350. 100% {
  351. transform: rotateZ(360deg);
  352. }
  353. }
  354. }
  355. .warning {
  356. width: 32rpx;
  357. height: 32rpx;
  358. color: #ff3000;
  359. }
  360. }
  361. .message-box {
  362. max-width: 50%;
  363. font-size: 16px;
  364. line-height: 20px;
  365. // max-width: 500rpx;
  366. white-space: normal;
  367. word-break: break-all;
  368. word-wrap: break-word;
  369. padding: 20rpx;
  370. border-radius: 10rpx;
  371. color: #fff;
  372. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  373. &.admin {
  374. background: #fff;
  375. color: #333;
  376. }
  377. :deep() {
  378. .imgred {
  379. width: 100%;
  380. }
  381. .imgred,
  382. img {
  383. width: 100%;
  384. }
  385. }
  386. }
  387. :deep() {
  388. .goods,
  389. .order {
  390. max-width: 500rpx;
  391. }
  392. }
  393. .message-img {
  394. width: 100px;
  395. height: 100px;
  396. border-radius: 6rpx;
  397. }
  398. .template-wrap {
  399. // width: 100%;
  400. padding: 20rpx 24rpx;
  401. background: #fff;
  402. border-radius: 10rpx;
  403. .title {
  404. font-size: 26rpx;
  405. font-weight: 500;
  406. color: #333;
  407. margin-bottom: 29rpx;
  408. }
  409. .item {
  410. font-size: 24rpx;
  411. color: var(--ui-BG-Main);
  412. margin-bottom: 16rpx;
  413. &:last-of-type {
  414. margin-bottom: 0;
  415. }
  416. }
  417. }
  418. .error-img {
  419. width: 400rpx;
  420. height: 400rpx;
  421. }
  422. #scrollBottom {
  423. height: 120rpx;
  424. }
  425. }
  426. </style>