Files
hfb_sys/frontend/src/features/chats/views/MobileMessagesView.vue
T

479 lines
11 KiB
Vue

<script setup lang="ts">
import { computed, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import MobileBottomNav from '@/components/MobileBottomNav.vue'
import { fetchChats, type ChatConversation } from '@/features/chats/api/chats'
import { formatDateMinute } from '@/shared/utils/time'
import { useDistributionSiteStore } from '@/stores/distributionSite'
const router = useRouter()
const distributionStore = useDistributionSiteStore()
const currentUserId = Number(localStorage.getItem('user_id') || 0)
const loading = ref(false)
const refreshing = ref(false)
const finished = ref(false)
const conversations = ref<ChatConversation[]>([])
const page = ref(1)
const pageSize = 20
const total = ref(0)
onMounted(() => {
onRefresh()
})
async function loadChats(isRefresh = false) {
if (isRefresh) {
page.value = 1
finished.value = false
}
loading.value = true
try {
const res = await fetchChats(
page.value,
pageSize,
distributionStore.isDistribution ? distributionStore.siteCode : undefined
)
conversations.value = isRefresh ? res.items : [...conversations.value, ...res.items]
total.value = res.total
if (conversations.value.length >= res.total || res.items.length === 0) {
finished.value = true
} else {
page.value += 1
}
} catch {
showToast({ message: '获取会话失败', icon: 'cross' })
finished.value = true
} finally {
loading.value = false
refreshing.value = false
}
}
function onRefresh() {
refreshing.value = true
loadChats(true)
}
function onLoad() {
if (loading.value || finished.value) return
loadChats(false)
}
function openConversation(item: ChatConversation) {
router.push(`/m/chats/${item.id}`)
}
function roleLabel(role: string) {
const map: Record<string, string> = {
renter: '租客',
owner: '号主',
support: '客服',
customer: '咨询',
}
return map[role] || '成员'
}
function conversationTypeLabel(item: ChatConversation) {
if (item.type === 'listing_group') return '账号群'
if (item.type === 'order_group' || item.order_id) return '订单'
return '平台客服'
}
function conversationAvatar(item: ChatConversation) {
const candidates =
item.participants?.filter(
participant => participant.participant_type === 'user' && participant.avatar_url
) || []
const otherUser = candidates.find(participant => participant.participant_id !== currentUserId)
return resolveAvatarURL((otherUser || candidates[0])?.avatar_url)
}
function resolveAvatarURL(url: string | undefined | null) {
if (!url) return ''
if (url.includes('/api/files/object?key=avatar/')) {
return url.replace('/api/files/object', '/api/public/files/object')
}
return url
}
function previewText(item: ChatConversation) {
return (
item.last_message_preview ||
(item.type === 'general_support' ? '客服会话已创建' : '订单群聊已创建')
)
}
const unreadTotal = computed(() =>
conversations.value.reduce((sum, item) => sum + item.unread_count, 0)
)
</script>
<template>
<main class="mobile-messages">
<header class="page-header">
<button class="back-btn" type="button" @click="router.back()">
<van-icon name="arrow-left" :size="20" />
</button>
<h1>
{{ distributionStore.isDistribution ? `${distributionStore.siteName} · 消息` : '消息' }}
</h1>
<span class="header-count" :class="{ 'is-clear': unreadTotal === 0 }">
{{ unreadTotal > 0 ? `${unreadTotal} 未读` : '全部已读' }}
</span>
</header>
<section class="inbox-summary">
<span class="summary-icon">
<van-icon name="chat-o" :size="19" />
</span>
<div class="summary-copy">
<strong>会话消息</strong>
<span>共 {{ total }} 个会话</span>
</div>
<span v-if="unreadTotal > 0" class="summary-unread">{{ unreadTotal }} 条未读</span>
<span v-else class="summary-read">已读</span>
</section>
<van-pull-refresh v-model="refreshing" class="scroll-container" @refresh="onRefresh">
<van-list
v-model:loading="loading"
:finished="finished"
finished-text="没有更多会话了"
:immediate-check="false"
@load="onLoad"
>
<van-empty
v-if="!loading && conversations.length === 0"
description="暂无会话"
class="empty-state"
/>
<div v-else class="conversation-list">
<button
v-for="item in conversations"
:key="item.id"
class="conversation-item"
:class="{ 'is-unread': item.unread_count > 0 }"
type="button"
@click="openConversation(item)"
>
<span class="conversation-avatar">
<img v-if="conversationAvatar(item)" :src="conversationAvatar(item)" alt="" />
<van-icon v-else name="chat-o" :size="20" />
</span>
<div class="conversation-main">
<div class="conversation-title-row">
<h2>{{ item.title }}</h2>
<span class="conversation-time">{{
formatDateMinute(item.last_message_at || item.created_at)
}}</span>
</div>
<div class="conversation-meta">
<span class="role-chip">{{ roleLabel(item.role) }}</span>
<span class="conversation-type">{{ conversationTypeLabel(item) }}</span>
<span v-if="item.needs_reply" class="reply-chip">待回复</span>
</div>
<p class="conversation-preview">{{ previewText(item) }}</p>
</div>
<span v-if="item.unread_count > 0" class="unread-badge">
{{ item.unread_count > 99 ? '99+' : item.unread_count }}
</span>
</button>
</div>
</van-list>
</van-pull-refresh>
<MobileBottomNav />
</main>
</template>
<style scoped>
.mobile-messages {
min-height: 100dvh;
padding-bottom: calc(52px + env(safe-area-inset-bottom));
background: #f4f4f2;
color: #252a31;
}
.page-header {
position: sticky;
top: 0;
z-index: 100;
display: grid;
grid-template-columns: 44px minmax(0, 1fr) auto;
align-items: center;
height: 52px;
padding: 0 10px;
background: rgba(255, 255, 255, 0.96);
border-bottom: 1px solid #ececea;
box-shadow: 0 1px 8px rgba(26, 28, 31, 0.03);
backdrop-filter: blur(12px);
}
.page-header h1 {
margin: 0;
overflow: hidden;
color: #24282d;
font-size: 17px;
font-weight: 700;
text-align: center;
text-overflow: ellipsis;
white-space: nowrap;
}
.back-btn {
display: grid;
width: 34px;
height: 34px;
place-items: center;
border: 1px solid #ececea;
border-radius: 8px;
background: #f7f7f5;
color: #4f555b;
cursor: pointer;
}
.header-count {
min-width: 52px;
padding: 4px 8px;
border: 1px solid #ead8cb;
border-radius: 999px;
background: #f8f1eb;
color: #9c542b;
font-size: 10px;
font-weight: 700;
text-align: center;
white-space: nowrap;
}
.header-count.is-clear {
border-color: #e8e8e6;
background: #f6f6f4;
color: #8a8e93;
}
.inbox-summary {
display: flex;
align-items: center;
gap: 10px;
margin: 12px 12px 0;
padding: 11px 12px;
border: 1px solid #e7e6e3;
border-radius: 8px;
background: #ffffff;
box-shadow: 0 4px 14px rgba(28, 31, 35, 0.035);
}
.summary-icon {
display: grid;
flex: 0 0 34px;
width: 34px;
height: 34px;
place-items: center;
border-radius: 8px;
background: #f7f1eb;
color: #a65b31;
}
.summary-copy {
display: grid;
min-width: 0;
gap: 2px;
}
.summary-copy strong {
color: #2a2f35;
font-size: 13px;
font-weight: 700;
}
.summary-copy span {
color: #858a90;
font-size: 10px;
}
.summary-unread,
.summary-read {
margin-left: auto;
padding: 3px 7px;
border-radius: 999px;
font-size: 10px;
font-weight: 700;
white-space: nowrap;
}
.summary-unread {
background: #f8eee6;
color: #9c542b;
}
.summary-read {
background: #f4f4f2;
color: #858a90;
}
.scroll-container {
min-height: calc(100dvh - 52px - 52px - env(safe-area-inset-bottom));
}
.empty-state {
padding-top: 72px;
}
.conversation-list {
display: flex;
flex-direction: column;
gap: 8px;
padding: 10px 12px 20px;
}
.conversation-item {
position: relative;
display: grid;
grid-template-columns: 44px minmax(0, 1fr) auto;
align-items: center;
gap: 12px;
width: 100%;
padding: 12px;
border: 1px solid #e7e6e3;
border-radius: 8px;
background: #ffffff;
text-align: left;
box-shadow: 0 4px 14px rgba(28, 31, 35, 0.035);
cursor: pointer;
transition:
background 0.15s,
border-color 0.15s;
}
.conversation-item.is-unread {
border-color: #e5d5c9;
}
.conversation-item.is-unread::before {
position: absolute;
top: 12px;
bottom: 12px;
left: 0;
width: 3px;
border-radius: 0 3px 3px 0;
background: #b96538;
content: '';
}
.conversation-item:active {
background: #faf8f6;
}
.conversation-avatar {
display: grid;
width: 44px;
height: 44px;
place-items: center;
overflow: hidden;
border: 1px solid #eadfd7;
border-radius: 8px;
background: #f8f2ed;
color: #a65b31;
}
.conversation-avatar img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
}
.conversation-main {
min-width: 0;
}
.conversation-title-row {
display: flex;
align-items: center;
gap: 8px;
}
.conversation-title-row h2 {
flex: 1;
min-width: 0;
margin: 0;
overflow: hidden;
color: #252a31;
font-size: 14px;
font-weight: 700;
text-overflow: ellipsis;
white-space: nowrap;
}
.conversation-time {
flex: none;
color: #95999e;
font-size: 10px;
}
.conversation-meta {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 6px;
margin-top: 5px;
color: #74797f;
font-size: 10px;
}
.role-chip {
padding: 2px 6px;
border-radius: 4px;
background: #f3f3f1;
color: #656b71;
font-weight: 600;
}
.conversation-type {
padding: 2px 6px;
border-radius: 4px;
background: #f8f1eb;
color: #985c38;
font-weight: 600;
}
.reply-chip {
padding: 2px 6px;
border-radius: 4px;
background: #f8ece3;
color: #a45227;
font-weight: 700;
}
.conversation-preview {
margin: 7px 0 0;
overflow: hidden;
color: #666c72;
font-size: 12px;
line-height: 1.45;
text-overflow: ellipsis;
white-space: nowrap;
}
.unread-badge {
min-width: 20px;
height: 20px;
padding: 0 6px;
border-radius: 999px;
background: #b96538;
color: #fff;
font-size: 10px;
font-weight: 700;
line-height: 20px;
text-align: center;
}
.scroll-container :deep(.van-list__finished-text) {
padding: 18px 0 4px;
color: #9a9ea3;
font-size: 11px;
}
</style>