【新增】:mall 客服接入 websocket 实现消息实时拉取

This commit is contained in:
puhui999 2024-07-04 17:00:50 +08:00
parent 893cd5d8f5
commit 2b329d33e4
4 changed files with 75 additions and 34 deletions

View File

@ -101,7 +101,6 @@ const messageTool = useMessage()
const message = ref('') //
const messageList = ref<KeFuMessageRespVO[]>([]) //
const keFuConversation = ref<KeFuConversationRespVO>({} as KeFuConversationRespVO) //
const poller = ref<any>(null) // TODO puhui999: websocket
// TODO puhui999:
const getMessageList = async (conversation: KeFuConversationRespVO) => {
keFuConversation.value = conversation
@ -112,14 +111,15 @@ const getMessageList = async (conversation: KeFuConversationRespVO) => {
messageList.value = list.reverse()
// TODO puhui999:
await scrollToBottom()
// TODO puhui999:
if (!poller.value) {
poller.value = setInterval(() => {
getMessageList(conversation)
}, 2000)
}
}
defineExpose({ getMessageList })
//
const refreshMessageList = () => {
if (!keFuConversation.value) {
return
}
getMessageList(keFuConversation.value)
}
defineExpose({ getMessageList, refreshMessageList })
//
const showChatBox = computed(() => !isEmpty(keFuConversation.value))
//
@ -181,13 +181,6 @@ const showTime = computed(() => (item: KeFuMessageRespVO, index: number) => {
}
return false
})
// TODO puhui999:
onBeforeUnmount(() => {
if (!poller.value) {
return
}
clearInterval(poller.value)
})
</script>
<style lang="scss" scoped>

View File

@ -55,22 +55,6 @@ const openRightMessage = (item: KeFuConversationRespVO, index: number) => {
activeConversationIndex.value = index
emits('change', item)
}
const poller = ref<any>(null) // TODO puhui999: websocket
onMounted(() => {
// TODO puhui999:
if (!poller.value) {
poller.value = setInterval(() => {
getConversationList()
}, 1000)
}
})
// TODO puhui999:
onBeforeUnmount(() => {
if (!poller.value) {
return
}
clearInterval(poller.value)
})
</script>
<style lang="scss" scoped>

View File

@ -1,3 +1,4 @@
// 客服消息类型枚举类
export const KeFuMessageContentTypeEnum = {
TEXT: 1, // 文本消息
IMAGE: 2, // 图片消息
@ -8,3 +9,8 @@ export const KeFuMessageContentTypeEnum = {
PRODUCT: 10, // 商品消息
ORDER: 11 // 订单消息"
}
// Promotion 的 WebSocket 消息类型枚举类
export const WebSocketMessageTypeConstants = {
KEFU_MESSAGE_TYPE: 'kefu_message_type', // 客服消息类型
KEFU_MESSAGE_ADMIN_READ: 'kefu_message_read_status_change' // 客服消息管理员已读
}

View File

@ -16,19 +16,77 @@
<script lang="ts" setup>
import { KeFuChatBox, KeFuConversationBox } from './components'
import { KeFuConversationRespVO } from '@/api/mall/promotion/kefu/conversation'
import { getAccessToken } from '@/utils/auth'
import { useWebSocket } from '@vueuse/core'
import { WebSocketMessageTypeConstants } from '@/views/mall/promotion/kefu/components/tools/constants'
defineOptions({ name: 'KeFu' })
const message = useMessage()
//
const keFuChatBoxRef = ref<InstanceType<typeof KeFuChatBox>>()
const handleChange = (conversation: KeFuConversationRespVO) => {
keFuChatBoxRef.value?.getMessageList(conversation)
}
//
//======================= websocket start=======================
const server = ref(
(import.meta.env.VITE_BASE_URL + '/infra/ws/').replace('http', 'ws') +
'?token=' +
getAccessToken()
) // WebSocket
/** 发起 WebSocket 连接 */
const { data, close, open } = useWebSocket(server.value, {
autoReconnect: false,
heartbeat: true
})
watchEffect(() => {
if (!data.value) {
return
}
try {
// 1.
if (data.value === 'pong') {
// state.recordList.push({
// text: '',
// time: new Date().getTime()
// })
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) {
//
getConversationList()
//
keFuChatBoxRef.value?.refreshMessageList()
return
}
} catch (error) {
console.error(error)
}
})
//======================= websocket end=======================
//
const keFuConversationRef = ref<InstanceType<typeof KeFuConversationBox>>()
onMounted(() => {
const getConversationList = () => {
keFuConversationRef.value?.getConversationList()
}
onMounted(() => {
getConversationList()
// websocket
open()
})
onBeforeUnmount(() => {
// websocket
close()
})
</script>