import React, { useState, useEffect } from 'react'
export default function Info() {
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 [formData, setFormData] = useState({
edition: '龙钞', type: '标百', isGraded: true, category: '成交', source: '直播', title: '', content: ''
})
useEffect(() => {
if (activeTab === 'manage') fetchMyList()
}, [activeTab])
useEffect(() => {
const now = new Date()
const date = `${now.getFullYear()}/${now.getMonth() + 1}/${now.getDate()}`
const gradeNote = formData.isGraded ? '(评级币)' : '(裸钞)'
const title = `「${date} ${formData.edition} ${formData.type} ${formData.category} 行情信息${gradeNote}」`
setFormData(prev => ({...prev, title}))
}, [formData.edition, formData.type, formData.isGraded, formData.category])
const API_BASE = localStorage.getItem('API_BASE') || ''
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}` } })
setMyList(res.ok ? await res.json() : [])
} catch (e) { console.error(e) }
setLoading(false)
}
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({ title: formData.title, content: formData.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: '', content: '' })
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 handleSaveEdit = 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 BtnGroup = ({ options, value, onChange, label }) => (
{label}
{options.map(opt => (
))}
)
const Card = ({ children, title, action }) => (
{title &&
{title}
{action}
}
{children}
)
const formatDate = (d) => d ? new Date(d).toLocaleString('zh-CN').slice(0, 16) : '-'
return (
{/* 顶部标签 */}
{[{ key: 'publish', label: '📝 发布行情', icon: '📝' }, { key: 'manage', label: '📋 发布管理', icon: '📋' }].map(t => (
setActiveTab(t.key)}
style={{ flex: 1, padding: '12px 16px', borderRadius: '10px', background: activeTab === t.key ? 'linear-gradient(135deg, #3b82f6 0%, #2563eb 100%)' : 'transparent',
color: activeTab === t.key ? '#fff' : '#9ca3af', cursor: 'pointer', textAlign: 'center', fontSize: '14px', fontWeight: '600', transition: 'all 0.3s', boxShadow: activeTab === t.key ? '0 2px 10px rgba(59,130,246,0.4)' : 'none' }}>
{t.label}
))}
{activeTab === 'publish' && (
{/* 新增按钮 */}
{showPublish && (
setFormData({...formData, edition: v})} />
类型
{['标百', '标十', '单张'].map(t => (
))}
是否评级
setFormData({...formData, category: v})} />
setFormData({...formData, source: v})} />
)}
)}
{activeTab === 'manage' && (
{loading ? (
⏳ 加载中...
) : myList.length === 0 ? (
暂无发布记录
) : (
{myList.map(item => (
{item.title}
{item.info_type === 'deal' ? '💰 成交' : '🔍 求购'}
{item.content &&
{item.content}
}
{formatDate(item.created_at)}
))}
)}
)}
{/* 编辑弹窗 */}
{editingItem && (
✏️ 编辑信息
setEditingItem({...editingItem, title: e.target.value})}
style={{ width: '100%', padding: '14px', background: '#0f172a', border: '1px solid #374151', borderRadius: '10px', color: '#fff', boxSizing: 'border-box', fontSize: '14px' }} />
)}
)
}