fix(douyu): 升级工作台任务流并修正换绑冷却
This commit is contained in:
@@ -71,9 +71,19 @@ export const douyuApi = {
|
||||
listPeaceGoods: () => api.get<DouyuXpdGoodsItem[], DouyuXpdGoodsItem[]>('/douyu/xpd-goods'),
|
||||
createTasks: (data: DouyuTaskBatchRequest) =>
|
||||
api.post<DouyuTaskBatchResult, DouyuTaskBatchResult>('/douyu/tasks/batch', data),
|
||||
listTasks: (handbookScope: 'elite' | 'esports' | 'peace', batchId?: string) =>
|
||||
listTasks: (
|
||||
handbookScope: 'elite' | 'esports' | 'peace',
|
||||
batchId?: string,
|
||||
workbenchOnly = false,
|
||||
pageSize = 100,
|
||||
) =>
|
||||
api.get<DouyuTaskItem[], DouyuTaskItem[]>('/douyu/tasks', {
|
||||
params: { handbook_scope: handbookScope, ...(batchId ? { batch_id: batchId } : {}) },
|
||||
params: {
|
||||
handbook_scope: handbookScope,
|
||||
...(batchId ? { batch_id: batchId } : {}),
|
||||
...(workbenchOnly ? { workbench_only: true } : {}),
|
||||
page_size: pageSize,
|
||||
},
|
||||
}),
|
||||
listWorkbenchAccounts: (handbookScope: 'elite' | 'esports' | 'peace') =>
|
||||
api.get<{ account_ids: number[]; configured: boolean }, { account_ids: number[]; configured: boolean }>('/douyu/workbench-accounts', {
|
||||
|
||||
@@ -373,6 +373,10 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
() => tasks.filter((task) => activeTaskTypes.has(task.task_type)),
|
||||
[activeTaskTypes, tasks],
|
||||
);
|
||||
const isLocallyStartedTask = useCallback(
|
||||
(task: DouyuTaskItem) => locallyStartedBatchIds.current.has(task.batch_id),
|
||||
[],
|
||||
);
|
||||
|
||||
// qrTask 由 activeQrTaskId 从最新 tasks 中派生,切换 Tab 即切换关注账号
|
||||
const qrTask = useMemo(
|
||||
@@ -475,63 +479,46 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
return map;
|
||||
}, [tasks]);
|
||||
|
||||
const loadData = useCallback(async () => {
|
||||
const loadAccountsPage = useCallback(async () => {
|
||||
if (!workbenchReady) return;
|
||||
setLoading(true);
|
||||
try {
|
||||
const [accResult, goodsResult, taskResult, cfgResult, typeResult, rechargeChannelResult] = await Promise.allSettled([
|
||||
workbenchIds.length > 0
|
||||
? douyuApi.listAccountsPaged({
|
||||
ids: workbenchIds.join(','),
|
||||
search: accountSearch.trim() || undefined,
|
||||
page: accountPage,
|
||||
page_size: accountPageSize,
|
||||
})
|
||||
: Promise.resolve({ items: [] as DouyuTaskAccountItem[], total: 0, page: accountPage, page_size: accountPageSize }),
|
||||
isPeaceHandbook
|
||||
? douyuApi.listPeaceGoods()
|
||||
: (isEsportsHandbook ? douyuApi.listEsportsGoods() : douyuApi.listGoods()),
|
||||
douyuApi.listTasks(handbook),
|
||||
canConfig ? douyuApi.getConfig() : Promise.resolve(null),
|
||||
douyuApi.taskTypes(),
|
||||
douyuApi.getRechargeChannel(),
|
||||
]);
|
||||
if (accResult.status === 'fulfilled') {
|
||||
setAccounts(accResult.value.items);
|
||||
setAccountTotal(accResult.value.total);
|
||||
if (accountPage > 1 && accResult.value.items.length === 0) {
|
||||
setAccountPage(Math.max(1, Math.ceil(accResult.value.total / accountPageSize)));
|
||||
}
|
||||
}
|
||||
if (goodsResult.status === 'fulfilled') setGoods(goodsResult.value);
|
||||
if (taskResult.status === 'fulfilled') {
|
||||
setTasks(taskResult.value);
|
||||
// 首次加载只压制历史的复制/失败提示。二维码只会因当前页面创建的批次自动打开。
|
||||
if (!autoOpenQrReady.current) {
|
||||
for (const task of taskResult.value) {
|
||||
// 历史兑换成功任务不触发自动复制,避免页面加载时打扰
|
||||
if (exchangeTaskSucceeded(task)) autoCopiedExchangeTaskIds.current.add(task.id);
|
||||
// 历史确认绑定失败任务不重复弹提示(只提示一次)
|
||||
if (
|
||||
task.task_type === 'confirm_bind'
|
||||
&& ['failed', 'error'].includes(task.status)
|
||||
&& (task.message || '').includes('待绑定游戏账号')
|
||||
) {
|
||||
autoNotifiedConfirmFailedTaskIds.current.add(task.id);
|
||||
}
|
||||
}
|
||||
autoOpenQrReady.current = true;
|
||||
autoOpenEsportsBindReady.current = true;
|
||||
}
|
||||
}
|
||||
if (cfgResult.status === 'fulfilled' && cfgResult.value) setConfig(cfgResult.value);
|
||||
if (typeResult.status === 'fulfilled') setTaskTypes(typeResult.value);
|
||||
if (rechargeChannelResult.status === 'fulfilled') {
|
||||
setGoldRechargeChannel(rechargeChannelResult.value.gold_recharge_channel);
|
||||
const result = workbenchIds.length > 0
|
||||
? await douyuApi.listAccountsPaged({
|
||||
ids: workbenchIds.join(','),
|
||||
search: accountSearch.trim() || undefined,
|
||||
page: accountPage,
|
||||
page_size: accountPageSize,
|
||||
})
|
||||
: { items: [] as DouyuTaskAccountItem[], total: 0, page: accountPage, page_size: accountPageSize };
|
||||
setAccounts(result.items);
|
||||
setAccountTotal(result.total);
|
||||
if (accountPage > 1 && result.items.length === 0) {
|
||||
setAccountPage(Math.max(1, Math.ceil(result.total / accountPageSize)));
|
||||
}
|
||||
} catch (error) {
|
||||
message.error(`加载工作台账号失败: ${getErrorMessage(error)}`);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [accountPage, accountPageSize, accountSearch, canConfig, handbook, isEsportsHandbook, isPeaceHandbook, workbenchIds]);
|
||||
}, [accountPage, accountPageSize, accountSearch, workbenchIds, workbenchReady]);
|
||||
|
||||
const loadStaticData = useCallback(async () => {
|
||||
const [goodsResult, cfgResult, typeResult, rechargeChannelResult] = await Promise.allSettled([
|
||||
isPeaceHandbook
|
||||
? douyuApi.listPeaceGoods()
|
||||
: (isEsportsHandbook ? douyuApi.listEsportsGoods() : douyuApi.listGoods()),
|
||||
canConfig ? douyuApi.getConfig() : Promise.resolve(null),
|
||||
douyuApi.taskTypes(),
|
||||
douyuApi.getRechargeChannel(),
|
||||
]);
|
||||
if (goodsResult.status === 'fulfilled') setGoods(goodsResult.value);
|
||||
if (cfgResult.status === 'fulfilled' && cfgResult.value) setConfig(cfgResult.value);
|
||||
if (typeResult.status === 'fulfilled') setTaskTypes(typeResult.value);
|
||||
if (rechargeChannelResult.status === 'fulfilled') {
|
||||
setGoldRechargeChannel(rechargeChannelResult.value.gold_recharge_channel);
|
||||
}
|
||||
}, [canConfig, isEsportsHandbook, isPeaceHandbook]);
|
||||
|
||||
const setPaySessionsSafely = useCallback(
|
||||
(updater: (current: DouyuPaySession[]) => DouyuPaySession[]) => {
|
||||
@@ -581,11 +568,26 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
}, [loadPaySessionTasks, paySessionsStorageKey]);
|
||||
|
||||
const loadTasks = useCallback(async () => {
|
||||
if (tasksLoadingRef.current) return;
|
||||
if (!workbenchReady || tasksLoadingRef.current) return;
|
||||
tasksLoadingRef.current = true;
|
||||
try {
|
||||
const data = await douyuApi.listTasks(handbook);
|
||||
const data = await douyuApi.listTasks(handbook, undefined, true, 300);
|
||||
setTasks(data);
|
||||
// 首次快照只建立历史基线;只有本页创建的批次允许触发自动二维码/复制/弹窗。
|
||||
if (!autoOpenQrReady.current) {
|
||||
for (const task of data) {
|
||||
if (exchangeTaskSucceeded(task)) autoCopiedExchangeTaskIds.current.add(task.id);
|
||||
if (
|
||||
task.task_type === 'confirm_bind'
|
||||
&& ['failed', 'error'].includes(task.status)
|
||||
&& (task.message || '').includes('待绑定游戏账号')
|
||||
) {
|
||||
autoNotifiedConfirmFailedTaskIds.current.add(task.id);
|
||||
}
|
||||
}
|
||||
autoOpenQrReady.current = true;
|
||||
autoOpenEsportsBindReady.current = true;
|
||||
}
|
||||
if (!data.some((task) => ['pending', 'running', 'planned'].includes(task.status))) {
|
||||
// 批次已结束时清理 busy 标记,避免 onResult 丢失导致永久锁住
|
||||
setRunningBatchIds(new Set());
|
||||
@@ -598,9 +600,13 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
} finally {
|
||||
tasksLoadingRef.current = false;
|
||||
}
|
||||
}, [handbook, loadPaySessionTasks]);
|
||||
}, [handbook, loadPaySessionTasks, workbenchReady]);
|
||||
|
||||
useEffect(() => { loadData(); }, [loadData]);
|
||||
useEffect(() => { void loadAccountsPage(); }, [loadAccountsPage]);
|
||||
useEffect(() => { void loadStaticData(); }, [loadStaticData]);
|
||||
useEffect(() => {
|
||||
if (workbenchReady) void loadTasks();
|
||||
}, [loadTasks, workbenchIds, workbenchReady]);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
@@ -658,10 +664,6 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
localStorage.setItem(DOUYU_GIFT_COUNT_STORAGE_KEY, String(giftCount));
|
||||
}, [giftCount]);
|
||||
|
||||
const hasActiveTasks = useMemo(
|
||||
() => visibleTasks.some((task) => ['pending', 'running', 'planned'].includes(task.status)),
|
||||
[visibleTasks],
|
||||
);
|
||||
const hasActivePayTasks = useMemo(
|
||||
() => paySessions.some((session) => (
|
||||
(paySessionTasks[session.batchId] || []).some(
|
||||
@@ -699,18 +701,30 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
|
||||
const selectedHasRunning = selectedIds.some((id) => runningAccountIds.has(id));
|
||||
|
||||
// 任务状态由 WS(level=task) 即时推送;轮询仅作兜底(多标签页/断线重连),
|
||||
// 有活跃任务或 WS 连接时 5 秒一次,空闲 30 秒轻量刷新
|
||||
// WS 是主更新通道;仅本页活跃批次在断线时高频兜底,空闲页面低频做一致性校验。
|
||||
useEffect(() => {
|
||||
const intervalMs = (
|
||||
hasActiveTasks
|
||||
|| hasActivePayTasks
|
||||
|| logs.connected
|
||||
|| runningBatchIds.size > 0
|
||||
) ? 5000 : 30000;
|
||||
const timer = setInterval(loadTasks, intervalMs);
|
||||
return () => clearInterval(timer);
|
||||
}, [hasActivePayTasks, hasActiveTasks, loadTasks, logs.connected, runningBatchIds]);
|
||||
if (!workbenchReady) return undefined;
|
||||
const active = activeBatchIds.size > 0 || hasActivePayTasks || runningBatchIds.size > 0;
|
||||
const intervalMs = active ? (logs.connected ? 30000 : 5000) : 90000;
|
||||
const refreshVisibleTasks = () => {
|
||||
if (document.visibilityState === 'visible') void loadTasks();
|
||||
};
|
||||
const timer = window.setInterval(refreshVisibleTasks, intervalMs);
|
||||
document.addEventListener('visibilitychange', refreshVisibleTasks);
|
||||
window.addEventListener('focus', refreshVisibleTasks);
|
||||
return () => {
|
||||
window.clearInterval(timer);
|
||||
document.removeEventListener('visibilitychange', refreshVisibleTasks);
|
||||
window.removeEventListener('focus', refreshVisibleTasks);
|
||||
};
|
||||
}, [
|
||||
activeBatchIds,
|
||||
hasActivePayTasks,
|
||||
loadTasks,
|
||||
logs.connected,
|
||||
runningBatchIds,
|
||||
workbenchReady,
|
||||
]);
|
||||
|
||||
const openQrTask = useCallback(async (task: DouyuTaskItem, activate = true) => {
|
||||
let resolved = task;
|
||||
@@ -890,7 +904,11 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
useEffect(() => {
|
||||
if (!autoOpenQrReady.current) return;
|
||||
const pending = visibleTasks
|
||||
.filter((task) => exchangeTaskSucceeded(task) && !autoCopiedExchangeTaskIds.current.has(task.id))
|
||||
.filter((task) => (
|
||||
isLocallyStartedTask(task)
|
||||
&& exchangeTaskSucceeded(task)
|
||||
&& !autoCopiedExchangeTaskIds.current.has(task.id)
|
||||
))
|
||||
.sort((a, b) => a.id - b.id);
|
||||
if (pending.length === 0) return;
|
||||
for (const task of pending) autoCopiedExchangeTaskIds.current.add(task.id);
|
||||
@@ -908,14 +926,15 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
console.warn('复制兑换图片失败', error);
|
||||
},
|
||||
);
|
||||
}, [visibleTasks]);
|
||||
}, [isLocallyStartedTask, visibleTasks]);
|
||||
|
||||
// 确认绑定失败自动弹提示(页面中央弹窗,自动消失,一次性去重)
|
||||
useEffect(() => {
|
||||
if (!autoOpenQrReady.current) return;
|
||||
const failedTasks = visibleTasks
|
||||
.filter((task) => (
|
||||
task.task_type === 'confirm_bind'
|
||||
isLocallyStartedTask(task)
|
||||
&& task.task_type === 'confirm_bind'
|
||||
&& ['failed', 'error'].includes(task.status)
|
||||
&& (task.message || '').includes('待绑定游戏账号')
|
||||
&& !autoNotifiedConfirmFailedTaskIds.current.has(task.id)
|
||||
@@ -933,7 +952,7 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
setActiveQrTaskId((cur) => (cur != null && !next.includes(cur) ? (next[next.length - 1] ?? null) : cur));
|
||||
return next;
|
||||
});
|
||||
}, [visibleTasks]);
|
||||
}, [isLocallyStartedTask, visibleTasks]);
|
||||
|
||||
// 弹窗倒计时自动关闭
|
||||
useEffect(() => {
|
||||
@@ -1073,7 +1092,8 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
next.delete(result.batch_id);
|
||||
return next;
|
||||
});
|
||||
void loadData();
|
||||
void loadAccountsPage();
|
||||
void loadTasks();
|
||||
},
|
||||
onTask: (rawTask) => {
|
||||
const task = rawTask as unknown as DouyuTaskItem;
|
||||
@@ -1108,7 +1128,7 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
if (prev.some((t) => t.id === task.id)) {
|
||||
return prev.map((t) => (t.id === task.id ? task : t));
|
||||
}
|
||||
return [task, ...prev].slice(0, 100);
|
||||
return [task, ...prev].slice(0, 300);
|
||||
});
|
||||
},
|
||||
});
|
||||
@@ -1424,7 +1444,10 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
const skipped = result.skipped ? `,${result.skipped} 个因缺少登录凭据跳过` : '';
|
||||
message.success(`已开始重登 ${result.count} 个失效账号${skipped}`);
|
||||
setSelectedIds([]);
|
||||
window.setTimeout(() => void loadData(), 2000);
|
||||
window.setTimeout(() => {
|
||||
void loadAccountsPage();
|
||||
void loadTasks();
|
||||
}, 2000);
|
||||
} catch (error) {
|
||||
message.error(getErrorMessage(error));
|
||||
}
|
||||
@@ -1840,9 +1863,15 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
title: '换绑时间', dataIndex: 'change_role_wait_time', width: 150,
|
||||
render: (_, record) => {
|
||||
const fromTask = latestChangeWaitByAccount.get(record.id);
|
||||
const canChangeTime = fromTask?.canChangeTime ?? (
|
||||
isEsportsHandbook ? record.esports_can_change_time : null
|
||||
);
|
||||
const scopeState = isEsportsHandbook
|
||||
? record.esports_bind_status
|
||||
: record.role_bind_status;
|
||||
const noCooldown = scopeState === 'unbound' || scopeState === 'pending';
|
||||
const canChangeTime = noCooldown
|
||||
? null
|
||||
: (fromTask?.canChangeTime ?? (
|
||||
isEsportsHandbook ? record.esports_can_change_time : null
|
||||
));
|
||||
const esportsStatus = isEsportsHandbook ? esportsRebindStatus(canChangeTime) : null;
|
||||
if (esportsStatus) {
|
||||
return esportsStatus.available
|
||||
@@ -1854,10 +1883,10 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
</Space>
|
||||
);
|
||||
}
|
||||
const wait = fromTask?.wait ?? (
|
||||
const wait = noCooldown ? 0 : (fromTask?.wait ?? (
|
||||
isEsportsHandbook ? record.esports_change_role_wait_time : record.change_role_wait_time
|
||||
);
|
||||
const text = fromTask?.text || formatWaitSeconds(wait);
|
||||
));
|
||||
const text = noCooldown ? '0' : (fromTask?.text || formatWaitSeconds(wait));
|
||||
if (wait == null && !text) {
|
||||
return <Text type="secondary">未查</Text>;
|
||||
}
|
||||
@@ -1877,14 +1906,24 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
return <Text type="secondary">{text || '-'}</Text>;
|
||||
},
|
||||
sorter: (a, b) => {
|
||||
const aState = isEsportsHandbook ? a.esports_bind_status : a.role_bind_status;
|
||||
const bState = isEsportsHandbook ? b.esports_bind_status : b.role_bind_status;
|
||||
const aNoCooldown = aState === 'unbound' || aState === 'pending';
|
||||
const bNoCooldown = bState === 'unbound' || bState === 'pending';
|
||||
const aw = isEsportsHandbook
|
||||
? latestChangeWaitByAccount.get(a.id)?.canChangeTime ?? a.esports_can_change_time ?? -1
|
||||
: latestChangeWaitByAccount.get(a.id)?.wait
|
||||
?? (isEsportsHandbook ? a.esports_change_role_wait_time : a.change_role_wait_time) ?? -1;
|
||||
? (aNoCooldown ? 0 : latestChangeWaitByAccount.get(a.id)?.canChangeTime ?? a.esports_can_change_time ?? -1)
|
||||
: (aNoCooldown ? 0 : (
|
||||
latestChangeWaitByAccount.get(a.id)?.wait
|
||||
?? (isEsportsHandbook ? a.esports_change_role_wait_time : a.change_role_wait_time)
|
||||
?? -1
|
||||
));
|
||||
const bw = isEsportsHandbook
|
||||
? latestChangeWaitByAccount.get(b.id)?.canChangeTime ?? b.esports_can_change_time ?? -1
|
||||
: latestChangeWaitByAccount.get(b.id)?.wait
|
||||
?? (isEsportsHandbook ? b.esports_change_role_wait_time : b.change_role_wait_time) ?? -1;
|
||||
? (bNoCooldown ? 0 : latestChangeWaitByAccount.get(b.id)?.canChangeTime ?? b.esports_can_change_time ?? -1)
|
||||
: (bNoCooldown ? 0 : (
|
||||
latestChangeWaitByAccount.get(b.id)?.wait
|
||||
?? (isEsportsHandbook ? b.esports_change_role_wait_time : b.change_role_wait_time)
|
||||
?? -1
|
||||
));
|
||||
return aw - bw;
|
||||
},
|
||||
},
|
||||
@@ -2791,7 +2830,17 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
onClick={() => setLayoutMode((m) => (m === 'split' ? 'stack' : 'split'))}
|
||||
/>
|
||||
</Tooltip>
|
||||
<Button icon={<ReloadOutlined />} onClick={loadData} loading={loading}>刷新</Button>
|
||||
<Button
|
||||
icon={<ReloadOutlined />}
|
||||
onClick={() => {
|
||||
void loadAccountsPage();
|
||||
void loadStaticData();
|
||||
void loadTasks();
|
||||
}}
|
||||
loading={loading}
|
||||
>
|
||||
刷新
|
||||
</Button>
|
||||
{canConfig && <Button icon={<SettingOutlined />} onClick={() => setConfigOpen(true)}>配置</Button>}
|
||||
{(activeBatchIds.size > 0 || runningBatchIds.size > 0) && (
|
||||
<Popconfirm
|
||||
|
||||
Reference in New Issue
Block a user