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 [formData, setFormData] = useState({
edition: '龙钞',
type: '标百',
isGraded: true,
category: '成交',
source: '直播',
prices: {},
title: '',
content: ''
})
// 自动生成标题
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') || ''
// 获取我的发布列表
useEffect(() => {
if (activeTab === 'manage') {
fetchMyList()
}
}, [activeTab])
const fetchMyList = async () => {
setLoading(true)
try {
const token = localStorage.getItem('token')
const res = await fetch(`${API_BASE}/api/information/my`, {
headers: { Authorization: `Bearer ${token}` }
})
const data = await res.json()
setMyList(data || [])
} catch (e) {
console.error(e)
}
setLoading(false)
}
// 发布信息
const handlePublish = async () => {
if (!formData.title) {
alert('请填写标题')
return
}
try {
const token = localStorage.getItem('token')
const gradeNote = formData.isGraded ? '(评级币)' : '(裸钞)'
let priceContent = ''
if (formData.prices && Object.keys(formData.prices).length > 0) {
priceContent = '\n价格:'
const priceLabels = {
price_4: '带4',
price_no4: '无4',
price_no47: '无47',
price_no347: '无347',
price_no1347: '无1347',
price_no247: '无247',
price_no1247: '无1247',
price_no12347: '无12347'
}
Object.entries(formData.prices).forEach(([key, value]) => {
if (value) {
priceContent += `${priceLabels[key] || key}:¥${value} `
}
})
}
const res = await fetch(`${API_BASE}/api/information/publish`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
title: formData.title,
content: formData.content + priceContent,
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: '直播',
prices: {},
title: '',
content: ''
})
fetchMyList()
} else {
alert(data.message || '发布失败')
}
} catch (e) {
alert('发布失败: ' + e.message)
}
}
// Tab切换
const tabs = [
{ key: 'publish', label: '📝 发布行情' },
{ key: 'manage', label: '📋 发布管理' }
]
// 按钮选择组件
const ButtonGroup = ({ options, value, onChange, label }) => (
{label &&
}
{options.map(opt => (
))}
)
// 价格网格
const PriceGrid = () => {
const priceItems = [
{ key: 'price_4', label: '带4' },
{ key: 'price_no4', label: '无4' },
{ key: 'price_no47', label: '无47' },
{ key: 'price_no347', label: '无347' },
{ key: 'price_no1347', label: '无1347' },
{ key: 'price_no247', label: '无247' },
{ key: 'price_no1247', label: '无1247' },
{ key: 'price_no12347', label: '无12347' }
]
return (
{priceItems.map(item => (
setFormData({
...formData,
prices: {...formData.prices, [item.key]: e.target.value}
})}
placeholder="¥"
style={{
width: '100%',
padding: '8px',
background: '#0f172a',
border: '1px solid #334155',
borderRadius: '4px',
color: '#fff',
boxSizing: 'border-box',
fontSize: '13px'
}}
/>
))}
)
}
const formatDate = (dateStr) => {
if (!dateStr) return '-'
const date = new Date(dateStr)
return `${date.getMonth() + 1}/${date.getDate()} ${date.getHours()}:${String(date.getMinutes()).padStart(2, '0')}`
}
return (
{/* Tab导航 */}
{tabs.map(tab => (
setActiveTab(tab.key)}
style={{
padding: '10px 20px',
borderRadius: '8px',
background: activeTab === tab.key ? '#3b82f6' : 'transparent',
color: '#fff',
cursor: 'pointer',
whiteSpace: 'nowrap',
fontSize: '14px'
}}
>
{tab.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}
{formatDate(item.created_at)} | {item.info_type === 'deal' ? '💰 成交' : '🔍 求购'}
))}
)}
)}
)
}