chatBox1.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <template>
  2. <view class="chat-box" :style="{ height: pageHeight + 'px' }">
  3. <!-- 竖向滚动区域需要设置固定 height -->
  4. <scroll-view
  5. :style="{ height: pageHeight + 'px' }"
  6. scroll-y="true"
  7. :scroll-with-animation="false"
  8. :enable-back-to-top="true"
  9. :scroll-into-view="state.scrollInto"
  10. >
  11. <!-- 消息渲染 -->
  12. <view class="message-item ss-flex-col" v-for="(item, index) in chatList" :key="index">
  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.date) }}
  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. <span
  48. v-if="
  49. item.senderType === UserTypeEnum.MEMBER &&
  50. index == chatList.length - 1 &&
  51. isSendSuccess !== 0
  52. "
  53. class="send-status"
  54. >
  55. <image
  56. v-if="isSendSuccess == -1"
  57. class="loading"
  58. :src="sheep.$url.static('/static/img/shop/chat/loading.png')"
  59. mode="aspectFill"
  60. ></image>
  61. <!-- <image
  62. v-if="chatData.isSendSuccess == 1"
  63. class="warning"
  64. :src="sheep.$url.static('/static/img/shop/chat/warning.png')"
  65. mode="aspectFill"
  66. @click="onAgainSendMessage(item)"
  67. ></image> -->
  68. </span>
  69. <!-- 内容 -->
  70. <template v-if="item.contentType === KeFuMessageContentTypeEnum.TEXT">
  71. <view class="message-box" :class="{'admin': item.senderType === UserTypeEnum.ADMIN}">
  72. <mp-html :content="replaceEmoji(item.content)" />
  73. </view>
  74. </template>
  75. <template v-if="item.contentType === KeFuMessageContentTypeEnum.IMAGE">
  76. <view class="message-box" :class="{'admin': item.senderType === UserTypeEnum.ADMIN}" :style="{ width: '200rpx' }">
  77. <su-image
  78. class="message-img"
  79. isPreview
  80. :previewList="[sheep.$url.cdn(item.content)]"
  81. :current="0"
  82. :src="sheep.$url.cdn(item.content)"
  83. :height="200"
  84. :width="200"
  85. mode="aspectFill"
  86. ></su-image>
  87. </view>
  88. </template>
  89. <template v-if="item.contentType === KeFuMessageContentTypeEnum.PRODUCT">
  90. <GoodsItem
  91. :goodsData="item.content.item"
  92. @tap="
  93. sheep.$router.go('/pages/goods/index', {
  94. id: item.content.item.id,
  95. })
  96. "
  97. />
  98. </template>
  99. <template v-if="item.contentType === KeFuMessageContentTypeEnum.ORDER">
  100. <OrderItem
  101. from="msg"
  102. :orderData="item.content.item"
  103. @tap="
  104. sheep.$router.go('/pages/order/detail', {
  105. id: item.content.item.id,
  106. })
  107. "
  108. />
  109. </template>
  110. <!-- user头像 -->
  111. <image
  112. v-if="item.senderType === UserTypeEnum.MEMBER"
  113. class="chat-avatar ss-m-l-24"
  114. :src="sheep.$url.cdn(item?.senderAvatar) ||
  115. sheep.$url.static('/static/img/shop/chat/default.png')"
  116. mode="aspectFill"
  117. >
  118. </image>
  119. </view>
  120. </view>
  121. <!-- 视图滚动锚点 -->
  122. <view id="scrollBottom"></view>
  123. </scroll-view>
  124. </view>
  125. </template>
  126. <script setup>
  127. import sheep from '@/sheep';
  128. import OrderItem from '@/pages/chat/components/order.vue';
  129. import GoodsItem from '@/pages/chat/components/goods.vue';
  130. import { reactive, ref, unref } from 'vue';
  131. import { formatDate } from '@/sheep/util';
  132. import dayjs from 'dayjs';
  133. import { KeFuMessageContentTypeEnum,UserTypeEnum } from './constants';
  134. import { emojiList } from '@/pages/chat/emoji';
  135. const KEFU_MESSAGE_TYPE = 'kefu_message_type'; // 客服消息类型
  136. const { screenHeight, safeAreaInsets, safeArea, screenWidth } = sheep.$platform.device;
  137. const pageHeight = safeArea.height - 44 - 35 - 50;
  138. const state = reactive({
  139. scrollInto: '',
  140. });
  141. const chatList = [
  142. {
  143. id: 1,
  144. conversationId: 1001,
  145. senderId: 1,
  146. senderType: 1, // UserTypeEnum.MEMBER
  147. receiverId: 2,
  148. receiverType: 1, // UserTypeEnum.MEMBER
  149. contentType: 1, // KeFuMessageContentTypeEnum.TEXT
  150. content: "Hello, how are you?",
  151. readStatus: false
  152. },
  153. {
  154. id: 2,
  155. conversationId: 1001,
  156. senderId: 2,
  157. senderType: 1, // UserTypeEnum.MEMBER
  158. receiverId: 1,
  159. receiverType: 1, // UserTypeEnum.MEMBER
  160. contentType: 1, // KeFuMessageContentTypeEnum.TEXT
  161. content: "I'm good, thanks! [流泪][流泪][流泪][流泪]",
  162. readStatus: false
  163. },
  164. {
  165. id: 3,
  166. conversationId: 1002,
  167. senderId: 3,
  168. senderType: 2, // UserTypeEnum.ADMIN
  169. receiverId: 4,
  170. receiverType: 1, // UserTypeEnum.MEMBER
  171. contentType: 2, // KeFuMessageContentTypeEnum.IMAGE
  172. content: "https://static.iocoder.cn/mall/a79f5d2ea6bf0c3c11b2127332dfe2df.jpg",
  173. readStatus: true
  174. },
  175. {
  176. id: 4,
  177. conversationId: 1002,
  178. senderId: 4,
  179. senderType: 1, // UserTypeEnum.MEMBER
  180. receiverId: 3,
  181. receiverType: 2, // UserTypeEnum.ADMIN
  182. contentType: 1, // KeFuMessageContentTypeEnum.TEXT
  183. content: "This is a text message.",
  184. readStatus: false
  185. },
  186. {
  187. id: 5,
  188. conversationId: 1003,
  189. senderId: 5,
  190. senderType: 1, // UserTypeEnum.MEMBER
  191. receiverId: 6,
  192. receiverType: 1, // UserTypeEnum.MEMBER
  193. contentType: 3, // KeFuMessageContentTypeEnum.VOICE
  194. content: "Voice content here",
  195. readStatus: true
  196. },
  197. {
  198. id: 6,
  199. conversationId: 1003,
  200. senderId: 6,
  201. senderType: 1, // UserTypeEnum.MEMBER
  202. receiverId: 5,
  203. receiverType: 1, // UserTypeEnum.MEMBER
  204. contentType: 1, // KeFuMessageContentTypeEnum.TEXT
  205. content: "Another text message.",
  206. readStatus: false
  207. },
  208. {
  209. id: 7,
  210. conversationId: 1004,
  211. senderId: 7,
  212. senderType: 2, // UserTypeEnum.ADMIN
  213. receiverId: 8,
  214. receiverType: 1, // UserTypeEnum.MEMBER
  215. contentType: 1, // KeFuMessageContentTypeEnum.VIDEO
  216. content: "Video content here",
  217. readStatus: true
  218. },
  219. {
  220. id: 8,
  221. conversationId: 1004,
  222. senderId: 8,
  223. senderType: 1, // UserTypeEnum.MEMBER
  224. receiverId: 7,
  225. receiverType: 2, // UserTypeEnum.ADMIN
  226. contentType: 5, // KeFuMessageContentTypeEnum.SYSTEM
  227. content: "System message content",
  228. readStatus: false
  229. },
  230. {
  231. id: 9,
  232. conversationId: 1005,
  233. senderId: 9,
  234. senderType: 1, // UserTypeEnum.MEMBER
  235. receiverId: 10,
  236. receiverType: 1, // UserTypeEnum.MEMBER
  237. contentType: 10, // KeFuMessageContentTypeEnum.PRODUCT
  238. content: "Product message content",
  239. readStatus: true
  240. },
  241. {
  242. id: 10,
  243. conversationId: 1005,
  244. senderId: 10,
  245. senderType: 1, // UserTypeEnum.MEMBER
  246. receiverId: 9,
  247. receiverType: 1, // UserTypeEnum.MEMBER
  248. contentType: 11, // KeFuMessageContentTypeEnum.ORDER
  249. content: "Order message content",
  250. readStatus: false
  251. }
  252. ];
  253. const isSendSuccess = ref(-1)
  254. //======================= 工具函数 =======================
  255. /**
  256. * 是否显示时间
  257. * @param {*} item - 数据
  258. * @param {*} index - 索引
  259. */
  260. const showTime = (item, index) => {
  261. if (unref(chatList)[index + 1]) {
  262. let dateString = dayjs(unref(chatList)[index + 1].date).fromNow();
  263. return dateString !== dayjs(unref(item).date).fromNow();
  264. }
  265. return false;
  266. };
  267. // 处理表情
  268. function replaceEmoji(data) {
  269. let newData = data;
  270. if (typeof newData !== 'object') {
  271. let reg = /\[(.+?)\]/g; // [] 中括号
  272. let zhEmojiName = newData.match(reg);
  273. if (zhEmojiName) {
  274. zhEmojiName.forEach((item) => {
  275. let emojiFile = selEmojiFile(item);
  276. newData = newData.replace(
  277. item,
  278. `<img class="chat-img" style="width: 24px;height: 24px;margin: 0 3px;" src="${sheep.$url.cdn(
  279. '/static/img/chat/emoji/' + emojiFile,
  280. )}"/>`,
  281. );
  282. });
  283. }
  284. }
  285. return newData;
  286. }
  287. function selEmojiFile(name) {
  288. for (let index in emojiList) {
  289. if (emojiList[index].name === name) {
  290. return emojiList[index].file;
  291. }
  292. }
  293. return false;
  294. }
  295. </script>
  296. <style scoped lang="scss">
  297. .chat-box {
  298. padding: 0 20rpx 0;
  299. .loadmore-btn {
  300. width: 98%;
  301. height: 40px;
  302. font-size: 12px;
  303. color: #8c8c8c;
  304. .loadmore-icon {
  305. transform: rotate(90deg);
  306. }
  307. }
  308. .message-item {
  309. margin-bottom: 33rpx;
  310. }
  311. .date-message,
  312. .system-message {
  313. width: fit-content;
  314. border-radius: 12rpx;
  315. padding: 8rpx 16rpx;
  316. margin-bottom: 16rpx;
  317. background-color: var(--ui-BG-3);
  318. color: #999;
  319. font-size: 24rpx;
  320. }
  321. .chat-avatar {
  322. width: 70rpx;
  323. height: 70rpx;
  324. border-radius: 50%;
  325. }
  326. .send-status {
  327. color: #333;
  328. height: 80rpx;
  329. margin-right: 8rpx;
  330. display: flex;
  331. align-items: center;
  332. .loading {
  333. width: 32rpx;
  334. height: 32rpx;
  335. -webkit-animation: rotating 2s linear infinite;
  336. animation: rotating 2s linear infinite;
  337. @-webkit-keyframes rotating {
  338. 0% {
  339. transform: rotateZ(0);
  340. }
  341. 100% {
  342. transform: rotateZ(360deg);
  343. }
  344. }
  345. @keyframes rotating {
  346. 0% {
  347. transform: rotateZ(0);
  348. }
  349. 100% {
  350. transform: rotateZ(360deg);
  351. }
  352. }
  353. }
  354. .warning {
  355. width: 32rpx;
  356. height: 32rpx;
  357. color: #ff3000;
  358. }
  359. }
  360. .message-box {
  361. max-width: 50%;
  362. font-size: 16px;
  363. line-height: 20px;
  364. // max-width: 500rpx;
  365. white-space: normal;
  366. word-break: break-all;
  367. word-wrap: break-word;
  368. padding: 20rpx;
  369. border-radius: 10rpx;
  370. color: #fff;
  371. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  372. &.admin {
  373. background: #fff;
  374. color: #333;
  375. }
  376. :deep() {
  377. .imgred {
  378. width: 100%;
  379. }
  380. .imgred,
  381. img {
  382. width: 100%;
  383. }
  384. }
  385. }
  386. :deep() {
  387. .goods,
  388. .order {
  389. max-width: 500rpx;
  390. }
  391. }
  392. .message-img {
  393. width: 100px;
  394. height: 100px;
  395. border-radius: 6rpx;
  396. }
  397. .template-wrap {
  398. // width: 100%;
  399. padding: 20rpx 24rpx;
  400. background: #fff;
  401. border-radius: 10rpx;
  402. .title {
  403. font-size: 26rpx;
  404. font-weight: 500;
  405. color: #333;
  406. margin-bottom: 29rpx;
  407. }
  408. .item {
  409. font-size: 24rpx;
  410. color: var(--ui-BG-Main);
  411. margin-bottom: 16rpx;
  412. &:last-of-type {
  413. margin-bottom: 0;
  414. }
  415. }
  416. }
  417. .error-img {
  418. width: 400rpx;
  419. height: 400rpx;
  420. }
  421. #scrollBottom {
  422. height: 120rpx;
  423. }
  424. }
  425. </style>