feat(douyu): 统一支付码队列交互
This commit is contained in:
@@ -17,6 +17,7 @@ import {
|
||||
type DouyuTaskItem,
|
||||
} from '../../api/modules';
|
||||
import ExchangeResultImage from '../../components/ExchangeResultImage';
|
||||
import PaymentQrQueue, { type PaymentQrQueueItem } from '../../components/PaymentQrQueue';
|
||||
import QrActions from '../../components/QrActions';
|
||||
import { usePermissions } from '../../hooks/usePermissions';
|
||||
import { useWebSocketLogs } from '../../hooks/useWebSocketLogs';
|
||||
@@ -61,6 +62,12 @@ import {
|
||||
} from './config';
|
||||
|
||||
const { Text } = Typography;
|
||||
const DOUYU_PAYMENT_TASK_TYPES = [
|
||||
'create_elite_qr',
|
||||
'create_esports_qr',
|
||||
'create_gold_qr',
|
||||
] as const;
|
||||
const DOUYU_PAYMENT_TASK_TYPE_SET = new Set<string>(DOUYU_PAYMENT_TASK_TYPES);
|
||||
|
||||
function parseAccountNames(text: string): string[] {
|
||||
const seen = new Set<string>();
|
||||
@@ -72,6 +79,24 @@ function parseAccountNames(text: string): string[] {
|
||||
.filter((name) => name.length > 0 && !seen.has(name) && seen.add(name));
|
||||
}
|
||||
|
||||
function payTaskTypeLabel(task: DouyuTaskItem): string {
|
||||
if (task.task_type === 'create_elite_qr') return '精英宝典开通';
|
||||
if (task.task_type === 'create_esports_qr') return '电竞手册开通';
|
||||
if (task.task_type === 'create_gold_qr') return '鱼翅充值';
|
||||
return TASK_STATUS_LABELS[task.task_type] || task.task_type;
|
||||
}
|
||||
|
||||
function payTaskAccountLabel(task: DouyuTaskItem): string {
|
||||
return task.account_nickname
|
||||
|| task.account_username
|
||||
|| task.account_uid
|
||||
|| `#${task.account_id}`;
|
||||
}
|
||||
|
||||
function isLoopbackPaymentUrl(value: string): boolean {
|
||||
return /^https?:\/\/(localhost|127\.0\.0\.1|0\.0\.0\.0|\[::1\])(:|\/|$)/i.test(value);
|
||||
}
|
||||
|
||||
/** 绑定状态小标签:取值 unknown / unbound / pending / bound。 */
|
||||
function BindStatusTag({ status, label }: { status?: string; label?: string }) {
|
||||
if (!status) return null;
|
||||
@@ -142,7 +167,9 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
const [qrTaskIds, setQrTaskIds] = useState<number[]>([]);
|
||||
const [activeQrTaskId, setActiveQrTaskId] = useState<number | null>(null);
|
||||
const [esportsBindTask, setEsportsBindTask] = useState<DouyuTaskItem | null>(null);
|
||||
const [payTask, setPayTask] = useState<DouyuTaskItem | null>(null);
|
||||
const [payBatchIds, setPayBatchIds] = useState<string[]>([]);
|
||||
const [payOpen, setPayOpen] = useState(false);
|
||||
const [zoomPayTask, setZoomPayTask] = useState<DouyuTaskItem | null>(null);
|
||||
const qrCanvasWrapRef = useRef<HTMLDivElement | null>(null);
|
||||
const esportsQrCanvasWrapRef = useRef<HTMLDivElement | null>(null);
|
||||
const payQrCanvasWrapRef = useRef<HTMLDivElement | null>(null);
|
||||
@@ -493,7 +520,7 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
if (activate) setActiveQrTaskId(task.id);
|
||||
// 绑定二维码与支付/电竞面板互斥,避免两个 Modal 叠加混淆
|
||||
setEsportsBindTask(null);
|
||||
setPayTask(null);
|
||||
setPayOpen(false);
|
||||
}, []);
|
||||
|
||||
const closeQrTask = useCallback((taskId: number) => {
|
||||
@@ -512,15 +539,17 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
const openEsportsBindTask = useCallback((task: DouyuTaskItem) => {
|
||||
autoOpenedEsportsBindTaskIds.current.add(task.id);
|
||||
closeAllQrTasks();
|
||||
setPayTask(null);
|
||||
setPayOpen(false);
|
||||
setEsportsBindTask(task);
|
||||
}, [closeAllQrTasks]);
|
||||
|
||||
const openPayTask = useCallback((task: DouyuTaskItem) => {
|
||||
autoOpenedPayTaskIds.current.add(task.id);
|
||||
const openPayTasks = useCallback((batchId?: string) => {
|
||||
if (batchId) {
|
||||
setPayBatchIds((prev) => (prev.includes(batchId) ? prev : [...prev, batchId]));
|
||||
}
|
||||
closeAllQrTasks();
|
||||
setEsportsBindTask(null);
|
||||
setPayTask(task);
|
||||
setPayOpen(true);
|
||||
}, [closeAllQrTasks]);
|
||||
|
||||
// 自动弹出最新绑定二维码(running 有 url 即可);多账号并行,不互斥
|
||||
@@ -555,17 +584,24 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
|
||||
// 自动弹出支付二维码
|
||||
useEffect(() => {
|
||||
if (!autoOpenPayReady.current || payTask) return;
|
||||
const nextPayTask = visibleTasks
|
||||
if (!autoOpenPayReady.current) return;
|
||||
const nextPayTasks = visibleTasks
|
||||
.filter((task) => (
|
||||
locallyStartedBatchIds.current.has(task.batch_id)
|
||||
&& hasPaymentQrcode(task)
|
||||
&& !paymentArrived(task)
|
||||
&& !autoOpenedPayTaskIds.current.has(task.id)
|
||||
))
|
||||
.sort((a, b) => b.id - a.id)[0];
|
||||
if (nextPayTask) openPayTask(nextPayTask);
|
||||
}, [openPayTask, payTask, visibleTasks]);
|
||||
.sort((a, b) => a.id - b.id);
|
||||
if (nextPayTasks.length === 0) return;
|
||||
for (const task of nextPayTasks) {
|
||||
autoOpenedPayTaskIds.current.add(task.id);
|
||||
}
|
||||
setPayBatchIds((prev) => [
|
||||
...new Set([...prev, ...nextPayTasks.map((task) => task.batch_id)]),
|
||||
]);
|
||||
setPayOpen(true);
|
||||
}, [visibleTasks]);
|
||||
|
||||
// 兑换成功后自动生成结果图片并复制到剪贴板(批量时最后完成的覆盖剪贴板)
|
||||
useEffect(() => {
|
||||
@@ -653,13 +689,10 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
}, [esportsBindTask, tasks]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!payTask) return;
|
||||
const latest = tasks.find((task) => task.id === payTask.id);
|
||||
if (!latest) return;
|
||||
// 任务进终态后不再自动关闭弹窗:二维码刚弹出来就消失会让人以为"闪一下就没了",
|
||||
// 改成在弹窗里显示结果,由用户手动关闭。
|
||||
if (latest !== payTask) setPayTask(latest);
|
||||
}, [payTask, tasks]);
|
||||
if (!zoomPayTask) return;
|
||||
const latest = tasks.find((task) => task.id === zoomPayTask.id);
|
||||
if (latest && latest !== zoomPayTask) setZoomPayTask(latest);
|
||||
}, [tasks, zoomPayTask]);
|
||||
|
||||
// 多批次并发:不再用全局 busy 标志阻塞 UI,刷新频率直接用各子条件判断
|
||||
|
||||
@@ -717,16 +750,26 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
if (['donate_elite_gift', 'donate_esports_chicken_gift', 'donate_esports_firework_gift'].includes(taskType)) {
|
||||
payload.gift_count = giftCount;
|
||||
}
|
||||
// 支付码批次尽量一次全出码,队列只负责展示和等待到账,不让人工等前一个付完才生成下一个。
|
||||
const batchConcurrency = DOUYU_PAYMENT_TASK_TYPE_SET.has(taskType)
|
||||
? Math.min(10, Math.max(concurrency, ids.length))
|
||||
: concurrency;
|
||||
try {
|
||||
const result = await douyuApi.createTasks({
|
||||
account_ids: ids,
|
||||
task_type: taskType,
|
||||
handbook_scope: handbook,
|
||||
concurrency,
|
||||
concurrency: batchConcurrency,
|
||||
payload,
|
||||
});
|
||||
// 只有本页面创建的批次才允许主动弹二维码,防止其他工作台/浏览器的任务打扰当前操作。
|
||||
locallyStartedBatchIds.current.add(result.batch_id);
|
||||
if (DOUYU_PAYMENT_TASK_TYPE_SET.has(taskType)) {
|
||||
setPayBatchIds((prev) => (
|
||||
prev.includes(result.batch_id) ? prev : [...prev, result.batch_id]
|
||||
));
|
||||
setPayOpen(true);
|
||||
}
|
||||
setRunningBatchIds((prev) => new Set(prev).add(result.batch_id));
|
||||
logs.connectBatch(result.batch_id, `/api/douyu/ws/${result.batch_id}`, {
|
||||
clear: true,
|
||||
@@ -1441,7 +1484,7 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
const openTask = () => {
|
||||
if (!task) return;
|
||||
if (hasBindQrcode(task)) openQrTask(task);
|
||||
else if (hasPaymentQrcode(task)) openPayTask(task);
|
||||
else if (hasPaymentQrcode(task)) openPayTasks(task.batch_id);
|
||||
else if (task.task_type === 'get_xpd_bind_qr') openQrTask(task);
|
||||
else openEsportsBindTask(task);
|
||||
};
|
||||
@@ -1526,7 +1569,86 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
if (configOpen && config) setConfigFormValues({ ...config });
|
||||
}, [configOpen, config]);
|
||||
|
||||
const payUrl = resultText(payTask?.result, 'pay_url');
|
||||
const payBatchIdSet = useMemo(() => new Set(payBatchIds), [payBatchIds]);
|
||||
const payTasks = useMemo(
|
||||
() => tasks.filter(
|
||||
(task) => DOUYU_PAYMENT_TASK_TYPE_SET.has(task.task_type)
|
||||
&& payBatchIdSet.has(task.batch_id),
|
||||
),
|
||||
[payBatchIdSet, tasks],
|
||||
);
|
||||
const payFailedTasks = useMemo(
|
||||
() => payTasks.filter(
|
||||
(task) => !hasPaymentQrcode(task)
|
||||
&& ['failed', 'error', 'stopped'].includes(task.status),
|
||||
),
|
||||
[payTasks],
|
||||
);
|
||||
const payQueuedTasks = useMemo(
|
||||
() => payTasks.filter(
|
||||
(task) => !hasPaymentQrcode(task)
|
||||
&& ['planned', 'pending', 'running'].includes(task.status),
|
||||
),
|
||||
[payTasks],
|
||||
);
|
||||
const payReadyTasks = useMemo(
|
||||
() => payTasks.filter(
|
||||
(task) => hasPaymentQrcode(task)
|
||||
&& !paymentArrived(task)
|
||||
&& !['failed', 'error', 'stopped'].includes(task.status),
|
||||
),
|
||||
[payTasks],
|
||||
);
|
||||
const payDoneCount = payTasks.length - payReadyTasks.length - payQueuedTasks.length - payFailedTasks.length;
|
||||
const payTaskById = useMemo(() => new Map(payTasks.map((task) => [task.id, task])), [payTasks]);
|
||||
const payQueueItems = useMemo<PaymentQrQueueItem[]>(() => {
|
||||
const ready = payReadyTasks.map((task) => {
|
||||
const qrValue = taskQrUrl(task);
|
||||
return {
|
||||
key: task.id,
|
||||
title: payTaskAccountLabel(task),
|
||||
subtitle: `${payTaskTypeLabel(task)} · UID ${task.account_uid || '-'}`,
|
||||
description: task.message || '',
|
||||
phase: 'ready' as const,
|
||||
qrValue,
|
||||
copyValue: qrValue,
|
||||
statusLabel: resultText(task.result, 'payment_status_label')
|
||||
|| TASK_STATUS_LABELS[task.status]
|
||||
|| task.status,
|
||||
statusColor: TASK_STATUS_COLORS[task.status] || 'default',
|
||||
warning: isLoopbackPaymentUrl(qrValue)
|
||||
? '二维码是本机地址(localhost),手机无法扫码'
|
||||
: '',
|
||||
};
|
||||
});
|
||||
const queued = payQueuedTasks.map((task) => ({
|
||||
key: `queued-${task.id}`,
|
||||
title: payTaskAccountLabel(task),
|
||||
subtitle: `${payTaskTypeLabel(task)} · UID ${task.account_uid || '-'}`,
|
||||
phase: task.status === 'running' ? 'generating' as const : 'queued' as const,
|
||||
}));
|
||||
const failed = payFailedTasks.map((task) => ({
|
||||
key: `failed-${task.id}`,
|
||||
title: payTaskAccountLabel(task),
|
||||
subtitle: `${payTaskTypeLabel(task)} · UID ${task.account_uid || '-'}`,
|
||||
phase: 'error' as const,
|
||||
error: task.message || '支付码生成失败',
|
||||
}));
|
||||
return [...ready, ...queued, ...failed];
|
||||
}, [payFailedTasks, payQueuedTasks, payReadyTasks]);
|
||||
const zoomPayUrl = taskQrUrl(zoomPayTask);
|
||||
const copyPaymentLink = async (value: string) => {
|
||||
if (!value) {
|
||||
message.warning('没有可复制的支付链接');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await navigator.clipboard.writeText(value);
|
||||
message.success('支付链接已复制');
|
||||
} catch {
|
||||
message.error('复制失败,请手动选中复制');
|
||||
}
|
||||
};
|
||||
const goldAmountLabel = '充值金额';
|
||||
const isRechargeChannelLoading = goldRechargeChannel === null;
|
||||
const isSupplierGoldRecharge = goldRechargeChannel === 'supplier_api';
|
||||
@@ -1536,9 +1658,6 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
const goldRechargeButtonLabel = isRechargeChannelLoading
|
||||
? '加载中'
|
||||
: (isSupplierGoldRecharge ? 'API直充鱼翅' : '扫码充值鱼翅');
|
||||
const payAccountName = payTask
|
||||
? (payTask.account_nickname || payTask.account_username || payTask.account_uid || `#${payTask.account_id}`)
|
||||
: '';
|
||||
const selectedText = selectedIds.length > 0 ? `已选 ${selectedIds.length} 个账号` : '请先勾选账号';
|
||||
const actionByKey = useMemo(() => new Map(quickActions.map((item) => [item.key, item])), [quickActions]);
|
||||
const actionDisabled = (taskType: string) => {
|
||||
@@ -2444,58 +2563,93 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
title={
|
||||
<Space>
|
||||
<CreditCardOutlined />
|
||||
<span>支付二维码</span>
|
||||
<span>
|
||||
支付码
|
||||
{payQueueItems.length > 0
|
||||
? `(${payReadyTasks.length} 个待扫码`
|
||||
+ `${payQueuedTasks.length ? ` · ${payQueuedTasks.length} 个排队中` : ''}`
|
||||
+ `${payDoneCount ? ` · ${payDoneCount} 个已完成` : ''})`
|
||||
: ''}
|
||||
</span>
|
||||
</Space>
|
||||
}
|
||||
open={!!payTask}
|
||||
onCancel={() => setPayTask(null)}
|
||||
footer={null}
|
||||
open={payOpen}
|
||||
onCancel={() => setPayOpen(false)}
|
||||
footer={(
|
||||
<Space wrap>
|
||||
<Text type="secondary">
|
||||
多选账号会并行生成多个码;支付到账后自动从待扫码队列移除
|
||||
</Text>
|
||||
<Button onClick={() => setPayOpen(false)}>关闭</Button>
|
||||
<Button
|
||||
onClick={() => {
|
||||
setPayBatchIds([]);
|
||||
setPayOpen(false);
|
||||
}}
|
||||
>
|
||||
清空列表
|
||||
</Button>
|
||||
</Space>
|
||||
)}
|
||||
centered
|
||||
width={400}
|
||||
width={920}
|
||||
>
|
||||
{payTask && (
|
||||
<PaymentQrQueue
|
||||
items={payQueueItems}
|
||||
emptyText={payDoneCount > 0
|
||||
? '待扫码队列已清空,支付完成的账号已自动移除'
|
||||
: '当前没有待扫码支付码'}
|
||||
queuedText="正在排队生成斗鱼支付码"
|
||||
onZoom={(item) => {
|
||||
const task = payTaskById.get(Number(item.key));
|
||||
if (task) setZoomPayTask(task);
|
||||
}}
|
||||
onCopy={(item) => void copyPaymentLink(item.copyValue || item.qrValue || '')}
|
||||
/>
|
||||
</Modal>
|
||||
|
||||
<Modal
|
||||
open={zoomPayTask !== null}
|
||||
title={`扫码支付 · ${zoomPayTask ? payTaskAccountLabel(zoomPayTask) : ''}`}
|
||||
footer={null}
|
||||
onCancel={() => setZoomPayTask(null)}
|
||||
centered
|
||||
width={680}
|
||||
>
|
||||
{zoomPayTask && (
|
||||
<div style={{ textAlign: 'center' }}>
|
||||
{payAccountName && (
|
||||
<Text type="secondary" style={{ marginBottom: 12, display: 'block' }}>{payAccountName}</Text>
|
||||
)}
|
||||
{paymentArrived(payTask) ? (
|
||||
// 已到账/已开通:二维码不再有意义,留在弹窗里给结论 + 手动关闭
|
||||
<Space direction="vertical" align="center" size={12}>
|
||||
<Alert
|
||||
type="success"
|
||||
showIcon
|
||||
message={payTask.task_type === 'create_elite_qr'
|
||||
? '精英宝典已处理完成'
|
||||
: payTask.task_type === 'create_esports_qr'
|
||||
? '电竞手册已处理完成'
|
||||
: '充值已到账'}
|
||||
description={payTask.message || ''}
|
||||
/>
|
||||
<Button type="primary" onClick={() => setPayTask(null)}>关闭</Button>
|
||||
</Space>
|
||||
) : ['failed', 'error', 'stopped'].includes(payTask.status) ? (
|
||||
<Space direction="vertical" align="center" size={12}>
|
||||
<Alert
|
||||
type={payTask.status === 'stopped' ? 'warning' : 'error'}
|
||||
showIcon
|
||||
message={payTask.status === 'stopped' ? '任务已停止' : '支付码任务未完成'}
|
||||
description={payTask.message || ''}
|
||||
/>
|
||||
<Button onClick={() => setPayTask(null)}>关闭</Button>
|
||||
{paymentArrived(zoomPayTask) ? (
|
||||
<Alert
|
||||
type="success"
|
||||
showIcon
|
||||
message={payTaskTypeLabel(zoomPayTask) + '已完成'}
|
||||
description={zoomPayTask.message || ''}
|
||||
/>
|
||||
) : ['failed', 'error', 'stopped'].includes(zoomPayTask.status) ? (
|
||||
<Alert
|
||||
type={zoomPayTask.status === 'stopped' ? 'warning' : 'error'}
|
||||
showIcon
|
||||
message={zoomPayTask.status === 'stopped' ? '任务已停止' : '支付码任务未完成'}
|
||||
description={zoomPayTask.message || ''}
|
||||
/>
|
||||
) : zoomPayUrl ? (
|
||||
<Space direction="vertical" size={10} style={{ width: '100%' }}>
|
||||
<div
|
||||
ref={payQrCanvasWrapRef}
|
||||
style={{ display: 'inline-block', background: '#fff', padding: 12, lineHeight: 0 }}
|
||||
>
|
||||
<QRCode value={zoomPayUrl} size={520} bordered={false} />
|
||||
</div>
|
||||
<QrActions wrapRef={payQrCanvasWrapRef} url={zoomPayUrl} disabled={!zoomPayUrl} />
|
||||
</Space>
|
||||
) : (
|
||||
<Space direction="vertical" align="center" size={12}>
|
||||
{payUrl ? (
|
||||
<div ref={payQrCanvasWrapRef}>
|
||||
<QRCode value={payUrl} size={220} bordered={false} />
|
||||
</div>
|
||||
) : (
|
||||
<Text type="secondary">支付码生成中...</Text>
|
||||
)}
|
||||
<QrActions wrapRef={payQrCanvasWrapRef} url={payUrl} disabled={!payUrl} />
|
||||
<Text>{payTask.message || '请扫码支付'}</Text>
|
||||
</Space>
|
||||
<Text type="secondary">支付码生成中...</Text>
|
||||
)}
|
||||
<div style={{ marginTop: 10 }}>
|
||||
<Text type="secondary" style={{ fontSize: 12, wordBreak: 'break-all' }}>
|
||||
{zoomPayUrl}
|
||||
</Text>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</Modal>
|
||||
|
||||
Reference in New Issue
Block a user