import React, { useState, useEffect } from 'react' export default function YichensBoard() { const [todayStats, setTodayStats] = useState({ total: 0, deals: 0, wants: 0, others: 0 }) const [todayCategory, setTodayCategory] = useState([]) const [posts, setPosts] = useState([]) const [loading, setLoading] = useState(false) const [expandedPosts, setExpandedPosts] = useState({}) const [postTypeFilter, setPostTypeFilter] = useState('all') const [categoryFilter, setCategoryFilter] = useState('') const [page, setPage] = useState(1) const [totalPosts, setTotalPosts] = useState(0) const [searchKeyword, setSearchKeyword] = useState('') const API_BASE = localStorage.getItem('API_BASE') || '' const today = new Date() const dateStr = today.getFullYear() + '/' + (today.getMonth() + 1) + '/' + today.getDate() useEffect(() => { fetchTodayStats(); fetchPosts(1, '') }, []) const fetchTodayStats = async () => { try { const res = await fetch(API_BASE + '/api/yichens/stats/today') setTodayStats(await res.json()) } catch(e) { console.error(e) } } const fetchTodayCategory = async () => { try { const res = await fetch(API_BASE + '/api/yichens/stats/today-category') setTodayCategory(await res.json()) } catch(e) { console.error(e) } } const fetchPosts = async (p, cat) => { setLoading(true) const currentPage = p !== undefined ? p : page const currentCat = cat !== undefined ? cat : categoryFilter let url = API_BASE + '/api/yichens/posts?limit=390&offset=' + ((currentPage - 1) * 390) if (postTypeFilter === 'deal') url += '&post_type=deal' else if (postTypeFilter === 'want') url += '&post_type=want' else if (postTypeFilter === 'other') url += '&post_type=normal' try { const res = await fetch(url) let data = await res.json() || [] if (currentCat) { if (currentCat === '龙') { data = data.filter(p => p.category && (p.category.includes('龙') || p.category.includes('纪念钞'))) } else if (currentCat === '蛇') { data = data.filter(p => p.category && p.category.includes('蛇')) } else if (currentCat === '马') { data = data.filter(p => p.category && p.category.includes('马')) } else if (currentCat === '其他') { data = data.filter(p => p.category && !p.category.includes('龙') && !p.category.includes('纪念钞') && !p.category.includes('蛇') && !p.category.includes('马')) } } // 搜索关键词过滤 - 强匹配(完全包含) if (searchKeyword) { const kw = searchKeyword.trim() if (kw) { data = data.filter(p => (p.title && p.title.includes(kw)) || (p.content && p.content.includes(kw)) || (p.category && p.category.includes(kw)) || (p.contact && p.contact.includes(kw)) ) } } setPosts(data) setTotalPosts(todayStats.total || 0) } catch { setPosts([]) } setLoading(false) } useEffect(() => { if (todayStats.total > 0) fetchTodayCategory() }, [todayStats]) // 搜索关键词变化时重新获取数据 useEffect(() => { setPage(1) fetchPosts(1, '') }, [searchKeyword]) const togglePost = id => setExpandedPosts(p => ({ ...p, [id]: !p[id] })) const StatCard = ({ label, value, color, onClick }) => (