108 lines
2.6 KiB
Vue
108 lines
2.6 KiB
Vue
<script setup lang="ts">
|
|
import { computed } from 'vue'
|
|
import type { ChatMessage } from '@/types/customer-service'
|
|
import fileIcon from '@/static/icons/PDF.png'
|
|
|
|
const props = defineProps<{
|
|
message: ChatMessage
|
|
}>()
|
|
|
|
const messageType = computed(() => props.message.type || 'text')
|
|
const showReadStatus = computed(() => props.message.role === 'user' && props.message.read !== undefined)
|
|
const readStatusText = computed(() => (props.message.read ? '已读' : '未读'))
|
|
|
|
function handlePreviewImage() {
|
|
if (!props.message.imageUrl)
|
|
return
|
|
|
|
uni.previewImage({
|
|
urls: [props.message.imageUrl],
|
|
current: props.message.imageUrl,
|
|
})
|
|
}
|
|
|
|
function handleOpenFile() {
|
|
if (!props.message.filePath) {
|
|
uni.showToast({ title: props.message.fileName || '文件', icon: 'none' })
|
|
return
|
|
}
|
|
|
|
uni.openDocument({
|
|
filePath: props.message.filePath,
|
|
showMenu: true,
|
|
fail: () => {
|
|
uni.showToast({ title: `已发送${props.message.fileName || '文件'}`, icon: 'none' })
|
|
},
|
|
})
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<view
|
|
class="chat-message"
|
|
:class="`chat-message--${message.role}`"
|
|
>
|
|
<image
|
|
class="chat-message__avatar"
|
|
:src="message.avatar"
|
|
mode="aspectFill"
|
|
/>
|
|
|
|
<view class="chat-message__body">
|
|
<text class="chat-message__nickname">
|
|
{{ message.nickname }}
|
|
</text>
|
|
|
|
<view
|
|
v-if="messageType === 'image' && message.imageUrl"
|
|
class="chat-message__image-wrap"
|
|
@tap="handlePreviewImage"
|
|
>
|
|
<image
|
|
class="chat-message__image"
|
|
:src="message.imageUrl"
|
|
mode="aspectFill"
|
|
/>
|
|
</view>
|
|
|
|
<view
|
|
v-else-if="messageType === 'file'"
|
|
class="chat-message__bubble chat-message__bubble--file"
|
|
:class="`chat-message__bubble--${message.role}`"
|
|
@tap="handleOpenFile"
|
|
>
|
|
<image
|
|
class="chat-message__file-icon"
|
|
:src="fileIcon"
|
|
mode="aspectFit"
|
|
/>
|
|
<text class="chat-message__file-name">
|
|
{{ message.fileName || message.content }}
|
|
</text>
|
|
</view>
|
|
|
|
<view
|
|
v-else
|
|
class="chat-message__bubble"
|
|
:class="`chat-message__bubble--${message.role}`"
|
|
>
|
|
<text class="chat-message__text">
|
|
{{ message.content }}
|
|
</text>
|
|
</view>
|
|
|
|
<text
|
|
v-if="showReadStatus"
|
|
class="chat-message__read-status"
|
|
:class="{ 'chat-message__read-status--read': message.read }"
|
|
>
|
|
{{ readStatusText }}
|
|
</text>
|
|
</view>
|
|
</view>
|
|
</template>
|
|
|
|
<style scoped lang="scss">
|
|
@import './ChatMessage.scss';
|
|
</style>
|