Skip to content

Commit 893cd5d

Browse files
author
puhui999
committed
【优化】:mall 客服优化消息样式,增加消息时间段显示
1 parent e89b274 commit 893cd5d

File tree

4 files changed

+81
-53
lines changed

4 files changed

+81
-53
lines changed

src/views/mall/promotion/kefu/components/KeFuChatBox.vue

Lines changed: 77 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,55 @@
66
<el-main class="kefu-content" style="overflow: visible">
77
<el-scrollbar ref="scrollbarRef" always height="calc(100vh - 495px)">
88
<div ref="innerRef" class="w-[100%] pb-3px">
9-
<div
10-
v-for="item in messageList"
11-
:key="item.id"
12-
:class="[
13-
item.senderType === UserTypeEnum.MEMBER
14-
? `ss-row-left`
15-
: item.senderType === UserTypeEnum.ADMIN
16-
? `ss-row-right`
17-
: ''
18-
]"
19-
class="flex mb-20px w-[100%]"
20-
>
21-
<el-avatar
22-
v-if="item.senderType === UserTypeEnum.MEMBER"
23-
:src="keFuConversation.userAvatar"
24-
alt="avatar"
25-
/>
26-
<div class="kefu-message p-10px">
27-
<!-- 文本消息 -->
28-
<TextMessageItem :message="item" />
29-
<!-- 图片消息 -->
30-
<ImageMessageItem :message="item" />
9+
<div v-for="(item, index) in messageList" :key="item.id" class="w-[100%]">
10+
<div class="flex justify-center items-center mb-20px">
11+
<!-- 日期 -->
12+
<div
13+
v-if="
14+
item.contentType !== KeFuMessageContentTypeEnum.SYSTEM && showTime(item, index)
15+
"
16+
class="date-message"
17+
>
18+
{{ formatDate(item.createTime) }}
19+
</div>
20+
<!-- 系统消息 -->
21+
<view
22+
v-if="item.contentType === KeFuMessageContentTypeEnum.SYSTEM"
23+
class="system-message"
24+
>
25+
{{ item.content }}
26+
</view>
27+
</div>
28+
<div
29+
:class="[
30+
item.senderType === UserTypeEnum.MEMBER
31+
? `ss-row-left`
32+
: item.senderType === UserTypeEnum.ADMIN
33+
? `ss-row-right`
34+
: ''
35+
]"
36+
class="flex mb-20px w-[100%]"
37+
>
38+
<el-avatar
39+
v-if="item.senderType === UserTypeEnum.MEMBER"
40+
:src="keFuConversation.userAvatar"
41+
alt="avatar"
42+
/>
43+
<div
44+
:class="{ 'kefu-message': KeFuMessageContentTypeEnum.TEXT === item.contentType }"
45+
class="p-10px"
46+
>
47+
<!-- 文本消息 -->
48+
<TextMessageItem :message="item" />
49+
<!-- 图片消息 -->
50+
<ImageMessageItem :message="item" />
51+
</div>
52+
<el-avatar
53+
v-if="item.senderType === UserTypeEnum.ADMIN"
54+
:src="item.senderAvatar"
55+
alt="avatar"
56+
/>
3157
</div>
32-
<el-avatar
33-
v-if="item.senderType === UserTypeEnum.ADMIN"
34-
:src="item.senderAvatar"
35-
alt="avatar"
36-
/>
3758
</div>
3859
</div>
3960
</el-scrollbar>
@@ -69,6 +90,11 @@ import { Emoji } from './tools/emoji'
6990
import { KeFuMessageContentTypeEnum } from './tools/constants'
7091
import { isEmpty } from '@/utils/is'
7192
import { UserTypeEnum } from '@/utils/constants'
93+
import { formatDate } from '@/utils/formatTime'
94+
import dayjs from 'dayjs'
95+
import relativeTime from 'dayjs/plugin/relativeTime'
96+
97+
dayjs.extend(relativeTime)
7298
7399
defineOptions({ name: 'KeFuMessageBox' })
74100
const messageTool = useMessage()
@@ -90,7 +116,7 @@ const getMessageList = async (conversation: KeFuConversationRespVO) => {
90116
if (!poller.value) {
91117
poller.value = setInterval(() => {
92118
getMessageList(conversation)
93-
}, 1000)
119+
}, 2000)
94120
}
95121
}
96122
defineExpose({ getMessageList })
@@ -143,7 +169,18 @@ const scrollToBottom = async () => {
143169
await nextTick()
144170
scrollbarRef.value!.setScrollTop(innerRef.value!.clientHeight)
145171
}
146-
172+
/**
173+
* 是否显示时间
174+
* @param {*} item - 数据
175+
* @param {*} index - 索引
176+
*/
177+
const showTime = computed(() => (item: KeFuMessageRespVO, index: number) => {
178+
if (unref(messageList.value)[index + 1]) {
179+
let dateString = dayjs(unref(messageList.value)[index + 1].createTime).fromNow()
180+
return dateString !== dayjs(unref(item).createTime).fromNow()
181+
}
182+
return false
183+
})
147184
// TODO puhui999: 轮训相关,功能完善后移除
148185
onBeforeUnmount(() => {
149186
if (!poller.value) {
@@ -225,6 +262,17 @@ onBeforeUnmount(() => {
225262
transform: scale(1.03);
226263
}
227264
}
265+
266+
.date-message,
267+
.system-message {
268+
width: fit-content;
269+
border-radius: 12rpx;
270+
padding: 8rpx 16rpx;
271+
margin-bottom: 16rpx;
272+
background-color: #e8e8e8;
273+
color: #999;
274+
font-size: 24rpx;
275+
}
228276
}
229277
230278
.chat-tools {

src/views/mall/promotion/kefu/components/KeFuConversationBox.vue

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<script lang="ts" setup>
3737
import { KeFuConversationApi, KeFuConversationRespVO } from '@/api/mall/promotion/kefu/conversation'
3838
import { useEmoji } from './tools/emoji'
39-
import { formatDate, getNowDateTime } from '@/utils/formatTime'
39+
import { formatDate } from '@/utils/formatTime'
4040
import { KeFuMessageContentTypeEnum } from './tools/constants'
4141
4242
defineOptions({ name: 'KeFuConversationBox' })
@@ -45,24 +45,6 @@ const activeConversationIndex = ref(-1) // 选中的会话
4545
const conversationList = ref<KeFuConversationRespVO[]>([]) // 会话列表
4646
const getConversationList = async () => {
4747
conversationList.value = await KeFuConversationApi.getConversationList()
48-
// 测试数据
49-
for (let i = 0; i < 5; i++) {
50-
conversationList.value.push({
51-
id: 1,
52-
userId: 283,
53-
userAvatar:
54-
'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKMezSxtOImrC9lbhwHiazYwck3xwrEcO7VJfG6WQo260whaeVNoByE5RreiaGsGfOMlIiaDhSaA991w/132',
55-
userNickname: '辉辉鸭' + i,
56-
lastMessageTime: getNowDateTime(),
57-
lastMessageContent:
58-
'[爱心][爱心]你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇你好哇',
59-
lastMessageContentType: 1,
60-
adminPinned: false,
61-
userDeleted: false,
62-
adminDeleted: false,
63-
adminUnreadMessageCount: 19
64-
})
65-
}
6648
}
6749
defineExpose({ getConversationList })
6850
const emits = defineEmits<{

src/views/mall/promotion/kefu/components/message/ImageMessageItem.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
? `mr-10px`
1010
: ''
1111
]"
12-
class="flex messages-center"
1312
>
1413
<el-image
1514
:src="message.content"
1615
fit="contain"
17-
style="width: 200px; height: 200px"
16+
style="width: 200px"
1817
@click="imagePreview(message.content)"
1918
/>
2019
</div>

src/views/mall/promotion/kefu/components/tools/PictureSelectUpload.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ const emits = defineEmits<{
1616
// 选择并上传文件
1717
const selectAndUpload = async () => {
1818
const files: any = await getFiles()
19-
message.success('图片发送请稍等。。。')
19+
message.success('图片发送中请稍等。。。')
2020
const res = await FileApi.updateFile({ file: files[0].file })
21-
message.success('图片发送成功!')
2221
emits('send-picture', res.data)
2322
}
2423
@@ -34,7 +33,7 @@ const selectAndUpload = async () => {
3433
async function getFiles(options = {}) {
3534
const { multiple, accept, limit, fileSize } = {
3635
multiple: true,
37-
accept: 'image/jpeg, image/png, image/gif',
36+
accept: 'image/jpeg, image/png, image/gif', // 默认选择图片
3837
limit: 1,
3938
fileSize: 500,
4039
...options

0 commit comments

Comments
 (0)