import React, { useState, useEffect } from 'react' // 信息页面 - 寻配号发布和发布管理(包含寻号/行情区分) export default function Info() { // 检查登录状态,未登录则跳转到登录页 if (!localStorage.getItem('token')) { window.location.hash = '#/login' return null } // 顶部tab:寻配号发布 / 发布管理 const [activeTab, setActiveTab] = useState('publish') const [showPublish, setShowPublish] = useState(false) const [myList, setMyList] = useState([]) const [loading, setLoading] = useState(false) const [editingItem, setEditingItem] = useState(null) const [filterType, setFilterType] = useState('all') // all/seek/deal const [formData, setFormData] = useState({ edition: '龙钞', type: '标百', isGraded: true, category: '成交', source: '直播', title: '' }) // 寻配号发布表单(新版) const [seekForm, setSeekForm] = useState({ edition: '龙钞,马钞,蛇钞', price: '', features: '', contact: '', content: '' }) const API_BASE = localStorage.getItem('API_BASE') || '' useEffect(() => { if (activeTab === 'manage') fetchMyList() }, [activeTab]) useEffect(() => { // 自动生成标题 if (seekForm.edition || seekForm.features) { const title = `「寻号 ${seekForm.edition || '龙钞,马钞,蛇钞'} J0${seekForm.features || 'XXXXXXXX'}」` setSeekForm(prev => ({ ...prev, title })) } }, [seekForm.edition, seekForm.features]) const fetchMyList = async () => { setLoading(true) try { const token = localStorage.getItem('token') const res = await fetch(`${API_BASE}/api/information/my/list`, { headers: { Authorization: `Bearer ${token}` } }) if (res.ok) { const data = await res.json() setMyList(data || []) } } catch (e) { console.error(e) } setLoading(false) } // 发布寻配号 const handlePublishSeek = async () => { if (!seekForm.contact) { 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({ title: seekForm.title, content: document.getElementById('seekContent')?.value || seekForm.content, info_type: 'seek', expect_category: seekForm.edition, expect_number: seekForm.features ? `J0${seekForm.features}` : null }) }) const data = await res.json() if (data.id || data.code === 0) { alert('发布成功!') setShowPublish(false) setSeekForm({ edition: '龙钞,马钞,蛇钞', price: '', features: '', contact: '', content: '', title: '' }) const contentEl = document.getElementById('seekContent') if (contentEl) contentEl.value = '' fetchMyList() } else { alert(data.message || '发布失败') } } catch (e) { alert('发布失败: ' + e.message) } } // 发布行情 const handlePublishDeal = async () => { const content = document.getElementById('publishContent')?.value || '' 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({ title: formData.title, content: content, info_type: formData.category === '成交' ? 'deal' : 'seek' }) }) const data = await res.json() if (data.id || data.code === 0) { alert('发布成功!') setShowPublish(false) setFormData({ edition: '龙钞', type: '标百', isGraded: true, category: '成交', source: '直播', title: '' }) const contentEl = document.getElementById('publishContent') if (contentEl) contentEl.value = '' fetchMyList() } else { alert(data.message || '发布失败') } } catch (e) { alert('发布失败: ' + e.message) } } // 删除 const handleDelete = async (id) => { if (!confirm('确定删除这条信息吗?')) return try { const token = localStorage.getItem('token') const res = await fetch(`${API_BASE}/api/information/${id}`, { method: 'DELETE', headers: { Authorization: `Bearer ${token}` } }) if (res.ok) { alert('删除成功') fetchMyList() } else { alert('删除失败') } } catch (e) { alert('删除失败: ' + e.message) } } // 编辑 const handleEdit = (item) => { setEditingItem({ id: item.id, title: item.title, content: item.content }) } const saveEdit = async () => { if (!editingItem) return try { const token = localStorage.getItem('token') const res = await fetch(`${API_BASE}/api/information/${editingItem.id}`, { method: 'PUT', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ title: editingItem.title, content: editingItem.content }) }) if (res.ok) { alert('保存成功') setEditingItem(null) fetchMyList() } else { alert('保存失败') } } catch (e) { alert('保存失败: ' + e.message) } } const getUserPhone = () => { try { const user = JSON.parse(localStorage.getItem('user') || '{}') return user.phone || user.phoneNumber || user.mobile || user.tel || '' } catch { return '' } } useEffect(() => { setSeekForm(prev => ({ ...prev, contact: getUserPhone() })) }, []) // 格式化日期 const formatDate = (date) => { if (!date) return '-' return new Date(date).toLocaleString('zh-CN').slice(0, 16) } // 过滤后的列表 const filteredList = myList.filter(item => filterType === 'all' || item.info_type === filterType) return (