import React, { useState, useEffect } from 'react' // 资讯页面 - 寻配号、成交数据、发布中心 export default function News() { const [activeTab, setActiveTab] = useState('seek') // seek-寻配号, deal-成交, publish-发布 const [infoList, setInfoList] = useState([]) const [loading, setLoading] = useState(false) const [showPublish, setShowPublish] = useState(false) const [myList, setMyList] = useState([]) // 发布表单 const [formData, setFormData] = useState({ info_type: 'seek', title: '', content: '', collection_id: '', expect_category: '', expect_version: '', expect_packaging: '', expect_number: '', expect_price_min: '', expect_price_max: '', deal_price: '', deal_date: '' }) // 我的藏品列表(用于关联) const [myCollections, setMyCollections] = useState([]) const API_BASE = localStorage.getItem('API_BASE') || '' useEffect(() => { fetchInfoList() fetchMyCollections() fetchMyList() }, [activeTab]) // 获取资讯列表 const fetchInfoList = async () => { setLoading(true) try { const token = localStorage.getItem('token') const res = await fetch(`${API_BASE}/api/information/list?info_type=${activeTab}`, { headers: { 'Authorization': `Bearer ${token}` } }) const data = await res.json() setInfoList(data || []) } catch (e) { console.error(e) } setLoading(false) } // 获取我的藏品列表 const fetchMyCollections = async () => { try { const token = localStorage.getItem('token') const res = await fetch(`${API_BASE}/api/collections?page=1&page_size=100`, { headers: { 'Authorization': `Bearer ${token}` } }) const data = await res.json() setMyCollections(data.items || []) } catch (e) { console.error(e) } } // 获取我的发布列表 const fetchMyList = async () => { try { const token = localStorage.getItem('token') const res = await fetch(`${API_BASE}/api/information/my/list`, { headers: { 'Authorization': `Bearer ${token}` } }) const data = await res.json() setMyList(data || []) } catch (e) { console.error(e) } } // 发布资讯 const handlePublish = async () => { if (!formData.title) { alert('请输入标题') return } try { const token = localStorage.getItem('token') const res = await fetch(`${API_BASE}/api/information/`, { method: 'POST', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ ...formData, expect_price_min: formData.expect_price_min ? parseFloat(formData.expect_price_min) : null, expect_price_max: formData.expect_price_max ? parseFloat(formData.expect_price_max) : null, deal_price: formData.deal_price ? parseFloat(formData.deal_price) : null, }) }) if (res.ok) { alert('发布成功!') setShowPublish(false) setFormData({ info_type: 'seek', title: '', content: '', collection_id: '', expect_category: '', expect_version: '', expect_packaging: '', expect_number: '', expect_price_min: '', expect_price_max: '', deal_price: '', deal_date: '' }) fetchInfoList() fetchMyList() } } catch (e) { alert('发布失败') } } // 删除发布 const handleDelete = async (id) => { if (!confirm('确定删除?')) return try { const token = localStorage.getItem('token') await fetch(`${API_BASE}/api/information/${id}`, { method: 'DELETE', headers: { 'Authorization': `Bearer ${token}` } }) fetchMyList() } catch (e) { console.error(e) } } // 格式化日期 const formatDate = (dateStr) => { if (!dateStr) return '' return new Date(dateStr).toLocaleDateString('zh-CN') } // 资讯类型映射 const typeMap = { seek: '寻配号', deal: '成交数据', publish: '发布' } return (
{item.content}
{item.content}
)} {/* 寻配号条件 */} {item.info_type === 'seek' && (item.expect_category || item.expect_version || item.expect_number) && (