import React, { useEffect, useState } from 'react'; import { PageContainer } from '@ant-design/pro-components'; import { Table, Button, Modal, Form, Input, Tag, Space, Popconfirm, message, Avatar } from 'antd'; import { listAllPostsUsingGet, updatePostUsingPost, deletePostUsingPost, addPostUsingPost } from '@/services/hebi/postController'; import dayjs from 'dayjs'; // 新增 const ForumAdmin: React.FC = () => { const [posts, setPosts] = useState([]); const [loading, setLoading] = useState(false); const [editingPost, setEditingPost] = useState(null); const [modalVisible, setModalVisible] = useState(false); const [form] = Form.useForm(); const [search, setSearch] = useState(''); // 获取帖子列表 const fetchPosts = async () => { setLoading(true); const res = await listAllPostsUsingGet(); if (res && res.code === 0 && Array.isArray(res.data)) { setPosts(res.data); } setLoading(false); }; useEffect(() => { fetchPosts(); }, []); // 编辑 const handleEdit = (record: any) => { setEditingPost(record); setModalVisible(true); form.setFieldsValue({ ...record, tags: record.tagList?.join(','), }); }; // 新增 const handleAdd = () => { setEditingPost(null); setModalVisible(true); form.resetFields(); }; // 删除 const handleDelete = async (id: number) => { const res = await deletePostUsingPost({ id }); if (res && res.code === 0) { message.success('删除成功'); fetchPosts(); } else { message.error(res?.message || '删除失败'); } }; // 提交表单 const handleOk = async () => { const values = await form.validateFields(); const tags = values.tags ? values.tags.split(',').map((t: string) => t.trim()) : []; if (editingPost) { // 编辑 const res = await updatePostUsingPost({ ...editingPost, ...values, tags }); if (res && res.code === 0) { message.success('更新成功'); setModalVisible(false); fetchPosts(); } else { message.error(res?.message || '更新失败'); } } else { // 新增 const res = await addPostUsingPost({ ...values, tags }); if (res && res.code === 0) { message.success('新增成功'); setModalVisible(false); fetchPosts(); } else { message.error(res?.message || '新增失败'); } } }; const columns = [ { title: 'ID', dataIndex: 'id', width: 60, }, { title: '标题', dataIndex: 'title', width: 200, }, { title: '标签', dataIndex: 'tagList', render: (tags: string[]) => ( <> {tags?.map(tag => ( {tag} ))} ), }, { title: '作者', dataIndex: ['user', 'userName'], width: 100, render: (_: any, record: any) => ( {record.user?.userName} ), }, { title: '创建时间', dataIndex: 'createTime', width: 180, render: (time: string) => time ? dayjs(time).format('YYYY-MM-DD HH:mm:ss') : '-', }, { title: '操作', key: 'action', width: 160, render: (_: any, record: any) => ( handleDelete(record.id)}> ), }, ]; // 前端搜索过滤 const filteredPosts = posts.filter(post => { const keyword = search.trim().toLowerCase(); if (!keyword) return true; const titleMatch = post.title?.toLowerCase().includes(keyword); const tagsMatch = (post.tagList || []).some((tag: string) => tag.toLowerCase().includes(keyword) ); return titleMatch || tagsMatch; }); return (
setSearch(e.target.value)} />
setModalVisible(false)} destroyOnClose >
); }; export default ForumAdmin;