refactor: 收口跨平台重复业务实现
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import {
|
||||
Button, Card, Col, DatePicker, Dropdown, Form, Input, InputNumber, Modal, QRCode, Radio, Row, Select, Space, Table, Tag, Tooltip, Typography, theme,
|
||||
Button, Card, DatePicker, Dropdown, Form, Input, InputNumber, Modal, QRCode, Radio, Select, Space, Table, Tag, Tooltip, Typography, theme,
|
||||
} from 'antd';
|
||||
import { message } from '../utils/antdMessage';
|
||||
import type { MenuProps, TableProps } from 'antd';
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
type HuyaRechargeGoodsItem,
|
||||
type HuyaTaskItem,
|
||||
} from '../api/modules';
|
||||
import HuyaConfigFields from '../components/HuyaConfigFields';
|
||||
import RealtimeLogPanel from '../components/RealtimeLogPanel';
|
||||
import { usePermissions } from '../hooks/usePermissions';
|
||||
import { useWebSocketLogs } from '../hooks/useWebSocketLogs';
|
||||
@@ -282,6 +283,7 @@ export default function HuyaTasksPage() {
|
||||
const [accounts, setAccounts] = useState<HuyaAccountItem[]>([]);
|
||||
const [tasks, setTasks] = useState<HuyaTaskItem[]>([]);
|
||||
const [goods, setGoods] = useState<HuyaGoodsItem[]>([]);
|
||||
const [goodsLoading, setGoodsLoading] = useState(false);
|
||||
const [rechargeGoods, setRechargeGoods] = useState<HuyaRechargeGoodsItem[]>([]);
|
||||
const [taskTypes, setTaskTypes] = useState<Record<string, string>>(FALLBACK_TASK_TYPES);
|
||||
const [tags, setTags] = useState<string[]>([]);
|
||||
@@ -322,6 +324,7 @@ export default function HuyaTasksPage() {
|
||||
const accountTableAreaRef = useRef<HTMLDivElement | null>(null);
|
||||
const [accountTableAreaHeight, setAccountTableAreaHeight] = useState(460);
|
||||
const tasksLoadingRef = useRef(false);
|
||||
const liveGoodsRequestId = useRef(0);
|
||||
const { logs, connected: wsConnected, connect: connectLogs } = useWebSocketLogs();
|
||||
const { can } = usePermissions();
|
||||
|
||||
@@ -403,10 +406,9 @@ export default function HuyaTasksPage() {
|
||||
const loadAll = useCallback(async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const [accountResult, taskResult, goodsResult, rechargeGoodsResult, configResult, taskTypeResult, tagResult] = await Promise.allSettled([
|
||||
const [accountResult, taskResult, rechargeGoodsResult, configResult, taskTypeResult, tagResult] = await Promise.allSettled([
|
||||
huyaApi.listAccounts({ include_cookie: false }),
|
||||
huyaApi.listTasks(),
|
||||
huyaApi.listGoods(),
|
||||
huyaApi.listRechargeGoods(),
|
||||
canConfig ? huyaApi.getConfig() : Promise.resolve(null),
|
||||
huyaApi.taskTypes(),
|
||||
@@ -437,7 +439,6 @@ export default function HuyaTasksPage() {
|
||||
setBatchId(null);
|
||||
}
|
||||
}
|
||||
if (goodsResult.status === 'fulfilled') setGoods(goodsResult.value);
|
||||
if (rechargeGoodsResult.status === 'fulfilled') setRechargeGoods(rechargeGoodsResult.value);
|
||||
if (configResult.status === 'fulfilled' && configResult.value) form.setFieldsValue(configResult.value);
|
||||
if (taskTypeResult.status === 'fulfilled') setTaskTypes({ ...FALLBACK_TASK_TYPES, ...taskTypeResult.value });
|
||||
@@ -446,7 +447,6 @@ export default function HuyaTasksPage() {
|
||||
const failedLabels = [
|
||||
accountResult.status === 'rejected' ? `CK 列表: ${getErrorMessage(accountResult.reason)}` : '',
|
||||
taskResult.status === 'rejected' ? `任务记录: ${getErrorMessage(taskResult.reason)}` : '',
|
||||
goodsResult.status === 'rejected' ? `兑换商品: ${getErrorMessage(goodsResult.reason)}` : '',
|
||||
rechargeGoodsResult.status === 'rejected' ? `充值商品: ${getErrorMessage(rechargeGoodsResult.reason)}` : '',
|
||||
configResult.status === 'rejected' ? `虎牙配置: ${getErrorMessage(configResult.reason)}` : '',
|
||||
taskTypeResult.status === 'rejected' ? `任务类型: ${getErrorMessage(taskTypeResult.reason)}` : '',
|
||||
@@ -460,6 +460,38 @@ export default function HuyaTasksPage() {
|
||||
}
|
||||
}, [canConfig, form, rememberExistingPaymentQrcodes, rememberExistingQrcodes]);
|
||||
|
||||
const loadLiveGoods = useCallback(async (accountId: number | null) => {
|
||||
const requestId = ++liveGoodsRequestId.current;
|
||||
if (!accountId) {
|
||||
setGoods([]);
|
||||
setSelectedExchangeGoodsId('');
|
||||
return;
|
||||
}
|
||||
setGoodsLoading(true);
|
||||
try {
|
||||
const result = await huyaApi.listLiveGoods(accountId);
|
||||
if (liveGoodsRequestId.current !== requestId) return;
|
||||
setGoods(result);
|
||||
setSelectedExchangeGoodsId((current) => (
|
||||
result.some((item) => item.product_id === current)
|
||||
? current
|
||||
: result[0]?.product_id || ''
|
||||
));
|
||||
} catch (error) {
|
||||
if (liveGoodsRequestId.current !== requestId) return;
|
||||
setGoods([]);
|
||||
setSelectedExchangeGoodsId('');
|
||||
message.error(`实时商品加载失败:${getErrorMessage(error)}`);
|
||||
} finally {
|
||||
if (liveGoodsRequestId.current === requestId) setGoodsLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
const goodsAccountId = selectedIds[0] ?? accounts[0]?.id ?? null;
|
||||
useEffect(() => {
|
||||
void loadLiveGoods(goodsAccountId);
|
||||
}, [goodsAccountId, loadLiveGoods]);
|
||||
|
||||
const loadTasks = useCallback(async () => {
|
||||
if (tasksLoadingRef.current) return;
|
||||
tasksLoadingRef.current = true;
|
||||
@@ -1716,8 +1748,9 @@ export default function HuyaTasksPage() {
|
||||
<Button
|
||||
size="small"
|
||||
icon={<ReloadOutlined />}
|
||||
onClick={() => startTask('refresh_goods')}
|
||||
disabled={!canTask || selectedIds.length === 0 || batchBusy || wsConnected}
|
||||
loading={goodsLoading}
|
||||
onClick={() => void loadLiveGoods(goodsAccountId)}
|
||||
disabled={!canTask || !goodsAccountId || goodsLoading}
|
||||
>
|
||||
刷新
|
||||
</Button>
|
||||
@@ -1738,7 +1771,9 @@ export default function HuyaTasksPage() {
|
||||
<Select
|
||||
showSearch
|
||||
allowClear
|
||||
placeholder="选择兑换商品"
|
||||
loading={goodsLoading}
|
||||
disabled={!goodsAccountId || goodsLoading}
|
||||
placeholder={goodsLoading ? '正在读取实时商品' : !goodsAccountId ? '请先选择账号' : '暂无实时商品'}
|
||||
value={selectedExchangeGoodsId || undefined}
|
||||
onChange={(value) => setSelectedExchangeGoodsId(value || '')}
|
||||
options={goodsOptions}
|
||||
@@ -1834,68 +1869,7 @@ export default function HuyaTasksPage() {
|
||||
)}
|
||||
>
|
||||
<Form form={form} layout="vertical" disabled={!canConfig}>
|
||||
<Row gutter={8}>
|
||||
<Col span={12}>
|
||||
<Form.Item label="直播间 ID" name="room_pid">
|
||||
<Input placeholder="1199650619883" />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Form.Item label="SID" name="sid">
|
||||
<Input placeholder="2203" />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Form.Item label="兑换活动 ID" name="outer_act_id">
|
||||
<Input placeholder="9504" />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Form.Item label="绑定 gid" name="bind_gid">
|
||||
<Input placeholder="3203" />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Form.Item label="绑定 bActId" name="bind_act_id">
|
||||
<Input placeholder="9271" />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Form.Item label="积分中心 pageCode" name="points_page_code">
|
||||
<Input placeholder="heping_baodian_credit_center_page" />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Form.Item label="积分中心 sourceId" name="points_source_id">
|
||||
<Input placeholder="yellowcarlist" />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Form.Item label="积分中心 scene" name="points_scene">
|
||||
<Input placeholder="7" />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Form.Item label="页签 pageId(可选)" name="points_page_id">
|
||||
<Input placeholder="留空自动发现,如 343" />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={24}>
|
||||
<Form.Item label="页签 IDs(可选,逗号分隔)" name="points_tab_ids">
|
||||
<Input placeholder="留空自动拉全部,如 431,433" />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={24}>
|
||||
<Form.Item label="支付渠道" name="pay_channel">
|
||||
<Select
|
||||
options={[
|
||||
{ value: 'Weixin', label: '微信' },
|
||||
{ value: 'Zfb', label: '支付宝' },
|
||||
]}
|
||||
/>
|
||||
</Form.Item>
|
||||
</Col>
|
||||
</Row>
|
||||
<HuyaConfigFields />
|
||||
</Form>
|
||||
</Card>
|
||||
</Space>
|
||||
|
||||
Reference in New Issue
Block a user