feat(douyu): 增加供给目标管理界面
This commit is contained in:
@@ -5,6 +5,9 @@ import type {
|
||||
DouyuExchangeResultImage,
|
||||
DouyuGoodsItem,
|
||||
DouyuRechargeChannel,
|
||||
DouyuSupplyTarget,
|
||||
DouyuSupplyTargetUpdate,
|
||||
DouyuSupplyTargetUpsert,
|
||||
DouyuTaskAccountItem,
|
||||
DouyuTaskBatchRequest,
|
||||
DouyuTaskBatchResult,
|
||||
@@ -19,6 +22,12 @@ export const douyuApi = {
|
||||
taskTypes: () => api.get<Record<string, string>, Record<string, string>>('/douyu/task-types'),
|
||||
getAccountPlatformStatus: () =>
|
||||
api.get<DouyuAccountPlatformStatus, DouyuAccountPlatformStatus>('/douyu/account-platform-status'),
|
||||
listSupplyTargets: () =>
|
||||
api.get<DouyuSupplyTarget[], DouyuSupplyTarget[]>('/douyu/supply-targets'),
|
||||
createSupplyTarget: (data: DouyuSupplyTargetUpsert) =>
|
||||
api.post<DouyuSupplyTarget, DouyuSupplyTarget>('/douyu/supply-targets', data),
|
||||
updateSupplyTarget: (targetId: number, data: DouyuSupplyTargetUpdate) =>
|
||||
api.put<DouyuSupplyTarget, DouyuSupplyTarget>(`/douyu/supply-targets/${targetId}`, data),
|
||||
listAccounts: (params?: { search?: string; tag?: string; ids?: string }) =>
|
||||
api.get<DouyuTaskAccountItem[], DouyuTaskAccountItem[]>('/douyu/accounts', { params }),
|
||||
listAccountsPaged: (params: PageParams & { ids?: string; tag?: string }) =>
|
||||
|
||||
@@ -745,6 +745,33 @@ export interface DouyuSupplyTargetStatus {
|
||||
needs_replenishment: boolean;
|
||||
}
|
||||
|
||||
export interface DouyuSupplyTarget extends DouyuSupplyTargetStatus {
|
||||
platform: string;
|
||||
enabled: boolean;
|
||||
updated_by: number | null;
|
||||
updated_at: string | null;
|
||||
}
|
||||
|
||||
export interface DouyuSupplyTargetUpsert {
|
||||
platform?: 'douyu';
|
||||
scope: string;
|
||||
commodity_id: string;
|
||||
value_class: 'high_value' | 'ordinary';
|
||||
target_covered: number;
|
||||
target_available: number;
|
||||
target_allocatable: number;
|
||||
safety_buffer: number;
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
export interface DouyuSupplyTargetUpdate {
|
||||
target_covered: number;
|
||||
target_available: number;
|
||||
target_allocatable: number;
|
||||
safety_buffer: number;
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
export interface DouyuPlatformAlert {
|
||||
code: string;
|
||||
severity: 'critical' | 'warning' | 'info';
|
||||
|
||||
@@ -5,20 +5,28 @@ import {
|
||||
Card,
|
||||
Col,
|
||||
Empty,
|
||||
Form,
|
||||
Input,
|
||||
InputNumber,
|
||||
Modal,
|
||||
Row,
|
||||
Select,
|
||||
Space,
|
||||
Statistic,
|
||||
Switch,
|
||||
Table,
|
||||
Tag,
|
||||
Typography,
|
||||
} from 'antd';
|
||||
import type { TableProps } from 'antd';
|
||||
import { ReloadOutlined } from '@ant-design/icons';
|
||||
import { EditOutlined, PlusOutlined, ReloadOutlined } from '@ant-design/icons';
|
||||
import {
|
||||
douyuApi,
|
||||
type DouyuAccountPlatformStatus,
|
||||
type DouyuSupplyTargetStatus,
|
||||
type DouyuSupplyTarget,
|
||||
} from '../../api/modules';
|
||||
import { usePermissions } from '../../hooks/usePermissions';
|
||||
import { message } from '../../utils/antdMessage';
|
||||
import { getErrorMessage } from '../../utils/error';
|
||||
import { formatTime } from '../../utils/time';
|
||||
|
||||
@@ -34,22 +42,52 @@ function targetTotal(actual: number, target: number, buffer: number) {
|
||||
return `${actual}/${effectiveTarget || '-'}`;
|
||||
}
|
||||
|
||||
interface TargetFormValues {
|
||||
scope: string;
|
||||
commodity_id?: string;
|
||||
value_class: 'high_value' | 'ordinary';
|
||||
target_covered: number;
|
||||
target_available: number;
|
||||
target_allocatable: number;
|
||||
safety_buffer: number;
|
||||
enabled: boolean;
|
||||
}
|
||||
|
||||
export default function AccountPlatformStatusPanel() {
|
||||
const { can } = usePermissions();
|
||||
const canManage = can('douyu:config');
|
||||
const [form] = Form.useForm<TargetFormValues>();
|
||||
const [status, setStatus] = useState<DouyuAccountPlatformStatus | null>(null);
|
||||
const [targets, setTargets] = useState<DouyuSupplyTarget[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [modalOpen, setModalOpen] = useState(false);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [editing, setEditing] = useState<DouyuSupplyTarget | null>(null);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setLoading(true);
|
||||
setError('');
|
||||
try {
|
||||
setStatus(await douyuApi.getAccountPlatformStatus());
|
||||
const platformStatus = await douyuApi.getAccountPlatformStatus();
|
||||
if (canManage) {
|
||||
setTargets(await douyuApi.listSupplyTargets());
|
||||
} else {
|
||||
setTargets(platformStatus.supply_targets.map((row) => ({
|
||||
...row,
|
||||
platform: 'douyu',
|
||||
enabled: true,
|
||||
updated_by: null,
|
||||
updated_at: null,
|
||||
})));
|
||||
}
|
||||
setStatus(platformStatus);
|
||||
} catch (loadError: unknown) {
|
||||
setError(getErrorMessage(loadError));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
}, [canManage]);
|
||||
|
||||
useEffect(() => {
|
||||
void load();
|
||||
@@ -63,7 +101,69 @@ export default function AccountPlatformStatusPanel() {
|
||||
[status?.work_queue],
|
||||
);
|
||||
|
||||
const columns: TableProps<DouyuSupplyTargetStatus>['columns'] = [
|
||||
const openCreate = () => {
|
||||
setEditing(null);
|
||||
form.setFieldsValue({
|
||||
scope: 'elite',
|
||||
commodity_id: '',
|
||||
value_class: 'high_value',
|
||||
target_covered: 30,
|
||||
target_available: 20,
|
||||
target_allocatable: 10,
|
||||
safety_buffer: 3,
|
||||
enabled: true,
|
||||
});
|
||||
setModalOpen(true);
|
||||
};
|
||||
|
||||
const openEdit = (target: DouyuSupplyTarget) => {
|
||||
setEditing(target);
|
||||
form.setFieldsValue({
|
||||
scope: target.scope,
|
||||
commodity_id: target.commodity_id,
|
||||
value_class: target.value_class,
|
||||
target_covered: target.target_covered,
|
||||
target_available: target.target_available,
|
||||
target_allocatable: target.target_allocatable,
|
||||
safety_buffer: target.safety_buffer,
|
||||
enabled: target.enabled,
|
||||
});
|
||||
setModalOpen(true);
|
||||
};
|
||||
|
||||
const saveTarget = async () => {
|
||||
const values = await form.validateFields();
|
||||
setSaving(true);
|
||||
try {
|
||||
const payload = {
|
||||
target_covered: Number(values.target_covered || 0),
|
||||
target_available: Number(values.target_available || 0),
|
||||
target_allocatable: Number(values.target_allocatable || 0),
|
||||
safety_buffer: Number(values.safety_buffer || 0),
|
||||
enabled: Boolean(values.enabled),
|
||||
};
|
||||
if (editing) {
|
||||
await douyuApi.updateSupplyTarget(editing.target_id, payload);
|
||||
} else {
|
||||
await douyuApi.createSupplyTarget({
|
||||
platform: 'douyu',
|
||||
scope: values.scope,
|
||||
commodity_id: (values.commodity_id || '').trim(),
|
||||
value_class: values.value_class,
|
||||
...payload,
|
||||
});
|
||||
}
|
||||
message.success(editing ? '供给目标已更新' : '供给目标已创建');
|
||||
setModalOpen(false);
|
||||
await load();
|
||||
} catch (saveError: unknown) {
|
||||
message.error(getErrorMessage(saveError));
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
const columns: TableProps<DouyuSupplyTarget>['columns'] = [
|
||||
{
|
||||
title: '分类',
|
||||
width: 130,
|
||||
@@ -100,69 +200,163 @@ export default function AccountPlatformStatusPanel() {
|
||||
row.available_shortage,
|
||||
row.allocatable_shortage,
|
||||
);
|
||||
return shortage > 0 ? <Text type="danger">{shortage}</Text> : <Text type="success">0</Text>;
|
||||
return shortage > 0
|
||||
? <Text type="danger">{shortage}</Text>
|
||||
: <Text type="success">0</Text>;
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '状态',
|
||||
width: 100,
|
||||
width: 110,
|
||||
render: (_, row) => (
|
||||
row.needs_replenishment
|
||||
? <Tag color="warning">需补货</Tag>
|
||||
: <Tag color="success">达标</Tag>
|
||||
!row.enabled
|
||||
? <Tag>已停用</Tag>
|
||||
: row.needs_replenishment
|
||||
? <Tag color="warning">需补货</Tag>
|
||||
: <Tag color="success">达标</Tag>
|
||||
),
|
||||
},
|
||||
...(canManage ? [{
|
||||
title: '操作',
|
||||
width: 80,
|
||||
render: (_: unknown, row: DouyuSupplyTarget) => (
|
||||
<Button
|
||||
type="link"
|
||||
size="small"
|
||||
icon={<EditOutlined />}
|
||||
onClick={() => openEdit(row)}
|
||||
>
|
||||
编辑
|
||||
</Button>
|
||||
),
|
||||
}] : []),
|
||||
];
|
||||
|
||||
return (
|
||||
<Card
|
||||
title="账号平台状态"
|
||||
extra={(
|
||||
<Space size={8}>
|
||||
<Text type="secondary">
|
||||
{status?.checked_at ? formatTime(status.checked_at) : '-'}
|
||||
</Text>
|
||||
<Button icon={<ReloadOutlined />} loading={loading} onClick={() => void load()}>
|
||||
刷新
|
||||
</Button>
|
||||
</Space>
|
||||
)}
|
||||
>
|
||||
{error ? <Alert type="error" showIcon message={error} style={{ marginBottom: 16 }} /> : null}
|
||||
<Row gutter={[16, 16]}>
|
||||
<Col xs={12} md={6}><Statistic title="CK 有效" value={status?.accounts.ck_valid || 0} /></Col>
|
||||
<Col xs={12} md={6}><Statistic title="24h 预计到期" value={status?.accounts.ck_expiring_24h || 0} /></Col>
|
||||
<Col xs={12} md={6}><Statistic title="新鲜可兑换商品" value={status?.commodities.fresh_available || 0} /></Col>
|
||||
<Col xs={12} md={6}><Statistic title="待处理账号工作" value={pendingWork} /></Col>
|
||||
</Row>
|
||||
<>
|
||||
<Card
|
||||
title="账号平台状态"
|
||||
extra={(
|
||||
<Space size={8}>
|
||||
<Text type="secondary">
|
||||
{status?.checked_at ? formatTime(status.checked_at) : '-'}
|
||||
</Text>
|
||||
{canManage ? (
|
||||
<Button icon={<PlusOutlined />} onClick={openCreate}>
|
||||
新增目标
|
||||
</Button>
|
||||
) : null}
|
||||
<Button icon={<ReloadOutlined />} loading={loading} onClick={() => void load()}>
|
||||
刷新
|
||||
</Button>
|
||||
</Space>
|
||||
)}
|
||||
>
|
||||
{error ? <Alert type="error" showIcon message={error} style={{ marginBottom: 16 }} /> : null}
|
||||
<Row gutter={[16, 16]}>
|
||||
<Col xs={12} md={6}><Statistic title="CK 有效" value={status?.accounts.ck_valid || 0} /></Col>
|
||||
<Col xs={12} md={6}><Statistic title="24h 预计到期" value={status?.accounts.ck_expiring_24h || 0} /></Col>
|
||||
<Col xs={12} md={6}><Statistic title="新鲜可兑换商品" value={status?.commodities.fresh_available || 0} /></Col>
|
||||
<Col xs={12} md={6}><Statistic title="待处理账号工作" value={pendingWork} /></Col>
|
||||
</Row>
|
||||
|
||||
{status?.alerts.length ? (
|
||||
<Space orientation="vertical" size={8} style={{ width: '100%', marginTop: 16 }}>
|
||||
{status.alerts.map((alert) => (
|
||||
<Alert
|
||||
key={`${alert.code}-${alert.scope || ''}-${alert.value_class || ''}-${alert.message}`}
|
||||
type={alert.severity === 'critical' ? 'error' : alert.severity === 'warning' ? 'warning' : 'info'}
|
||||
showIcon
|
||||
message={alert.message}
|
||||
/>
|
||||
))}
|
||||
</Space>
|
||||
) : null}
|
||||
{status?.alerts.length ? (
|
||||
<Space orientation="vertical" size={8} style={{ width: '100%', marginTop: 16 }}>
|
||||
{status.alerts.map((alert) => (
|
||||
<Alert
|
||||
key={`${alert.code}-${alert.scope || ''}-${alert.value_class || ''}-${alert.message}`}
|
||||
type={alert.severity === 'critical' ? 'error' : alert.severity === 'warning' ? 'warning' : 'info'}
|
||||
showIcon
|
||||
message={alert.message}
|
||||
/>
|
||||
))}
|
||||
</Space>
|
||||
) : null}
|
||||
|
||||
<Table
|
||||
rowKey="target_id"
|
||||
size="small"
|
||||
style={{ marginTop: 16 }}
|
||||
columns={columns}
|
||||
dataSource={status?.supply_targets || []}
|
||||
pagination={false}
|
||||
scroll={{ x: 650 }}
|
||||
locale={{
|
||||
emptyText: (
|
||||
<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="暂无供给目标" />
|
||||
),
|
||||
}}
|
||||
/>
|
||||
</Card>
|
||||
<Table
|
||||
rowKey="target_id"
|
||||
size="small"
|
||||
style={{ marginTop: 16 }}
|
||||
columns={columns}
|
||||
dataSource={targets}
|
||||
pagination={false}
|
||||
scroll={{ x: 750 }}
|
||||
locale={{
|
||||
emptyText: (
|
||||
<Empty image={Empty.PRESENTED_IMAGE_SIMPLE} description="暂无供给目标" />
|
||||
),
|
||||
}}
|
||||
/>
|
||||
</Card>
|
||||
|
||||
<Modal
|
||||
title={editing ? '编辑供给目标' : '新增供给目标'}
|
||||
open={modalOpen}
|
||||
confirmLoading={saving}
|
||||
onOk={() => void saveTarget()}
|
||||
onCancel={() => setModalOpen(false)}
|
||||
okText="保存"
|
||||
cancelText="取消"
|
||||
destroyOnHidden
|
||||
>
|
||||
<Form form={form} layout="vertical">
|
||||
<Row gutter={12}>
|
||||
<Col span={12}>
|
||||
<Form.Item name="scope" label="作用域" rules={[{ required: true }]}>
|
||||
<Select
|
||||
disabled={Boolean(editing)}
|
||||
options={[
|
||||
{ value: 'elite', label: '精英宝典' },
|
||||
{ value: 'esports', label: '电竞手册' },
|
||||
{ value: 'peace', label: '和平小店' },
|
||||
]}
|
||||
/>
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Form.Item name="value_class" label="账号价值" rules={[{ required: true }]}>
|
||||
<Select
|
||||
disabled={Boolean(editing)}
|
||||
options={[
|
||||
{ value: 'high_value', label: '高价值' },
|
||||
{ value: 'ordinary', label: '普通' },
|
||||
]}
|
||||
/>
|
||||
</Form.Item>
|
||||
</Col>
|
||||
</Row>
|
||||
<Form.Item name="commodity_id" label="商品 ID">
|
||||
<Input disabled={Boolean(editing)} placeholder="留空表示该作用域全部商品" />
|
||||
</Form.Item>
|
||||
<Row gutter={12}>
|
||||
<Col span={12}>
|
||||
<Form.Item name="target_covered" label="覆盖目标">
|
||||
<InputNumber min={0} precision={0} style={{ width: '100%' }} />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Form.Item name="target_available" label="可兑换目标">
|
||||
<InputNumber min={0} precision={0} style={{ width: '100%' }} />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
</Row>
|
||||
<Row gutter={12}>
|
||||
<Col span={12}>
|
||||
<Form.Item name="target_allocatable" label="可分配目标">
|
||||
<InputNumber min={0} precision={0} style={{ width: '100%' }} />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
<Col span={12}>
|
||||
<Form.Item name="safety_buffer" label="安全缓冲">
|
||||
<InputNumber min={0} precision={0} style={{ width: '100%' }} />
|
||||
</Form.Item>
|
||||
</Col>
|
||||
</Row>
|
||||
<Form.Item name="enabled" label="启用" valuePropName="checked">
|
||||
<Switch />
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user