import React, { useState, useEffect } from 'react' // 资讯页面 - 展示寻配号和行情信息(所有人可见) export default function News() { const [activeTab, setActiveTab] = useState('deal') const [showSeekPublish, setShowSeekPublish] = useState(false) const [seekForm, setSeekForm] = useState({ edition: '龙钞', type: '标百', category: '寻号', price: '', matchType: '模糊', features: 'J0', content: '', title: '', contact: '' }) const [infoList, setInfoList] = useState([]) const [loading, setLoading] = useState(false) const API_BASE = localStorage.getItem('API_BASE') || '' // 获取资讯列表 useEffect(() => { fetchInfoList() }, [activeTab]) // 自动生成寻号标题 useEffect(() => { const title = `「寻号 ${seekForm.edition || '龙钞,马钞,蛇钞'} ${seekForm.type} ${seekForm.features || 'J0'} ${seekForm.matchType}」` setSeekForm(prev => ({...prev, title})) }, [seekForm.edition, seekForm.type, seekForm.features, seekForm.matchType]) const fetchInfoList = async () => { setLoading(true) try { const token = localStorage.getItem('token') // 根据tab获取不同类型的数据 const type = activeTab === 'seek' ? 'seek' : 'deal' const res = await fetch(`${API_BASE}/api/information/list?info_type=${type}`, { headers: { Authorization: `Bearer ${token}` } }) const data = await res.json() setInfoList(data || []) } catch (e) { console.error(e) } setLoading(false) } const handleSeekPublish = async () => { if (!seekForm.contact) { alert('请填写联系方式'); return } try { const token = localStorage.getItem('token') const content = (seekForm.content ? seekForm.content + '\n' : '') + (seekForm.price ? `价格: ${seekForm.price}` : '') + (seekForm.features ? '\n号码特征: ' + seekForm.features : '') + '\n联系方式: ' + seekForm.contact const res = await fetch(`${API_BASE}/api/information/`, { method: 'POST', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ title: seekForm.title, content, info_type: 'seek' }) }) const data = await res.json() if (data.id || data.code === 0) { alert('发布成功!') setShowSeekPublish(false) setSeekForm({ edition: '龙钞', type: '标百', category: '寻号', price: '', matchType: '模糊', features: 'J0', content: '', title: '', contact: '' }) fetchInfoList() } else { alert(data.message || '发布失败') } } catch (e) { alert('发布失败: ' + e.message) } } // Tab切换 const tabs = [ { key: 'seek', label: '🔍 寻配号' }, { key: 'deal', label: '💰 成交行情' } ] const formatDate = (dateStr) => { if (!dateStr) return '-' const date = new Date(dateStr) return `${date.getMonth() + 1}/${date.getDate()}` } return (