123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- <template>
- <view class="chat-box" :style="{ height: pageHeight + 'px' }">
- <!-- 竖向滚动区域需要设置固定 height -->
- <scroll-view
- :style="{ height: pageHeight + 'px' }"
- scroll-y="true"
- :scroll-with-animation="false"
- :enable-back-to-top="true"
- :scroll-into-view="state.scrollInto"
- >
- <!-- 消息渲染 -->
- <view class="message-item ss-flex-col" v-for="(item, index) in chatList" :key="index">
- <view class="ss-flex ss-row-center ss-col-center">
- <!-- 日期 -->
- <view v-if="item.contentType !== KeFuMessageContentTypeEnum.SYSTEM && showTime(item, index)"
- class="date-message">
- {{ formatDate(item.date) }}
- </view>
- <!-- 系统消息 -->
- <view v-if="item.contentType === KeFuMessageContentTypeEnum.SYSTEM" class="system-message">
- {{ item.content }}
- </view>
- </view>
- <!-- 消息体渲染管理员消息和用户消息并左右展示 -->
- <view
- v-if="item.contentType !== KeFuMessageContentTypeEnum.SYSTEM"
- class="ss-flex ss-col-top"
- :class="[
- item.senderType === UserTypeEnum.ADMIN
- ? `ss-row-left`
- : item.senderType === UserTypeEnum.MEMBER
- ? `ss-row-right`
- : '',
- ]"
- >
- <!-- 客服头像 -->
- <image
- v-show="item.senderType === UserTypeEnum.ADMIN"
- class="chat-avatar ss-m-r-24"
- :src="
- sheep.$url.cdn(item?.senderAvatar) ||
- sheep.$url.static('/static/img/shop/chat/default.png')
- "
- mode="aspectFill"
- ></image>
- <!-- 发送状态 -->
- <span
- v-if="
- item.senderType === UserTypeEnum.MEMBER &&
- index == chatList.length - 1 &&
- isSendSuccess !== 0
- "
- class="send-status"
- >
- <image
- v-if="isSendSuccess == -1"
- class="loading"
- :src="sheep.$url.static('/static/img/shop/chat/loading.png')"
- mode="aspectFill"
- ></image>
- <!-- <image
- v-if="chatData.isSendSuccess == 1"
- class="warning"
- :src="sheep.$url.static('/static/img/shop/chat/warning.png')"
- mode="aspectFill"
- @click="onAgainSendMessage(item)"
- ></image> -->
- </span>
- <!-- 内容 -->
- <template v-if="item.contentType === KeFuMessageContentTypeEnum.TEXT">
- <view class="message-box" :class="{'admin': item.senderType === UserTypeEnum.ADMIN}">
- <mp-html :content="replaceEmoji(item.content)" />
- </view>
- </template>
- <template v-if="item.contentType === KeFuMessageContentTypeEnum.IMAGE">
- <view class="message-box" :class="{'admin': item.senderType === UserTypeEnum.ADMIN}" :style="{ width: '200rpx' }">
- <su-image
- class="message-img"
- isPreview
- :previewList="[sheep.$url.cdn(item.content)]"
- :current="0"
- :src="sheep.$url.cdn(item.content)"
- :height="200"
- :width="200"
- mode="aspectFill"
- ></su-image>
- </view>
- </template>
- <template v-if="item.contentType === KeFuMessageContentTypeEnum.PRODUCT">
- <GoodsItem
- :goodsData="item.content.item"
- @tap="
- sheep.$router.go('/pages/goods/index', {
- id: item.content.item.id,
- })
- "
- />
- </template>
- <template v-if="item.contentType === KeFuMessageContentTypeEnum.ORDER">
- <OrderItem
- from="msg"
- :orderData="item.content.item"
- @tap="
- sheep.$router.go('/pages/order/detail', {
- id: item.content.item.id,
- })
- "
- />
- </template>
- <!-- user头像 -->
- <image
- v-if="item.senderType === UserTypeEnum.MEMBER"
- class="chat-avatar ss-m-l-24"
- :src="sheep.$url.cdn(item?.senderAvatar) ||
- sheep.$url.static('/static/img/shop/chat/default.png')"
- mode="aspectFill"
- >
- </image>
- </view>
- </view>
- <!-- 视图滚动锚点 -->
- <view id="scrollBottom"></view>
- </scroll-view>
- </view>
- </template>
- <script setup>
- import sheep from '@/sheep';
- import OrderItem from '@/pages/chat/components/order.vue';
- import GoodsItem from '@/pages/chat/components/goods.vue';
- import { reactive, ref, unref } from 'vue';
- import { formatDate } from '@/sheep/util';
- import dayjs from 'dayjs';
- import { KeFuMessageContentTypeEnum,UserTypeEnum } from './constants';
- import { emojiList } from '@/pages/chat/emoji';
- const KEFU_MESSAGE_TYPE = 'kefu_message_type'; // 客服消息类型
- const { screenHeight, safeAreaInsets, safeArea, screenWidth } = sheep.$platform.device;
- const pageHeight = safeArea.height - 44 - 35 - 50;
- const state = reactive({
- scrollInto: '',
- });
- const chatList = [
- {
- id: 1,
- conversationId: 1001,
- senderId: 1,
- senderType: 1, // UserTypeEnum.MEMBER
- receiverId: 2,
- receiverType: 1, // UserTypeEnum.MEMBER
- contentType: 1, // KeFuMessageContentTypeEnum.TEXT
- content: "Hello, how are you?",
- readStatus: false
- },
- {
- id: 2,
- conversationId: 1001,
- senderId: 2,
- senderType: 1, // UserTypeEnum.MEMBER
- receiverId: 1,
- receiverType: 1, // UserTypeEnum.MEMBER
- contentType: 1, // KeFuMessageContentTypeEnum.TEXT
- content: "I'm good, thanks! [流泪][流泪][流泪][流泪]",
- readStatus: false
- },
- {
- id: 3,
- conversationId: 1002,
- senderId: 3,
- senderType: 2, // UserTypeEnum.ADMIN
- receiverId: 4,
- receiverType: 1, // UserTypeEnum.MEMBER
- contentType: 2, // KeFuMessageContentTypeEnum.IMAGE
- content: "https://static.iocoder.cn/mall/a79f5d2ea6bf0c3c11b2127332dfe2df.jpg",
- readStatus: true
- },
- {
- id: 4,
- conversationId: 1002,
- senderId: 4,
- senderType: 1, // UserTypeEnum.MEMBER
- receiverId: 3,
- receiverType: 2, // UserTypeEnum.ADMIN
- contentType: 1, // KeFuMessageContentTypeEnum.TEXT
- content: "This is a text message.",
- readStatus: false
- },
- {
- id: 5,
- conversationId: 1003,
- senderId: 5,
- senderType: 1, // UserTypeEnum.MEMBER
- receiverId: 6,
- receiverType: 1, // UserTypeEnum.MEMBER
- contentType: 3, // KeFuMessageContentTypeEnum.VOICE
- content: "Voice content here",
- readStatus: true
- },
- {
- id: 6,
- conversationId: 1003,
- senderId: 6,
- senderType: 1, // UserTypeEnum.MEMBER
- receiverId: 5,
- receiverType: 1, // UserTypeEnum.MEMBER
- contentType: 1, // KeFuMessageContentTypeEnum.TEXT
- content: "Another text message.",
- readStatus: false
- },
- {
- id: 7,
- conversationId: 1004,
- senderId: 7,
- senderType: 2, // UserTypeEnum.ADMIN
- receiverId: 8,
- receiverType: 1, // UserTypeEnum.MEMBER
- contentType: 1, // KeFuMessageContentTypeEnum.VIDEO
- content: "Video content here",
- readStatus: true
- },
- {
- id: 8,
- conversationId: 1004,
- senderId: 8,
- senderType: 1, // UserTypeEnum.MEMBER
- receiverId: 7,
- receiverType: 2, // UserTypeEnum.ADMIN
- contentType: 5, // KeFuMessageContentTypeEnum.SYSTEM
- content: "System message content",
- readStatus: false
- },
- {
- id: 9,
- conversationId: 1005,
- senderId: 9,
- senderType: 1, // UserTypeEnum.MEMBER
- receiverId: 10,
- receiverType: 1, // UserTypeEnum.MEMBER
- contentType: 10, // KeFuMessageContentTypeEnum.PRODUCT
- content: "Product message content",
- readStatus: true
- },
- {
- id: 10,
- conversationId: 1005,
- senderId: 10,
- senderType: 1, // UserTypeEnum.MEMBER
- receiverId: 9,
- receiverType: 1, // UserTypeEnum.MEMBER
- contentType: 11, // KeFuMessageContentTypeEnum.ORDER
- content: "Order message content",
- readStatus: false
- }
- ];
- const isSendSuccess = ref(-1)
- //======================= 工具函数 =======================
- /**
- * 是否显示时间
- * @param {*} item - 数据
- * @param {*} index - 索引
- */
- const showTime = (item, index) => {
- if (unref(chatList)[index + 1]) {
- let dateString = dayjs(unref(chatList)[index + 1].date).fromNow();
- return dateString !== dayjs(unref(item).date).fromNow();
- }
- return false;
- };
- // 处理表情
- function replaceEmoji(data) {
- let newData = data;
- if (typeof newData !== 'object') {
- let reg = /\[(.+?)\]/g; // [] 中括号
- let zhEmojiName = newData.match(reg);
- if (zhEmojiName) {
- zhEmojiName.forEach((item) => {
- let emojiFile = selEmojiFile(item);
- newData = newData.replace(
- item,
- `<img class="chat-img" style="width: 24px;height: 24px;margin: 0 3px;" src="${sheep.$url.cdn(
- '/static/img/chat/emoji/' + emojiFile,
- )}"/>`,
- );
- });
- }
- }
- return newData;
- }
- function selEmojiFile(name) {
- for (let index in emojiList) {
- if (emojiList[index].name === name) {
- return emojiList[index].file;
- }
- }
- return false;
- }
- </script>
- <style scoped lang="scss">
- .chat-box {
- padding: 0 20rpx 0;
- .loadmore-btn {
- width: 98%;
- height: 40px;
- font-size: 12px;
- color: #8c8c8c;
- .loadmore-icon {
- transform: rotate(90deg);
- }
- }
- .message-item {
- margin-bottom: 33rpx;
- }
- .date-message,
- .system-message {
- width: fit-content;
- border-radius: 12rpx;
- padding: 8rpx 16rpx;
- margin-bottom: 16rpx;
- background-color: var(--ui-BG-3);
- color: #999;
- font-size: 24rpx;
- }
- .chat-avatar {
- width: 70rpx;
- height: 70rpx;
- border-radius: 50%;
- }
- .send-status {
- color: #333;
- height: 80rpx;
- margin-right: 8rpx;
- display: flex;
- align-items: center;
- .loading {
- width: 32rpx;
- height: 32rpx;
- -webkit-animation: rotating 2s linear infinite;
- animation: rotating 2s linear infinite;
- @-webkit-keyframes rotating {
- 0% {
- transform: rotateZ(0);
- }
- 100% {
- transform: rotateZ(360deg);
- }
- }
- @keyframes rotating {
- 0% {
- transform: rotateZ(0);
- }
- 100% {
- transform: rotateZ(360deg);
- }
- }
- }
- .warning {
- width: 32rpx;
- height: 32rpx;
- color: #ff3000;
- }
- }
- .message-box {
- max-width: 50%;
- font-size: 16px;
- line-height: 20px;
- // max-width: 500rpx;
- white-space: normal;
- word-break: break-all;
- word-wrap: break-word;
- padding: 20rpx;
- border-radius: 10rpx;
- color: #fff;
- background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
- &.admin {
- background: #fff;
- color: #333;
- }
- :deep() {
- .imgred {
- width: 100%;
- }
- .imgred,
- img {
- width: 100%;
- }
- }
- }
- :deep() {
- .goods,
- .order {
- max-width: 500rpx;
- }
- }
- .message-img {
- width: 100px;
- height: 100px;
- border-radius: 6rpx;
- }
- .template-wrap {
- // width: 100%;
- padding: 20rpx 24rpx;
- background: #fff;
- border-radius: 10rpx;
- .title {
- font-size: 26rpx;
- font-weight: 500;
- color: #333;
- margin-bottom: 29rpx;
- }
- .item {
- font-size: 24rpx;
- color: var(--ui-BG-Main);
- margin-bottom: 16rpx;
- &:last-of-type {
- margin-bottom: 0;
- }
- }
- }
- .error-img {
- width: 400rpx;
- height: 400rpx;
- }
- #scrollBottom {
- height: 120rpx;
- }
- }
- </style>
|