6
6
<el-main class =" kefu-content" style =" overflow : visible " >
7
7
<el-scrollbar ref =" scrollbarRef" always height =" calc(100vh - 495px)" >
8
8
<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
+ />
31
57
</div >
32
- <el-avatar
33
- v-if =" item.senderType === UserTypeEnum.ADMIN"
34
- :src =" item.senderAvatar"
35
- alt =" avatar"
36
- />
37
58
</div >
38
59
</div >
39
60
</el-scrollbar >
@@ -69,6 +90,11 @@ import { Emoji } from './tools/emoji'
69
90
import { KeFuMessageContentTypeEnum } from ' ./tools/constants'
70
91
import { isEmpty } from ' @/utils/is'
71
92
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 )
72
98
73
99
defineOptions ({ name: ' KeFuMessageBox' })
74
100
const messageTool = useMessage ()
@@ -90,7 +116,7 @@ const getMessageList = async (conversation: KeFuConversationRespVO) => {
90
116
if (! poller .value ) {
91
117
poller .value = setInterval (() => {
92
118
getMessageList (conversation )
93
- }, 1000 )
119
+ }, 2000 )
94
120
}
95
121
}
96
122
defineExpose ({ getMessageList })
@@ -143,7 +169,18 @@ const scrollToBottom = async () => {
143
169
await nextTick ()
144
170
scrollbarRef .value ! .setScrollTop (innerRef .value ! .clientHeight )
145
171
}
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
+ })
147
184
// TODO puhui999: 轮训相关,功能完善后移除
148
185
onBeforeUnmount (() => {
149
186
if (! poller .value ) {
@@ -225,6 +262,17 @@ onBeforeUnmount(() => {
225
262
transform : scale (1.03 );
226
263
}
227
264
}
265
+
266
+ .date-message ,
267
+ .system-message {
268
+ width : fit-content ;
269
+ border-radius : 12 rpx;
270
+ padding : 8 rpx 16 rpx;
271
+ margin-bottom : 16 rpx;
272
+ background-color : #e8e8e8 ;
273
+ color : #999 ;
274
+ font-size : 24 rpx;
275
+ }
228
276
}
229
277
230
278
.chat-tools {
0 commit comments