Files

122 lines
2.7 KiB
Plaintext

<template>
<view class="content">
<view class="top-line">
<text class="nickname">{{ senderNickname }}</text>
<text class="time">{{ formatTime(messageTime) }}</text>
</view>
<view class="bottom-line">
<text class="message" :class="{ 'unread': hasUnread }">
{{ messageContent }}
</text>
<text v-if="hasUnread" class="unread-count">
{{ displayUnreadCount }}
</text>
</view>
</view>
</template>
<script setup lang="uts">
import { IMessage, ISender } from '@/types/chatType.uts'
const props = defineProps<{
message : IMessage
}>()
// 计算属性提取,避免模板中过多可选链
const senderNickname = computed(() => props.message?.sender?.nickname ?? '')
const messageContent = computed(() => props.message?.content ?? '')
const messageTime = computed(() => props.message?.timestamp ?? 0)
const unreadCount = computed(() => props.message?.unreadCount ?? 0)
const hasUnread = computed(() => unreadCount.value > 0)
const displayUnreadCount = computed(() => {
const count = unreadCount.value
return count > 99 ? '99+' : count.toString()
})
// 格式化时间
const formatTime = (timestamp : number) : string => {
const date = new Date(timestamp)
const now = new Date()
const diffHours = Math.floor((now.getTime() - date.getTime()) / (1000 * 60 * 60))
if (diffHours < 24) {
// 当天消息显示时间
return `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`
} else if (diffHours < 48) {
// 昨天
return '昨天'
} else if (diffHours < 168) {
// 一周内
const days = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
return days[date.getDay()]
} else {
// 更早
return `${date.getMonth() + 1}月${date.getDate()}日`
}
}
</script>
<style>
.content {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
}
.top-line {
display: flex;
flex-flow: row;
justify-content: space-between;
margin-bottom: 10rpx;
}
.nickname {
font-size: 32rpx;
font-weight: 500;
color: #333;
max-width: 400rpx;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.time {
font-size: 24rpx;
color: #999;
}
.bottom-line {
display: flex;
flex-flow: row;
justify-content: space-between;
align-items: center;
}
.message {
font-size: 28rpx;
color: #666;
max-width: 500rpx;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.message.unread {
color: #333;
font-weight: 500;
}
.unread-count {
min-width: 36rpx;
height: 36rpx;
line-height: 36rpx;
text-align: center;
background-color: #FF3B30;
color: #fff;
font-size: 24rpx;
border-radius: 18rpx;
padding: 0 8rpx;
}
</style>