feat(messages): 新增 PC 消息中心与订单群聊入口

This commit is contained in:
yml2213
2026-10-30 14:08:16 +08:00
parent 4fc4e9236b
commit f2552fe523
13 changed files with 1258 additions and 11 deletions
@@ -0,0 +1,200 @@
import { useState } from 'react'
import {
Alert,
Avatar,
Badge,
Button,
Card,
Empty,
Skeleton,
Space,
Tag,
Typography,
} from 'antd'
import { ArrowRightOutlined, CustomerServiceOutlined, MessageOutlined } from '@ant-design/icons'
import { Link, useNavigate } from 'react-router-dom'
import { BrandLogo } from '@/components/BrandLogo'
import { ensureSupportConversation, useConversations } from '@/features/messages/api'
import type { ChatConversation } from '@/features/messages/types'
import { useSessionStore } from '@/stores/session'
import { formatRelativeTime } from '@/shared/utils/format'
function conversationMeta(type: string) {
if (type === 'general_support') return { label: '平台客服', color: 'gold' }
if (type === 'order_group') return { label: '订单群聊', color: 'green' }
if (type === 'listing_group') return { label: '商品咨询', color: 'blue' }
return { label: '交易会话', color: 'default' }
}
function ConversationRow({ item }: { item: ChatConversation }) {
const meta = conversationMeta(item.type)
return (
<Link to={`/messages/${item.id}`} className="pc-chat-row">
<Avatar
size={46}
className="pc-chat-row__avatar"
icon={<MessageOutlined />}
style={{ background: item.type === 'general_support' ? '#b08a4a' : '#1e4d42' }}
/>
<div className="pc-chat-row__body">
<div className="pc-chat-row__top">
<strong>{item.title || '未命名会话'}</strong>
<span>{formatRelativeTime(item.last_message_at)}</span>
</div>
<div className="pc-chat-row__meta">
<Tag color={meta.color} bordered={false}>
{meta.label}
</Tag>
{item.latest_order_no ? <span>订单 {item.latest_order_no}</span> : null}
</div>
<p>{item.last_message_preview || '还没有消息,进入会话开始沟通。'}</p>
</div>
<div className="pc-chat-row__tail">
{item.unread_count > 0 ? <Badge count={item.unread_count} /> : null}
<ArrowRightOutlined />
</div>
</Link>
)
}
export function MessagesPage() {
const navigate = useNavigate()
const token = useSessionStore(state => state.token)
const hydrated = useSessionStore(state => state.hydrated)
const [openingSupport, setOpeningSupport] = useState(false)
const chats = useConversations(Boolean(token))
const items = chats.data?.pages.flatMap(page => page.items) ?? []
const unreadTotal = items.reduce((sum, item) => sum + item.unread_count, 0)
const openSupport = async () => {
setOpeningSupport(true)
try {
const conversation = await ensureSupportConversation()
navigate(`/messages/${conversation.id}`)
} catch {
// 全局响应拦截已提示,这里保留按钮可重试状态。
} finally {
setOpeningSupport(false)
}
}
if (!hydrated) {
return (
<div className="pc-messages-page">
<Card loading />
</div>
)
}
if (!token) {
return (
<div className="pc-messages-page">
<Card className="pc-messages-login">
<BrandLogo height={48} variant="seal" />
<Typography.Title level={3}>登录后查看消息</Typography.Title>
<Typography.Paragraph type="secondary">
订单群聊、客服回复与系统通知只对当前账号开放。
</Typography.Paragraph>
<Button
type="primary"
onClick={() => navigate('/login?redirect=/messages')}
>
去登录
</Button>
</Card>
</div>
)
}
return (
<div className="pc-messages-page">
<header className="pc-messages-hero">
<div>
<span className="pc-messages-hero__eyebrow">MESSAGES</span>
<Typography.Title level={2} className="brand-title">
消息中心
</Typography.Title>
<Typography.Paragraph type="secondary">
订单群聊会随下单自动建立,交接、结账与申诉都建议在群内留痕。
</Typography.Paragraph>
</div>
<Space wrap>
{unreadTotal > 0 ? <Tag color="red">{unreadTotal} 条未读</Tag> : <Tag>全部已读</Tag>}
<Button
type="primary"
icon={<CustomerServiceOutlined />}
loading={openingSupport}
onClick={() => void openSupport()}
>
联系平台客服
</Button>
</Space>
</header>
<div className="pc-messages-grid">
<main>
<Card
title="全部会话"
extra={items.length ? <Typography.Text type="secondary">{items.length} 个</Typography.Text> : null}
className="pc-messages-card"
>
{chats.isPending ? (
<Skeleton active paragraph={{ rows: 6 }} />
) : chats.isError ? (
<Alert
type="error"
showIcon
message="会话没有加载出来"
action={
<Button size="small" onClick={() => void chats.refetch()}>
重试
</Button>
}
/>
) : items.length === 0 ? (
<Empty description="还没有会话">
<Button type="primary" loading={openingSupport} onClick={() => void openSupport()}>
联系平台客服
</Button>
</Empty>
) : (
<div className="pc-chat-list">
{items.map(item => (
<ConversationRow item={item} key={item.id} />
))}
{chats.hasNextPage ? (
<Button
block
loading={chats.isFetchingNextPage}
onClick={() => void chats.fetchNextPage()}
>
加载更多会话
</Button>
) : null}
</div>
)}
</Card>
</main>
<aside className="pc-messages-side">
<Card title="沟通建议" className="pc-messages-side__card">
<ul>
<li>交接说明写清账号、密码、区服与可登录时间。</li>
<li>结账前先在群内确认消耗品与押金扣除依据。</li>
<li>图片证据可直接上传,外部截图请保证可长期访问。</li>
</ul>
</Card>
<Card title="客服范围" className="pc-messages-side__card">
<Typography.Paragraph type="secondary">
账号异常、支付问题、申诉进度与退款审核都可以联系平台客服。
</Typography.Paragraph>
<Button block icon={<CustomerServiceOutlined />} onClick={() => void openSupport()}>
新建客服会话
</Button>
</Card>
</aside>
</div>
</div>
)
}