Skip to content

Commit 4d2c038

Browse files
author
puhui999
committed
【功能完善】商城: 客服动态计算消息最后发送时间距离现在过去了多久
1 parent 3b49f7d commit 4d2c038

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<div class="flex justify-between items-center w-100%">
2727
<span class="username">{{ item.userNickname }}</span>
2828
<span class="color-[#999]" style="font-size: 13px">
29-
{{ formatPast(item.lastMessageTime, 'YYYY-MM-DD') }}
29+
{{ lastMessageTimeMap.get(item.id) ?? '计算中' }}
3030
</span>
3131
</div>
3232
<!-- 最后聊天内容 -->
@@ -89,6 +89,14 @@ const { replaceEmoji } = useEmoji()
8989
const activeConversationId = ref(-1) // 选中的会话
9090
const collapse = computed(() => appStore.getCollapse) // 折叠菜单
9191
92+
const lastMessageTimeMap = ref<Map<number, string>>(new Map<number, string>())
93+
/** 计算消息最后发送时间距离现在过去了多久 */
94+
const calculationLastMessageTime = () => {
95+
kefuStore.getConversationList?.forEach((item) => {
96+
lastMessageTimeMap.value.set(item.id, formatPast(item.lastMessageTime, 'YYYY-MM-DD'))
97+
})
98+
}
99+
defineExpose({ calculationLastMessageTime })
92100
/** 打开右侧的消息列表 */
93101
const emits = defineEmits<{
94102
(e: 'change', v: KeFuConversationRespVO): void
@@ -177,6 +185,16 @@ watch(showRightMenu, (val) => {
177185
document.body.removeEventListener('click', closeRightMenu)
178186
}
179187
})
188+
189+
const timer = ref<any>()
190+
/** 初始化 */
191+
onMounted(() => {
192+
timer.value = setInterval(calculationLastMessageTime, 1000 * 10) // 十秒计算一次
193+
})
194+
/** 组件卸载前 */
195+
onBeforeUnmount(() => {
196+
clearInterval(timer.value)
197+
})
180198
</script>
181199

182200
<style lang="scss" scoped>

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ const showTime = computed(() => (item: KeFuMessageRespVO, index: number) => {
414414
.kefu-message {
415415
background-color: rgb(245, 245, 245);
416416
margin-left: 10px;
417-
margin-top: 18px;
417+
margin-top: 3px;
418418
border-top-right-radius: 10px;
419419
border-bottom-right-radius: 10px;
420420
border-bottom-left-radius: 10px;
@@ -427,7 +427,7 @@ const showTime = computed(() => (item: KeFuMessageRespVO, index: number) => {
427427
.kefu-message {
428428
background-color: rgb(206, 223, 255);
429429
margin-right: 10px;
430-
margin-top: 18px;
430+
margin-top: 3px;
431431
border-top-left-radius: 10px;
432432
border-bottom-right-radius: 10px;
433433
border-bottom-left-radius: 10px;
@@ -454,11 +454,11 @@ const showTime = computed(() => (item: KeFuMessageRespVO, index: number) => {
454454
.date-message,
455455
.system-message {
456456
width: fit-content;
457-
border-radius: 12rpx;
458-
padding: 8rpx 16rpx;
459-
margin-bottom: 16rpx;
457+
background-color: rgba(0, 0, 0, 0.1);
458+
border-radius: 8px;
459+
padding: 0 5px;
460460
color: #999;
461-
font-size: 24rpx;
461+
font-size: 10px;
462462
}
463463
}
464464

src/views/mall/promotion/kefu/index.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const server = ref(
3131
) // WebSocket 服务地址
3232
3333
/** 发起 WebSocket 连接 */
34-
// TODO puhui999: websocket 连接有点问题收不到消息 🤣
3534
const { data, close, open } = useWebSocket(server.value, {
3635
autoReconnect: true,
3736
heartbeat: true
@@ -49,7 +48,6 @@ watchEffect(() => {
4948
}
5049
// 2.1 解析 type 消息类型
5150
const jsonMessage = JSON.parse(data.value)
52-
console.log(jsonMessage)
5351
const type = jsonMessage.type
5452
if (!type) {
5553
message.error('未知的消息类型:' + data.value)
@@ -83,10 +81,13 @@ const handleChange = (conversation: KeFuConversationRespVO) => {
8381
memberInfoRef.value?.initHistory(conversation)
8482
}
8583
84+
const keFuConversationRef = ref<InstanceType<typeof KeFuConversationList>>()
8685
/** 初始化 */
8786
onMounted(() => {
8887
/** 加载会话列表 */
89-
kefuStore.setConversationList()
88+
kefuStore.setConversationList().then(() => {
89+
keFuConversationRef.value?.calculationLastMessageTime()
90+
})
9091
// 打开 websocket 连接
9192
open()
9293
})

0 commit comments

Comments
 (0)