2024-07-01 11:36:07 +08:00
|
|
|
|
<template>
|
2024-11-04 15:25:43 +08:00
|
|
|
|
<el-container class="kefu-layout">
|
2024-07-10 00:02:39 +08:00
|
|
|
|
<!-- 会话列表 -->
|
2024-11-04 15:25:43 +08:00
|
|
|
|
<KeFuConversationList ref="keFuConversationRef" @change="handleChange" />
|
2024-07-10 00:02:39 +08:00
|
|
|
|
<!-- 会话详情(选中会话的消息列表) -->
|
2024-11-09 16:58:32 +08:00
|
|
|
|
<KeFuMessageList ref="keFuChatBoxRef" />
|
2024-11-05 09:50:47 +08:00
|
|
|
|
<!-- 会员信息(选中会话的会员信息) -->
|
2024-11-04 15:25:43 +08:00
|
|
|
|
<MemberInfo ref="memberInfoRef" />
|
|
|
|
|
</el-container>
|
2024-07-01 11:36:07 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2024-11-04 15:25:43 +08:00
|
|
|
|
import { KeFuConversationList, KeFuMessageList, MemberInfo } from './components'
|
2024-07-05 16:36:52 +08:00
|
|
|
|
import { WebSocketMessageTypeConstants } from './components/tools/constants'
|
2024-07-01 16:15:27 +08:00
|
|
|
|
import { KeFuConversationRespVO } from '@/api/mall/promotion/kefu/conversation'
|
2024-10-02 14:11:02 +08:00
|
|
|
|
import { getRefreshToken } from '@/utils/auth'
|
2024-07-04 17:00:50 +08:00
|
|
|
|
import { useWebSocket } from '@vueuse/core'
|
2024-11-09 16:58:32 +08:00
|
|
|
|
import { useMallKefuStore } from '@/store/modules/mall/kefu'
|
2024-11-10 18:09:22 +08:00
|
|
|
|
import { jsonParse } from '@/utils'
|
2024-07-01 11:36:07 +08:00
|
|
|
|
|
|
|
|
|
defineOptions({ name: 'KeFu' })
|
2024-07-05 16:36:52 +08:00
|
|
|
|
|
2024-07-10 00:02:39 +08:00
|
|
|
|
const message = useMessage() // 消息弹窗
|
2024-11-09 16:58:32 +08:00
|
|
|
|
const kefuStore = useMallKefuStore() // 客服缓存
|
2024-07-01 11:36:07 +08:00
|
|
|
|
|
2024-07-10 00:02:39 +08:00
|
|
|
|
// ======================= WebSocket start =======================
|
2024-07-04 17:00:50 +08:00
|
|
|
|
const server = ref(
|
2024-10-02 14:11:02 +08:00
|
|
|
|
(import.meta.env.VITE_BASE_URL + '/infra/ws').replace('http', 'ws') +
|
|
|
|
|
'?token=' +
|
|
|
|
|
getRefreshToken() // 使用 getRefreshToken() 方法,而不使用 getAccessToken() 方法的原因:WebSocket 无法方便的刷新访问令牌
|
2024-07-04 17:00:50 +08:00
|
|
|
|
) // WebSocket 服务地址
|
|
|
|
|
|
|
|
|
|
/** 发起 WebSocket 连接 */
|
|
|
|
|
const { data, close, open } = useWebSocket(server.value, {
|
2024-07-10 14:34:12 +08:00
|
|
|
|
autoReconnect: true,
|
2024-07-04 17:00:50 +08:00
|
|
|
|
heartbeat: true
|
|
|
|
|
})
|
2024-07-10 00:02:39 +08:00
|
|
|
|
|
|
|
|
|
/** 监听 WebSocket 数据 */
|
2024-07-04 17:00:50 +08:00
|
|
|
|
watchEffect(() => {
|
|
|
|
|
if (!data.value) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
// 1. 收到心跳
|
|
|
|
|
if (data.value === 'pong') {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 2.1 解析 type 消息类型
|
|
|
|
|
const jsonMessage = JSON.parse(data.value)
|
|
|
|
|
const type = jsonMessage.type
|
|
|
|
|
if (!type) {
|
|
|
|
|
message.error('未知的消息类型:' + data.value)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 2.2 消息类型:KEFU_MESSAGE_TYPE
|
|
|
|
|
if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE) {
|
2024-11-09 16:58:32 +08:00
|
|
|
|
const message = JSON.parse(jsonMessage.content)
|
2024-07-05 16:36:52 +08:00
|
|
|
|
// 刷新会话列表
|
2024-11-09 16:58:32 +08:00
|
|
|
|
kefuStore.updateConversation(message.conversationId)
|
2024-07-04 17:00:50 +08:00
|
|
|
|
// 刷新消息列表
|
2024-11-09 16:58:32 +08:00
|
|
|
|
keFuChatBoxRef.value?.refreshMessageList(message)
|
2024-07-04 17:00:50 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
2024-07-05 16:36:52 +08:00
|
|
|
|
// 2.3 消息类型:KEFU_MESSAGE_ADMIN_READ
|
|
|
|
|
if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_ADMIN_READ) {
|
2024-11-09 16:58:32 +08:00
|
|
|
|
// 更新会话已读
|
2024-11-10 18:09:22 +08:00
|
|
|
|
kefuStore.updateConversationStatus(jsonParse(jsonMessage.content))
|
2024-07-05 16:36:52 +08:00
|
|
|
|
}
|
2024-07-04 17:00:50 +08:00
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error)
|
|
|
|
|
}
|
|
|
|
|
})
|
2024-07-10 00:02:39 +08:00
|
|
|
|
// ======================= WebSocket end =======================
|
|
|
|
|
|
|
|
|
|
/** 加载指定会话的消息列表 */
|
2024-07-10 14:34:12 +08:00
|
|
|
|
const keFuChatBoxRef = ref<InstanceType<typeof KeFuMessageList>>()
|
2024-11-04 15:25:43 +08:00
|
|
|
|
const memberInfoRef = ref<InstanceType<typeof MemberInfo>>()
|
2024-07-10 00:02:39 +08:00
|
|
|
|
const handleChange = (conversation: KeFuConversationRespVO) => {
|
2024-07-17 11:25:26 +08:00
|
|
|
|
keFuChatBoxRef.value?.getNewMessageList(conversation)
|
2024-11-04 15:25:43 +08:00
|
|
|
|
memberInfoRef.value?.initHistory(conversation)
|
2024-07-10 00:02:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-20 23:44:25 +08:00
|
|
|
|
const keFuConversationRef = ref<InstanceType<typeof KeFuConversationList>>()
|
2024-07-10 00:02:39 +08:00
|
|
|
|
/** 初始化 */
|
2024-07-04 17:00:50 +08:00
|
|
|
|
onMounted(() => {
|
2024-11-09 16:58:32 +08:00
|
|
|
|
/** 加载会话列表 */
|
2024-11-20 23:44:25 +08:00
|
|
|
|
kefuStore.setConversationList().then(() => {
|
|
|
|
|
keFuConversationRef.value?.calculationLastMessageTime()
|
|
|
|
|
})
|
2024-07-04 17:00:50 +08:00
|
|
|
|
// 打开 websocket 连接
|
|
|
|
|
open()
|
|
|
|
|
})
|
2024-07-10 00:02:39 +08:00
|
|
|
|
|
|
|
|
|
/** 销毁 */
|
2024-07-04 17:00:50 +08:00
|
|
|
|
onBeforeUnmount(() => {
|
|
|
|
|
// 关闭 websocket 连接
|
|
|
|
|
close()
|
2024-07-01 11:36:07 +08:00
|
|
|
|
})
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style lang="scss">
|
2024-11-04 15:25:43 +08:00
|
|
|
|
.kefu-layout {
|
|
|
|
|
position: absolute;
|
|
|
|
|
flex: 1;
|
|
|
|
|
top: 0;
|
|
|
|
|
left: 0;
|
|
|
|
|
height: 100%;
|
|
|
|
|
width: 100%;
|
2024-07-01 11:36:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 定义滚动条样式 */
|
|
|
|
|
::-webkit-scrollbar {
|
|
|
|
|
width: 10px;
|
|
|
|
|
height: 6px;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-10 00:02:39 +08:00
|
|
|
|
/* 定义滚动条轨道 内阴影+圆角 */
|
2024-07-01 11:36:07 +08:00
|
|
|
|
::-webkit-scrollbar-track {
|
2024-07-10 00:02:39 +08:00
|
|
|
|
box-shadow: inset 0 0 0 rgba(240, 240, 240, 0.5);
|
2024-07-01 11:36:07 +08:00
|
|
|
|
border-radius: 10px;
|
|
|
|
|
background-color: #fff;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-10 00:02:39 +08:00
|
|
|
|
/* 定义滑块 内阴影+圆角 */
|
2024-07-01 11:36:07 +08:00
|
|
|
|
::-webkit-scrollbar-thumb {
|
|
|
|
|
border-radius: 10px;
|
2024-07-10 00:02:39 +08:00
|
|
|
|
box-shadow: inset 0 0 0 rgba(240, 240, 240, 0.5);
|
2024-07-01 11:36:07 +08:00
|
|
|
|
background-color: rgba(240, 240, 240, 0.5);
|
|
|
|
|
}
|
|
|
|
|
</style>
|