【新增】:mall 客服实现 emoji 表情选择和消息发送

pull/468/head
puhui999 2024-07-02 16:45:32 +08:00
parent 1abfec766e
commit 9c05ff35db
4 changed files with 151 additions and 189 deletions

View File

@ -51,7 +51,7 @@ export interface KeFuMessageRespVO {
export const KeFuMessageApi = { export const KeFuMessageApi = {
// 发送客服消息 // 发送客服消息
sendKeFuMessage: async (data: any) => { sendKeFuMessage: async (data: any) => {
return await request.put({ return await request.post({
url: '/promotion/kefu-message/send', url: '/promotion/kefu-message/send',
data data
}) })

View File

@ -0,0 +1,40 @@
<!-- emoji 表情选择组件 -->
<template>
<el-popover :width="500" placement="top" trigger="click">
<template #reference>
<Icon :size="30" class="ml-10px" icon="twemoji:grinning-face" />
</template>
<ElScrollbar height="300px">
<ul class="ml-2 flex flex-wrap px-2">
<li
v-for="(item, index) in emojiList"
:key="index"
:style="{
borderColor: 'var(--el-color-primary)',
color: 'var(--el-color-primary)'
}"
:title="item.name"
class="icon-item mr-2 mt-1 w-1/10 flex cursor-pointer items-center justify-center border border-solid p-2"
@click="handleSelect(item)"
>
<img :src="item.url" style="width: 24px; height: 24px" />
</li>
</ul>
</ElScrollbar>
</el-popover>
</template>
<script lang="ts" setup>
defineOptions({ name: 'EmojiSelectPopover' })
import { Emoji, getEmojiList } from './emoji'
const emojiList = computed(() => getEmojiList())
const emits = defineEmits<{
(e: 'select-emoji', v: Emoji)
}>()
const handleSelect = (item: Emoji) => {
// emoji 便
emits('select-emoji', item)
}
</script>

View File

@ -3,58 +3,61 @@
<el-header> <el-header>
<div class="kefu-title">{{ keFuConversation.userNickname }}</div> <div class="kefu-title">{{ keFuConversation.userNickname }}</div>
</el-header> </el-header>
<el-main class="kefu-content"> <el-main class="kefu-content" style="overflow: visible">
<div <el-scrollbar ref="scrollbarRef" always height="calc(100vh - 495px)">
v-for="item in messageList" <div ref="innerRef" class="w-[100%] pb-3px">
:key="item.id" <div
:class="[ v-for="item in messageList"
item.senderType === UserTypeEnum.MEMBER :key="item.id"
? `ss-row-left` :class="[
: item.senderType === UserTypeEnum.ADMIN item.senderType === UserTypeEnum.MEMBER
? `ss-row-right` ? `ss-row-left`
: '' : item.senderType === UserTypeEnum.ADMIN
]" ? `ss-row-right`
class="flex mb-20px w-[100%]" : ''
> ]"
<el-avatar class="flex mb-20px w-[100%]"
v-show="item.senderType === UserTypeEnum.MEMBER" >
:src="keFuConversation.userAvatar" <el-avatar
alt="avatar" v-show="item.senderType === UserTypeEnum.MEMBER"
/> :src="keFuConversation.userAvatar"
<div class="kefu-message flex items-center p-10px"> alt="avatar"
<!-- 文本消息 --> />
<template v-if="KeFuMessageContentTypeEnum.TEXT === item.contentType"> <div class="kefu-message flex items-center p-10px">
<div <!-- 文本消息 -->
v-dompurify-html="replaceEmoji(item.content)" <template v-if="KeFuMessageContentTypeEnum.TEXT === item.contentType">
:class="[ <div
item.senderType === UserTypeEnum.MEMBER v-dompurify-html="replaceEmoji(item.content)"
? `ml-10px` :class="[
: item.senderType === UserTypeEnum.ADMIN item.senderType === UserTypeEnum.MEMBER
? `mr-10px` ? `ml-10px`
: '' : item.senderType === UserTypeEnum.ADMIN
]" ? `mr-10px`
></div> : ''
</template> ]"
<template v-else> ></div>
{{ item.content }} </template>
</template> <template v-else>
{{ item.content }}
</template>
</div>
<el-avatar
v-show="item.senderType === UserTypeEnum.ADMIN"
:src="item.senderAvatar"
alt="avatar"
/>
</div>
</div> </div>
<el-avatar </el-scrollbar>
v-show="item.senderType === UserTypeEnum.ADMIN"
:src="item.senderAvatar"
alt="avatar"
/>
</div>
</el-main> </el-main>
<!-- TODO puhui999: 发送下次提交完善 -->
<el-footer height="230px"> <el-footer height="230px">
<div class="h-[100%]"> <div class="h-[100%]">
<div class="chat-tools"> <div class="chat-tools">
<Icon :size="30" class="ml-10px" icon="fa:frown-o" /> <EmojiSelectPopover @select-emoji="handleEmojiSelect" />
</div> </div>
<el-input v-model="message" :rows="6" type="textarea" /> <el-input v-model="message" :rows="6" type="textarea" />
<div class="h-45px flex justify-end"> <div class="h-45px flex justify-end">
<el-button class="mt-10px" type="primary">发送</el-button> <el-button class="mt-10px" type="primary" @click="handleSendMessage"></el-button>
</div> </div>
</div> </div>
</el-footer> </el-footer>
@ -63,162 +66,66 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { KeFuMessageRespVO } from '@/api/mall/promotion/kefu/message' import { ElScrollbar as ElScrollbarType } from 'element-plus'
import { KeFuMessageApi, KeFuMessageRespVO } from '@/api/mall/promotion/kefu/message'
import { KeFuConversationRespVO } from '@/api/mall/promotion/kefu/conversation' import { KeFuConversationRespVO } from '@/api/mall/promotion/kefu/conversation'
import { UserTypeEnum } from '@/utils/constants' import EmojiSelectPopover from './EmojiSelectPopover.vue'
import { replaceEmoji } from '@/views/mall/promotion/kefu/components/emoji' import { Emoji, replaceEmoji } from './emoji'
import { KeFuMessageContentTypeEnum } from '@/views/mall/promotion/kefu/components/constants' import { KeFuMessageContentTypeEnum } from './constants'
import { isEmpty } from '@/utils/is' import { isEmpty } from '@/utils/is'
import { UserTypeEnum } from '@/utils/constants'
defineOptions({ name: 'KeFuMessageBox' }) defineOptions({ name: 'KeFuMessageBox' })
const messageTool = useMessage()
const message = ref('') // const message = ref('') //
const messageList = ref<KeFuMessageRespVO[]>([]) // const messageList = ref<KeFuMessageRespVO[]>([]) //
const keFuConversation = ref<KeFuConversationRespVO>({} as KeFuConversationRespVO) // const keFuConversation = ref<KeFuConversationRespVO>({} as KeFuConversationRespVO) //
//
// TODO puhui999:
const getMessageList = async (conversation: KeFuConversationRespVO) => { const getMessageList = async (conversation: KeFuConversationRespVO) => {
keFuConversation.value = conversation keFuConversation.value = conversation
// const { list } = await KeFuMessageApi.getKeFuMessagePage({ const { list } = await KeFuMessageApi.getKeFuMessagePage({
// pageNo: 1, pageNo: 1,
// conversationId: conversation.id conversationId: conversation.id
// }) })
// TODO puhui999: 便 messageList.value = list.reverse()
const list = [ // TODO puhui999:
{ await scrollToBottom()
id: 19,
conversationId: 1,
senderId: 283,
senderAvatar: null,
senderType: 2,
receiverId: null,
receiverType: null,
contentType: 1,
content: '[爱心][爱心][坏笑][坏笑][天使][天使]',
readStatus: false,
createTime: 1718616705000
},
{
id: 18,
conversationId: 1,
senderId: 283,
senderAvatar: null,
senderType: 1,
receiverId: null,
receiverType: null,
contentType: 1,
content: '[瞌睡][瞌睡]',
readStatus: false,
createTime: 1718616690000
},
{
id: 17,
conversationId: 1,
senderId: 283,
senderAvatar: null,
senderType: 1,
receiverId: null,
receiverType: null,
contentType: 1,
content: '[冷酷][冷酷]',
readStatus: false,
createTime: 1718616350000
},
{
id: 16,
conversationId: 1,
senderId: 283,
senderAvatar: null,
senderType: 1,
receiverId: null,
receiverType: null,
contentType: 1,
content: '[天使]',
readStatus: false,
createTime: 1718615505000
},
{
id: 15,
conversationId: 1,
senderId: 283,
senderAvatar: null,
senderType: 2,
receiverId: null,
receiverType: null,
contentType: 1,
content: '[天使]',
readStatus: false,
createTime: 1718615485000
},
{
id: 14,
conversationId: 1,
senderId: 283,
senderAvatar: null,
senderType: 2,
receiverId: null,
receiverType: null,
contentType: 1,
content: '[心碎][心碎]',
readStatus: false,
createTime: 1718615453000
},
{
id: 13,
conversationId: 1,
senderId: 283,
senderAvatar: null,
senderType: 2,
receiverId: null,
receiverType: null,
contentType: 1,
content: '[心碎][心碎]',
readStatus: false,
createTime: 1718615430000
},
{
id: 12,
conversationId: 1,
senderId: 283,
senderAvatar: null,
senderType: 1,
receiverId: null,
receiverType: null,
contentType: 1,
content: '[心碎][心碎]',
readStatus: false,
createTime: 1718615425000
},
{
id: 11,
conversationId: 1,
senderId: 283,
senderAvatar: null,
senderType: 1,
receiverId: null,
receiverType: null,
contentType: 1,
content: '[困~][困~]',
readStatus: false,
createTime: 1718615413000
},
{
id: 10,
conversationId: 1,
senderId: 283,
senderAvatar: null,
senderType: 1,
receiverId: null,
receiverType: null,
contentType: 1,
content: '[睡着][睡着]',
readStatus: false,
createTime: 1718615407000
}
]
messageList.value = list
} }
defineExpose({ getMessageList }) defineExpose({ getMessageList })
// //
const showChatBox = computed(() => !isEmpty(keFuConversation.value)) const showChatBox = computed(() => !isEmpty(keFuConversation.value))
//
const handleEmojiSelect = (item: Emoji) => {
message.value += item.name
}
//
const handleSendMessage = async () => {
// 1.
if (isEmpty(unref(message.value))) {
messageTool.warning('请输入消息后再发送哦!')
}
// 2.
const msg = {
conversationId: keFuConversation.value.id,
contentType: KeFuMessageContentTypeEnum.TEXT,
content: message.value
}
await KeFuMessageApi.sendKeFuMessage(msg)
message.value = ''
// 3.
await getMessageList(keFuConversation.value)
//
await scrollToBottom()
}
const innerRef = ref<HTMLDivElement>()
const scrollbarRef = ref<InstanceType<typeof ElScrollbarType>>()
//
const scrollToBottom = async () => {
await nextTick()
scrollbarRef.value!.setScrollTop(innerRef.value!.clientHeight)
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@ -49,6 +49,11 @@ export const emojiList = [
{ name: '[恶魔]', file: 'emo.png' } { name: '[恶魔]', file: 'emo.png' }
] ]
export interface Emoji {
name: string
url: string
}
export const emojiPage = {} export const emojiPage = {}
emojiList.forEach((item, index) => { emojiList.forEach((item, index) => {
if (!emojiPage[Math.floor(index / 30) + 1]) { if (!emojiPage[Math.floor(index / 30) + 1]) {
@ -59,6 +64,8 @@ emojiList.forEach((item, index) => {
// 后端上传地址 // 后端上传地址
const staticUrl = import.meta.env.VITE_STATIC_URL const staticUrl = import.meta.env.VITE_STATIC_URL
// 后缀
const suffix = '/static/img/chat/emoji/'
// 处理表情 // 处理表情
export function replaceEmoji(data: string) { export function replaceEmoji(data: string) {
@ -72,7 +79,7 @@ export function replaceEmoji(data: string) {
newData = newData.replace( newData = newData.replace(
item, item,
`<img class="chat-img" style="width: 24px;height: 24px;margin: 0 3px;" src="${ `<img class="chat-img" style="width: 24px;height: 24px;margin: 0 3px;" src="${
staticUrl + '/static/img/chat/emoji/' + emojiFile staticUrl + suffix + emojiFile
}"/>` }"/>`
) )
}) })
@ -81,6 +88,14 @@ export function replaceEmoji(data: string) {
return newData return newData
} }
// 获得所有表情
export function getEmojiList(): Emoji[] {
return emojiList.map((item) => ({
url: staticUrl + suffix + item.file,
name: item.name
})) as Emoji[]
}
function selEmojiFile(name: string) { function selEmojiFile(name: string) {
for (const index in emojiList) { for (const index in emojiList) {
if (emojiList[index].name === name) { if (emojiList[index].name === name) {