import React, { useState, useEffect } from 'react' export default function YichensBoard() { const [stats, setStats] = useState({ posts: null, categories: [], users: null }) const [posts, setPosts] = useState([]) const [loading, setLoading] = useState(false) const [expandedPosts, setExpandedPosts] = useState({}) const [postTypeFilter, setPostTypeFilter] = useState('all') const API_BASE = localStorage.getItem('API_BASE') || '' useEffect(() => { fetchStats(); fetchPosts() }, []) const fetchStats = async () => { try { const [p, c, u] = await Promise.all([ fetch(`${API_BASE}/api/yichens/stats/posts?days=30`), fetch(`${API_BASE}/api/yichens/stats/categories?days=30`), fetch(`${API_BASE}/api/yichens/stats/users`) ]) setStats({ posts: await p.json(), categories: await c.json(), users: await u.json() }) } catch(e) { console.error(e) } } const fetchPosts = async () => { setLoading(true) let url = `${API_BASE}/api/yichens/posts?limit=50` if (postTypeFilter !== 'all') url += `&post_type=${postTypeFilter}` try { const res = await fetch(url) setPosts((await res.json()) || []) } catch { setPosts([]) } setLoading(false) } const togglePost = id => setExpandedPosts(p => ({ ...p, [id]: !p[id] })) const filtered = postTypeFilter === 'all' ? posts : posts.filter(p => p.post_type === postTypeFilter) return (
{stats.posts && (
30天帖子
{stats.posts.total_posts}
出售{stats.posts.total_deals} 求购{stats.posts.total_wants}
浏览量
{(stats.posts.total_views/10000).toFixed(1)}万
回复{stats.posts.total_replies}
用户数
{stats.users?.total_users||0}
今日新增{stats.users?.new_users_today||0}
)}
📊 分类统计
{stats.categories.map(cat => ( {cat.category} ({cat.count}) ))}
{[{key:'all',label:'全部'},{key:'deal',label:'出售'},{key:'want',label:'求购'}].map(k => ( ))}
{loading ?
加载中...
:
{filtered.map(post => (
togglePost(post.post_id)} style={{ cursor: 'pointer' }}>
{post.title||'无标题'} {post.post_type==='deal'?'出售':post.post_type==='want'?'求购':'普通'}
{post.author_username||'未知'} {post.post_time?.substring(0,16)||''}
{expandedPosts[post.post_id] && (
分类: {post.category||'-'}
价格: {post.price?post.price.toLocaleString()+'元':'待询'}
浏览: {post.view_count||0}
回复: {post.reply_count||0}
{post.url && 查看原帖}
)}
))}
}
) }