import { useCallback, useEffect, useState } from 'react'; import { Alert, Button, Card, Descriptions, Drawer, Empty, Input, InputNumber, Modal, QRCode, Select, Space, Switch, Table, Tabs, Tag, Timeline, Typography } from 'antd'; import { CheckCircleOutlined, CopyOutlined, LinkOutlined, QrcodeOutlined, ReloadOutlined, SyncOutlined, UserSwitchOutlined } from '@ant-design/icons'; import type { ColumnsType } from 'antd/es/table'; import { douyuApi, platformFulfillmentApi } from '../api/modules'; import type { DouyuGoodsItem, PlatformFulfillmentDetail, PlatformFulfillmentMapping, PlatformFulfillmentTask } from '../api/modules'; import { getErrorMessage } from '../utils/error'; import { message } from '../utils/antdMessage'; import { formatTime } from '../utils/time'; import { getUser } from '../store/auth'; import { usePermissions } from '../hooks/usePermissions'; const { Text, Title } = Typography; const STATUS_OPTIONS = [ { value: '', label: '全部' }, { value: 'pending_mapping', label: '待映射' }, { value: 'ready', label: '待分配' }, { value: 'allocated', label: '已分配' }, { value: 'waiting_bind', label: '待扫码' }, { value: 'role_ready', label: '待确认绑定' }, { value: 'binding_confirming', label: '确认绑定中' }, { value: 'bound', label: '待兑换' }, { value: 'manual_required', label: '待人工' }, { value: 'completed', label: '已完成' }, { value: 'cancelled', label: '已取消' }, ]; const STATUS_COLORS: Record = { pending_mapping: 'orange', ready: 'blue', allocated: 'cyan', waiting_bind: 'gold', role_ready: 'purple', binding_confirming: 'processing', bound: 'green', manual_required: 'red', completed: 'success', cancelled: 'default', }; function statusLabel(value: string) { return STATUS_OPTIONS.find((item) => item.value === value)?.label || value; } function money(fen: number) { return `¥${(Number(fen || 0) / 100).toFixed(2)}`; } export default function PlatformFulfillmentPage() { const user = getUser(); const { can } = usePermissions(user); const [status, setStatus] = useState(''); const [keyword, setKeyword] = useState(''); const [page, setPage] = useState(1); const [pageSize, setPageSize] = useState(20); const [rows, setRows] = useState([]); const [total, setTotal] = useState(0); const [loading, setLoading] = useState(false); const [syncing, setSyncing] = useState(false); const [selectedId, setSelectedId] = useState(null); const [detail, setDetail] = useState(null); const [detailLoading, setDetailLoading] = useState(false); const [qrOpen, setQrOpen] = useState(false); const [mappingOpen, setMappingOpen] = useState(false); const [mappingSku, setMappingSku] = useState(''); const [mappingGoodsId, setMappingGoodsId] = useState(''); const [mappingPoints, setMappingPoints] = useState(null); const [mappingEnabled, setMappingEnabled] = useState(true); const [goods, setGoods] = useState([]); const [mappingGoodsLoading, setMappingGoodsLoading] = useState(false); const [mappings, setMappings] = useState([]); const loadTasks = useCallback(async () => { setLoading(true); try { const result = await platformFulfillmentApi.listTasks({ status: status || undefined, keyword: keyword.trim() || undefined, page, page_size: pageSize, }); setRows(result.items); setTotal(result.pagination.total); } catch (error) { message.error(getErrorMessage(error)); } finally { setLoading(false); } }, [keyword, page, pageSize, status]); const loadDetail = useCallback(async (taskId: number) => { setDetailLoading(true); try { const result = await platformFulfillmentApi.getTask(taskId); setDetail(result); } catch (error) { message.error(getErrorMessage(error)); } finally { setDetailLoading(false); } }, []); useEffect(() => { void loadTasks(); }, [loadTasks]); useEffect(() => { if (selectedId) void loadDetail(selectedId); }, [loadDetail, selectedId]); const selected = detail?.task || null; const qrValue = selected?.bindTask?.qrUrl || ''; useEffect(() => { if (qrValue) setQrOpen(true); }, [qrValue]); const sync = async () => { setSyncing(true); try { const result = await platformFulfillmentApi.sync(200); message.success(`同步完成:新增 ${result.created},更新 ${result.updated}`); await loadTasks(); } catch (error) { message.error(getErrorMessage(error)); } finally { setSyncing(false); } }; const runTaskAction = async (action: () => Promise) => { try { const result = await action(); setDetail(result); setSelectedId(result.task.id); await loadTasks(); message.success('操作已完成'); } catch (error) { message.error(getErrorMessage(error)); } }; const openMapping = async (task?: PlatformFulfillmentTask) => { setMappingSku(task?.internalSkuKey || ''); setMappingGoodsId(task?.commodityId || ''); setMappingPoints(task?.requiredPoints ?? null); setMappingEnabled(true); setMappingOpen(true); setMappingGoodsLoading(true); try { const [goodsRows, mappingRows] = await Promise.all([douyuApi.listLiveGoods(), platformFulfillmentApi.listMappings(task?.internalSkuKey || '')]); setGoods(goodsRows); setMappings(mappingRows); if (goodsRows.length === 0) { message.warning('斗鱼活动商品为空,请稍后刷新商品列表'); } } catch (error) { message.error(getErrorMessage(error)); } finally { setMappingGoodsLoading(false); } }; const reloadMappingGoods = async () => { setMappingGoodsLoading(true); try { const rows = await douyuApi.listLiveGoods(); setGoods(rows); if (rows.length === 0) { message.warning('斗鱼活动商品为空,请稍后重试'); } else { message.success(`已加载 ${rows.length} 个斗鱼活动商品`); } } catch (error) { message.error(getErrorMessage(error)); } finally { setMappingGoodsLoading(false); } }; const selectGoods = (commodityId: string) => { const item = goods.find((row) => row.commodity_id === commodityId); setMappingGoodsId(commodityId); if (item?.score != null) setMappingPoints(Number(item.score)); }; const saveMapping = async () => { if (!mappingSku.trim() || !mappingGoodsId) { message.warning('请填写内部 SKU 并选择斗鱼商品'); return; } const item = goods.find((row) => row.commodity_id === mappingGoodsId); try { await platformFulfillmentApi.upsertMapping({ internal_sku_key: mappingSku.trim(), commodity_id: mappingGoodsId, goods_name: item?.name || '', required_points: mappingPoints, enabled: mappingEnabled, }); message.success('商品映射已保存'); setMappings(await platformFulfillmentApi.listMappings(mappingSku.trim())); await loadTasks(); } catch (error) { message.error(getErrorMessage(error)); } }; const columns: ColumnsType = [ { title: '订单 / 商品', key: 'order', width: 280, render: (_, task) => ( {task.platformOrderId || `#${task.sourceOrderId}`} {task.skuName || task.internalSkuKey || '-'} {task.provider || '-'} / {task.shopName || task.shopId || '-'} ), }, { title: '商品映射', key: 'mapping', width: 230, render: (_, task) => ( {task.commodityId || '未映射'} {task.requiredPoints != null ? `${task.requiredPoints} 积分` : '价格未知'} ), }, { title: '订单金额', dataIndex: 'orderAmountFen', width: 110, render: (value: number) => money(value), }, { title: '账号', key: 'account', width: 190, render: (_, task) => task.account ? ( {task.account.nickname || task.account.username} 积分 {task.account.points ?? '-'} · 鱼翅 {task.account.goldBalance ?? '-'} 租约 {task.lease?.status || '-'} · {formatTime(task.lease?.expiresAt)} ) : ( 未分配 ), }, { title: '绑定', key: 'binding', width: 180, render: (_, task) => ( {task.bindingStatus} {task.bindTask?.roleName || task.bindTask?.bindPhase || '-'} ), }, { title: '状态', key: 'status', width: 150, render: (_, task) => ( {statusLabel(task.executionStatus)} 兑换:{task.exchangeStatus} ), }, { title: '更新时间', dataIndex: 'updatedAt', width: 160, render: (value: string) => formatTime(value), }, { title: '操作', key: 'actions', width: 330, render: (_, task) => ( {task.executionStatus === 'pending_mapping' ? ( ) : null} {task.executionStatus === 'ready' ? ( ) : null} {task.executionStatus === 'allocated' ? ( ) : null} {['waiting_bind', 'binding_confirming'].includes(task.executionStatus) ? ( ) : null} {task.executionStatus === 'role_ready' ? ( ) : null} {task.executionStatus === 'bound' ? ( <> ) : null} ), }, ]; const actionButtons = selected ? ( {selected.bindTask?.qrUrl ? ( ) : null} {selected.account ? : null} {selected.executionStatus !== 'completed' && selected.executionStatus !== 'cancelled' ? ( ) : null} ) : null; return (
直播账号履约工作台 读取 order_site 人工订单,客户扫码绑定游戏角色,运营使用斗鱼账号人工兑换。
{ setKeyword(event.target.value); setPage(1); }} style={{ width: 300 }} /> setMappingSku(event.target.value)} />