import { useState } from 'react' import { Alert, App, Avatar, Badge, Button, Card, Empty, Skeleton, Space, Tabs, Tag, Typography, } from 'antd' import { ArrowRightOutlined, CustomerServiceOutlined, MessageOutlined } from '@ant-design/icons' import { Link, useNavigate } from 'react-router-dom' import { useQueryClient } from '@tanstack/react-query' import { BrandLogo } from '@/components/BrandLogo' import { ensureSupportConversation, markAllNotificationsRead, markNotificationRead, useConversations, useNotifications, } from '@/features/messages/api' import type { AppNotification, 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 ( } style={{ background: item.type === 'general_support' ? '#b08a4a' : '#1e4d42' }} />
{item.title || '未命名会话'} {formatRelativeTime(item.last_message_at)}
{meta.label} {item.latest_order_no ? 订单 {item.latest_order_no} : null}

{item.last_message_preview || '还没有消息,进入会话开始沟通。'}

{item.unread_count > 0 ? : null}
) } function NotificationRow({ notice, onRead, }: { notice: AppNotification onRead: (id: number) => void }) { const unread = !notice.read_at return ( ) } export function MessagesPage() { const navigate = useNavigate() const queryClient = useQueryClient() const { message } = App.useApp() const token = useSessionStore(state => state.token) const hydrated = useSessionStore(state => state.hydrated) const [openingSupport, setOpeningSupport] = useState(false) const [tab, setTab] = useState<'chats' | 'notices'>('chats') const chats = useConversations(Boolean(token)) const notices = useNotifications(Boolean(token)) const items = chats.data?.pages.flatMap(page => page.items) ?? [] const noticeItems = notices.data?.pages.flatMap(page => page.items) ?? [] const unreadTotal = items.reduce((sum, item) => sum + item.unread_count, 0) const unreadNotices = noticeItems.filter(item => !item.read_at).length const readNotice = async (id: number) => { try { await markNotificationRead(id) await queryClient.invalidateQueries({ queryKey: ['notifications'] }) } catch (error) { message.error(error instanceof Error ? error.message : '标记已读失败') } } const readAllNotices = async () => { try { await markAllNotificationsRead() await queryClient.invalidateQueries({ queryKey: ['notifications'] }) message.success('通知已全部标记为已读') } catch (error) { message.error(error instanceof Error ? error.message : '操作失败,请稍后再试') } } const openSupport = async () => { setOpeningSupport(true) try { const conversation = await ensureSupportConversation() navigate(`/messages/${conversation.id}`) } catch { // 全局响应拦截已提示,这里保留按钮可重试状态。 } finally { setOpeningSupport(false) } } if (!hydrated) { return (
) } if (!token) { return (
登录后查看消息 订单群聊、客服回复与系统通知只对当前账号开放。
) } return (
MESSAGES 消息中心 订单群聊会随下单自动建立,交接、结账与申诉都建议在群内留痕。
{unreadTotal > 0 ? {unreadTotal} 条未读 : 全部已读}
setTab(key as 'chats' | 'notices')} className="pc-messages-tabs" items={[ { key: 'chats', label: `会话${items.length ? ` · ${items.length}` : ''}` }, { key: 'notices', label: `通知${unreadNotices ? ` · ${unreadNotices}` : ''}`, }, ]} />
{items.length} 个 ) : null ) : unreadNotices > 0 ? ( ) : ( 全部已读 ) } className="pc-messages-card" > {tab === 'chats' ? ( chats.isPending ? ( ) : chats.isError ? ( void chats.refetch()}> 重试 } /> ) : items.length === 0 ? ( ) : (
{items.map(item => ( ))} {chats.hasNextPage ? ( ) : null}
) ) : notices.isPending ? ( ) : notices.isError ? ( void notices.refetch()}> 重试 } /> ) : noticeItems.length === 0 ? ( ) : (
{noticeItems.map(notice => ( void readNotice(id)} /> ))} {notices.hasNextPage ? ( ) : null}
)}
) }