1
1
<template >
2
- <div class =" kefu" >
2
+ <el-aside class =" kefu pt-5px h-100%" width =" 260px" >
3
+ <div class =" color-[#999] font-bold my-10px" >
4
+ 会话记录({{ kefuStore.getConversationList.length }})
5
+ </div >
3
6
<div
4
- v-for =" item in conversationList "
7
+ v-for =" item in kefuStore.getConversationList "
5
8
:key =" item.id"
6
9
:class =" { active: item.id === activeConversationId, pinned: item.adminPinned }"
7
- class =" kefu-conversation flex items-center"
10
+ class =" kefu-conversation px-10px flex items-center"
8
11
@click =" openRightMessage(item)"
9
12
@contextmenu.prevent =" rightClick($event as PointerEvent, item)"
10
13
>
22
25
<div class =" ml-10px w-100%" >
23
26
<div class =" flex justify-between items-center w-100%" >
24
27
<span class =" username" >{{ item.userNickname }}</span >
25
- <span class =" color-[var(--left-menu-text-color) ]" style =" font-size : 13px " >
26
- {{ formatPast (item.lastMessageTime, 'YYYY-MM-DD') }}
28
+ <span class =" color-[#999 ]" style =" font-size : 13px " >
29
+ {{ lastMessageTimeMap.get (item.id) ?? '计算中' }}
27
30
</span >
28
31
</div >
29
32
<!-- 最后聊天内容 -->
30
33
<div
31
34
v-dompurify-html ="
32
35
getConversationDisplayText(item.lastMessageContentType, item.lastMessageContent)
33
36
"
34
- class =" last-message flex items-center color-[var(--left-menu-text-color) ]"
37
+ class =" last-message flex items-center color-[#999 ]"
35
38
>
36
39
</div >
37
40
</div >
65
68
取消
66
69
</li >
67
70
</ul >
68
- </div >
71
+ </el-aside >
69
72
</template >
70
73
71
74
<script lang="ts" setup>
@@ -74,29 +77,36 @@ import { useEmoji } from './tools/emoji'
74
77
import { formatPast } from ' @/utils/formatTime'
75
78
import { KeFuMessageContentTypeEnum } from ' ./tools/constants'
76
79
import { useAppStore } from ' @/store/modules/app'
80
+ import { useMallKefuStore } from ' @/store/modules/mall/kefu'
81
+ import { jsonParse } from ' @/utils'
77
82
78
83
defineOptions ({ name: ' KeFuConversationList' })
79
84
80
85
const message = useMessage () // 消息弹窗
81
86
const appStore = useAppStore ()
87
+ const kefuStore = useMallKefuStore () // 客服缓存
82
88
const { replaceEmoji } = useEmoji ()
83
- const conversationList = ref <KeFuConversationRespVO []>([]) // 会话列表
84
89
const activeConversationId = ref (- 1 ) // 选中的会话
85
90
const collapse = computed (() => appStore .getCollapse ) // 折叠菜单
86
91
87
- /** 加载会话列表 */
88
- const getConversationList = async () => {
89
- const list = await KeFuConversationApi .getConversationList ()
90
- list .sort ((a : KeFuConversationRespVO , _ ) => (a .adminPinned ? - 1 : 1 ))
91
- conversationList .value = list
92
+ /** 计算消息最后发送时间距离现在过去了多久 */
93
+ const lastMessageTimeMap = ref <Map <number , string >>(new Map <number , string >())
94
+ const calculationLastMessageTime = () => {
95
+ kefuStore .getConversationList ?.forEach ((item ) => {
96
+ lastMessageTimeMap .value .set (item .id , formatPast (item .lastMessageTime , ' YYYY-MM-DD' ))
97
+ })
92
98
}
93
- defineExpose ({ getConversationList })
99
+ defineExpose ({ calculationLastMessageTime })
94
100
95
101
/** 打开右侧的消息列表 */
96
102
const emits = defineEmits <{
97
103
(e : ' change' , v : KeFuConversationRespVO ): void
98
104
}>()
99
105
const openRightMessage = (item : KeFuConversationRespVO ) => {
106
+ // 同一个会话则不处理
107
+ if (activeConversationId .value === item .id ) {
108
+ return
109
+ }
100
110
activeConversationId .value = item .id
101
111
emits (' change' , item )
102
112
}
@@ -118,7 +128,7 @@ const getConversationDisplayText = computed(
118
128
case KeFuMessageContentTypeEnum .VOICE :
119
129
return ' [语音消息]'
120
130
case KeFuMessageContentTypeEnum .TEXT :
121
- return replaceEmoji (lastMessageContent )
131
+ return replaceEmoji (jsonParse ( lastMessageContent ). text || lastMessageContent )
122
132
default :
123
133
return ' '
124
134
}
@@ -155,7 +165,7 @@ const updateConversationPinned = async (adminPinned: boolean) => {
155
165
message .notifySuccess (adminPinned ? ' 置顶成功' : ' 取消置顶成功' )
156
166
// 2. 关闭右键菜单,更新会话列表
157
167
closeRightMenu ()
158
- await getConversationList ( )
168
+ await kefuStore . updateConversation ( rightClickConversation . value . id )
159
169
}
160
170
161
171
/** 删除会话 */
@@ -165,7 +175,7 @@ const deleteConversation = async () => {
165
175
await KeFuConversationApi .deleteConversation (rightClickConversation .value .id )
166
176
// 2. 关闭右键菜单,更新会话列表
167
177
closeRightMenu ()
168
- await getConversationList ( )
178
+ kefuStore . deleteConversation ( rightClickConversation . value . id )
169
179
}
170
180
171
181
/** 监听右键菜单的显示状态,添加点击事件监听器 */
@@ -176,42 +186,48 @@ watch(showRightMenu, (val) => {
176
186
document .body .removeEventListener (' click' , closeRightMenu )
177
187
}
178
188
})
189
+
190
+ const timer = ref <any >()
191
+ /** 初始化 */
192
+ onMounted (() => {
193
+ timer .value = setInterval (calculationLastMessageTime , 1000 * 10 ) // 十秒计算一次
194
+ })
195
+ /** 组件卸载前 */
196
+ onBeforeUnmount (() => {
197
+ clearInterval (timer .value )
198
+ })
179
199
</script >
180
200
181
201
<style lang="scss" scoped>
182
202
.kefu {
203
+ background-color : #e5e4e4 ;
204
+
183
205
& -conversation {
184
206
height : 60px ;
185
- padding : 10px ;
186
207
// background-color: #fff;
187
- transition : border-left 0.05s ease-in-out ; /* 设置过渡效果 */
208
+ // transition: border-left 0.05s ease-in-out; /* 设置过渡效果 */
188
209
189
210
.username {
190
211
min-width : 0 ;
191
212
max-width : 60% ;
213
+ }
214
+
215
+ .last-message {
216
+ font-size : 13px ;
217
+ }
218
+
219
+ .last-message ,
220
+ .username {
192
221
overflow : hidden ;
193
222
text-overflow : ellipsis ;
194
223
display : -webkit-box ;
195
224
-webkit-box-orient : vertical ;
196
225
-webkit-line-clamp : 1 ;
197
226
}
198
-
199
- .last-message {
200
- font-size : 13px ;
201
- width : 200px ;
202
- overflow : hidden ; // 隐藏超出的文本
203
- white-space : nowrap ; // 禁止换行
204
- text-overflow : ellipsis ; // 添加省略号
205
- }
206
227
}
207
228
208
229
.active {
209
- border-left : 5px #3271ff solid ;
210
- background-color : var (--login-bg-color );
211
- }
212
-
213
- .pinned {
214
- background-color : var (--left-menu-bg-active-color );
230
+ background-color : rgba (128 , 128 , 128 , 0.5 ); // 透明色,暗黑模式下也能体现
215
231
}
216
232
217
233
.right-menu-ul {
0 commit comments