'use client' import { useState, useEffect } from 'react' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Input } from '@/components/ui/input' import { Button } from '@/components/ui/button' import { Textarea } from '@/components/ui/textarea' import { RefreshCw, Lock, Newspaper, Plus, Trash2, LogOut, Rss, Settings, Check, X, Eye, Globe } from 'lucide-react' type News = { id: number title: string category: string excerpt: string createdAt: string } type AutoFetchedNews = { id: number title: string source: string url: string imageUrl: string excerpt: string category: string publishTime: string status: string isDigitalEconomy: boolean createdAt: string } export default function AdminPage() { const [activeTab, setActiveTab] = useState<'news' | 'autofetch'>('news') const [isAuthenticated, setIsAuthenticated] = useState(false) const [password, setPassword] = useState('') const [newsList, setNewsList] = useState([]) const [autoFetchedNews, setAutoFetchedNews] = useState([]) const [isLoading, setIsLoading] = useState(false) // 新闻发布表单状态 const [showAddNews, setShowAddNews] = useState(false) const [newNews, setNewNews] = useState({ title: '', category: '公司新闻', excerpt: '', content: '' }) // 自动抓取状态 const [isAutoFetching, setIsAutoFetching] = useState(false) const [fetchStatus, setFetchStatus] = useState<{ lastFetch: string | null nextFetch: string | null }>({ lastFetch: null, nextFetch: null }) const handleLogin = (e: React.FormEvent) => { e.preventDefault() if (password === 'admin123') { setIsAuthenticated(true) fetchAllData() } else { alert('密码错误') } } const fetchAllData = async () => { setIsLoading(true) try { const [newsRes, autoFetchRes] = await Promise.all([ fetch('/api/news'), fetch('/api/auto-fetch') ]) if (newsRes.ok) { const data = await newsRes.json() setNewsList(data.news || []) } if (autoFetchRes.ok) { const data = await autoFetchRes.json() // 合并待审核和已批准的内容 const allNews = [ ...(data.pending || []), ...(data.approved || []).filter((a: AutoFetchedNews) => !data.pending?.some((p: AutoFetchedNews) => p.id === a.id) ) ] setAutoFetchedNews(allNews) setFetchStatus({ lastFetch: data.lastFetch, nextFetch: data.nextFetch }) } } catch (error) { console.error('获取数据失败', error) } finally { setIsLoading(false) } } const handleAddNews = async (e: React.FormEvent) => { e.preventDefault() try { const res = await fetch('/api/news', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(newNews) }) if (res.ok) { setNewNews({ title: '', category: '公司新闻', excerpt: '', content: '' }) setShowAddNews(false) fetchAllData() } } catch (error) { alert('发布失败') } } // 自动抓取相关函数 const triggerAutoFetch = async () => { setIsAutoFetching(true) try { const res = await fetch('/api/auto-fetch', { method: 'POST' }) const data = await res.json() if (data.success) { alert(`成功抓取 ${data.articles?.length || 0} 条资讯`) fetchAllData() } else { alert('抓取失败:' + data.error) } } catch (error) { alert('抓取失败,请稍后重试') } finally { setIsAutoFetching(false) } } const approveArticle = async (id: number) => { try { const res = await fetch(`/api/auto-fetch/${id}/approve`, { method: 'POST' }) if (res.ok) { fetchAllData() } } catch (error) { alert('操作失败') } } const rejectArticle = async (id: number) => { const reason = prompt('请输入拒绝原因:') if (reason === null) return try { const res = await fetch(`/api/auto-fetch/${id}/reject`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ reason }) }) if (res.ok) { fetchAllData() } } catch (error) { alert('操作失败') } } const deleteArticle = async (id: number) => { if (!confirm('确定要删除这条资讯吗?')) return try { const res = await fetch(`/api/auto-fetch/${id}`, { method: 'DELETE' }) if (res.ok) { fetchAllData() } } catch (error) { alert('删除失败') } } if (!isAuthenticated) { return (
白马家园后台管理
setPassword(e.target.value)} />
) } return (
{/* Sidebar */} {/* Main Content */}

{activeTab === 'news' && '公司/行业动态'} {activeTab === 'autofetch' && '资讯自动抓取'}

{activeTab === 'news' && '发布和管理最新的公司动态与行业资讯'} {activeTab === 'autofetch' && '自动抓取白马精选公众号等资讯,智能筛选后展示'}

{activeTab === 'news' && ( )} {activeTab === 'autofetch' && ( )}
{/* 新闻发布表单 */} {activeTab === 'news' && showAddNews && ( 撰写新动态
setNewNews({...newNews, title: e.target.value})} />
setNewNews({...newNews, excerpt: e.target.value})} />