`remark_changed` 此前是「定义了事件类型、但压根没有改备注的接口」,于是永远不发。
按 S4 的原则(留着一个永远不发的类型比删掉更糟 —— 看代码的人会以为它有数据),
补接口而不是删类型。
## 接口
`PUT /api/accounts/{id}/remark`,权限点 `account:remark`(授予 super_admin /
operation / support,与 `account:tag` 一致)。
- 与改标签一样走 `record_account_action()`:**审计 + 账号时间线一次写完**,
审计带 `account_id`,事件 detail 带 `{from, to}`;
- **只做单个、不做批量**:备注是自由文本,给一堆账号写同一句备注没有实际意义
(标签是短枚举才有批量);
- 内容 trim 后截断到 256(与 `accounts.remark` 列宽一致),清空也记一条事件;
- 备注没变化时直接返回、**不写事件**:否则时间线会被「点开保存了一下但没改」刷满。
## 权限点新增前的安全检查
新权限点会被 `custom_permissions` 覆盖,所以先查了库:
`admin` / `kf1_test` 的 `custom_permissions` 是 **JSON null**(不是 SQL NULL),
Python 侧取出来是 `None` → `get_user_permissions` 走角色默认。
也就是说**没有任何用户在用自定义权限列表**,新增权限点不会把谁的能力锁掉。
(这一点很容易看错:`IS NULL` 判出来是「有自定义权限」,`JSON_LENGTH` 还是 1。)
## 前端
- 详情页「备注」一行改为可编辑(铅笔 → 输入框 → 保存/取消,回车即存);
- `EVENT_STYLE` 补 `remark_changed`,时间线里能看到备注的每次变更。
## 一个自己踩到又修掉的坑
第一版把两个 `useState` 写在了 `if (!accountId) return <Empty/>` **之后** ——
正是本次会话刚在 LoginTasksPage 修掉的那类 hooks 顺序违规(`react-hooks/rules-of-hooks`
直接报错)。已上移到早期 return 之前的 state 区块。lint 抓到,构建不报,属于
「能跑但迟早炸」的那类问题。
## 验证
- `TZ=Asia/Shanghai`:**426 passed / 6 skipped**(S4 后 420 → 新增 6 条)
- `TZ=UTC`(相关文件)77 passed;ruff check/format 全绿
- 前端 `tsc -b && vite build` 通过、`npm run lint` 0 errors
- 新增用例:写入并带 from/to 事件与审计关联、未变化不写事件、清空也记一条、
trim + 截断到 256、账号不存在 404、**新权限点确实在三个角色的默认权限里**
(防止「加了接口但没人能用」)
417 lines
15 KiB
TypeScript
417 lines
15 KiB
TypeScript
import { useCallback, useEffect, useState } from 'react';
|
||
import { useNavigate, useParams } from 'react-router-dom';
|
||
import {
|
||
Button, Card, Descriptions, Empty, Input, Popconfirm, Select, Space, Spin, Statistic, Table, Tag, Tooltip, Typography,
|
||
} from 'antd';
|
||
import type { TableProps } from 'antd';
|
||
import {
|
||
ArrowLeftOutlined, EditOutlined, ReloadOutlined,
|
||
} from '@ant-design/icons';
|
||
import { accountApi, type AccountEventItem, type AccountOverview } from '../api/modules';
|
||
import { getErrorMessage } from '../utils/error';
|
||
import { message } from '../utils/antdMessage';
|
||
import { formatTime } from '../utils/time';
|
||
|
||
const { Text, Title } = Typography;
|
||
|
||
/**
|
||
* 事件类型 → 展示样式。
|
||
* 颜色用来一眼区分「钱进来 / 分出去 / 状态变化」三类,而不是按类型各给一个颜色。
|
||
*/
|
||
const EVENT_STYLE: Record<string, { label: string; color: string }> = {
|
||
imported: { label: '导入', color: 'default' },
|
||
login_ok: { label: '登录', color: 'blue' },
|
||
ck_invalid: { label: 'CK 失效', color: 'red' },
|
||
ck_cleared: { label: 'CK 已清除', color: 'red' },
|
||
relogin_ok: { label: '重登成功', color: 'green' },
|
||
relogin_failed: { label: '重登转人工', color: 'orange' },
|
||
status_changed: { label: '状态变更', color: 'geekblue' },
|
||
bind_ok: { label: '绑定', color: 'cyan' },
|
||
recharge: { label: '充值', color: 'green' },
|
||
exchange: { label: '兑换', color: 'purple' },
|
||
points_credit: { label: '积分到账', color: 'gold' },
|
||
elite_opened: { label: '精英宝典', color: 'magenta' },
|
||
esports_opened: { label: '电竞手册', color: 'volcano' },
|
||
assigned: { label: '分配', color: 'default' },
|
||
tag_changed: { label: '标签', color: 'default' },
|
||
remark_changed: { label: '备注', color: 'default' },
|
||
};
|
||
|
||
/** 事件粗分类(与后端 `account_events_service.CATEGORY_*` 对应)。 */
|
||
const CATEGORY_OPTIONS = [
|
||
{ value: 'lifecycle', label: '状态变迁' },
|
||
{ value: 'auth', label: '登录与 CK' },
|
||
{ value: 'business', label: '业务流水' },
|
||
{ value: 'ops', label: '人工操作' },
|
||
];
|
||
|
||
/** 生命周期状态 → 展示色(与账号列表页保持一致,取值域见后端 account_lifecycle)。 */
|
||
const STATUS_TONES: Record<string, string> = {
|
||
imported: 'default',
|
||
active: 'green',
|
||
invalid: 'red',
|
||
manual: 'orange',
|
||
disabled: 'default',
|
||
archived: 'default',
|
||
};
|
||
|
||
function eventTag(type: string) {
|
||
const style = EVENT_STYLE[type] ?? { label: type, color: 'default' };
|
||
return <Tag color={style.color}>{style.label}</Tag>;
|
||
}
|
||
|
||
/** 金额展示:充值为正(绿色 ¥),兑换消耗为负(红色积分)。 */
|
||
function amountText(item: AccountEventItem) {
|
||
if (item.amount === null || item.amount === undefined) return null;
|
||
if (item.event_type === 'recharge') {
|
||
return <Text type="success">+¥{item.amount}</Text>;
|
||
}
|
||
if (item.event_type === 'exchange') {
|
||
return <Text type="danger">{item.amount} 分</Text>;
|
||
}
|
||
return (
|
||
<Text type={item.amount >= 0 ? 'success' : 'danger'}>
|
||
{item.amount > 0 ? '+' : ''}
|
||
{item.amount}
|
||
</Text>
|
||
);
|
||
}
|
||
|
||
export default function AccountDetailPage() {
|
||
const { id } = useParams<{ id: string }>();
|
||
const accountId = Number(id);
|
||
const navigate = useNavigate();
|
||
|
||
const [overview, setOverview] = useState<AccountOverview | null>(null);
|
||
const [loading, setLoading] = useState(true);
|
||
const [events, setEvents] = useState<AccountEventItem[]>([]);
|
||
const [eventTotal, setEventTotal] = useState(0);
|
||
const [page, setPage] = useState(1);
|
||
const [pageSize, setPageSize] = useState(20);
|
||
const [eventType, setEventType] = useState('');
|
||
const [category, setCategory] = useState('');
|
||
const [eventsLoading, setEventsLoading] = useState(false);
|
||
const [remarkEditing, setRemarkEditing] = useState(false);
|
||
const [remarkDraft, setRemarkDraft] = useState('');
|
||
|
||
const loadOverview = useCallback(async () => {
|
||
if (!accountId) return;
|
||
setLoading(true);
|
||
try {
|
||
setOverview(await accountApi.overview(accountId));
|
||
} catch (e: unknown) {
|
||
message.error(getErrorMessage(e));
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
}, [accountId]);
|
||
|
||
const loadEvents = useCallback(async () => {
|
||
if (!accountId) return;
|
||
setEventsLoading(true);
|
||
try {
|
||
const data = await accountApi.events(accountId, {
|
||
page,
|
||
page_size: pageSize,
|
||
event_type: eventType,
|
||
category,
|
||
});
|
||
setEvents(data.items);
|
||
setEventTotal(data.total);
|
||
} catch (e: unknown) {
|
||
message.error(getErrorMessage(e));
|
||
} finally {
|
||
setEventsLoading(false);
|
||
}
|
||
}, [accountId, page, pageSize, eventType, category]);
|
||
|
||
useEffect(() => {
|
||
void loadOverview();
|
||
}, [loadOverview]);
|
||
|
||
useEffect(() => {
|
||
void loadEvents();
|
||
}, [loadEvents]);
|
||
|
||
const columns: TableProps<AccountEventItem>['columns'] = [
|
||
{
|
||
title: '时间',
|
||
dataIndex: 'occurred_at',
|
||
width: 165,
|
||
render: (val: string) => formatTime(val),
|
||
},
|
||
{
|
||
title: '类型',
|
||
dataIndex: 'event_type',
|
||
width: 100,
|
||
render: (val: string) => eventTag(val),
|
||
},
|
||
{ title: '说明', dataIndex: 'title', ellipsis: true },
|
||
{
|
||
title: '金额 / 变动',
|
||
dataIndex: 'amount',
|
||
width: 110,
|
||
align: 'right',
|
||
render: (_val, record) => amountText(record) ?? <Text type="secondary">-</Text>,
|
||
},
|
||
{
|
||
title: '变动后余额',
|
||
dataIndex: 'balance_after',
|
||
width: 110,
|
||
align: 'right',
|
||
render: (val: number | null) =>
|
||
val === null || val === undefined ? <Text type="secondary">-</Text> : val,
|
||
},
|
||
];
|
||
|
||
if (!accountId) {
|
||
return <Empty description="账号 ID 无效" />;
|
||
}
|
||
|
||
const stats = overview?.stats;
|
||
const status = overview?.status || '';
|
||
|
||
/** 人工切换生命周期状态:disabled / archived / auto(恢复自动推导)。 */
|
||
const handleLifecycle = async (next: 'disabled' | 'archived' | 'auto') => {
|
||
if (!accountId) return;
|
||
try {
|
||
await accountApi.setLifecycle(accountId, next);
|
||
message.success('状态已更新');
|
||
void loadOverview();
|
||
void loadEvents();
|
||
} catch (e: unknown) {
|
||
message.error(getErrorMessage(e));
|
||
}
|
||
};
|
||
|
||
|
||
/** 保存备注:备注是自由文本,走单个接口(标签那种短枚举才有批量)。 */
|
||
const handleSaveRemark = async () => {
|
||
if (!accountId) return;
|
||
try {
|
||
await accountApi.setRemark(accountId, remarkDraft.trim());
|
||
message.success('备注已更新');
|
||
setRemarkEditing(false);
|
||
void loadOverview();
|
||
void loadEvents();
|
||
} catch (e: unknown) {
|
||
message.error(getErrorMessage(e));
|
||
}
|
||
};
|
||
|
||
const handleAutomation = async (autoManaged: boolean) => {
|
||
if (!accountId) return;
|
||
try {
|
||
await accountApi.setAutomation(accountId, autoManaged);
|
||
message.success(autoManaged ? '已恢复自动化' : '已设为不参与自动化');
|
||
void loadOverview();
|
||
} catch (e: unknown) {
|
||
message.error(getErrorMessage(e));
|
||
}
|
||
};
|
||
|
||
return (
|
||
<div style={{ padding: 4 }}>
|
||
<Space style={{ marginBottom: 12 }} wrap>
|
||
<Button icon={<ArrowLeftOutlined />} onClick={() => navigate('/accounts')}>
|
||
返回账号列表
|
||
</Button>
|
||
<Button icon={<ReloadOutlined />} onClick={() => { void loadOverview(); void loadEvents(); }}>
|
||
刷新
|
||
</Button>
|
||
{status === 'disabled' || status === 'archived' ? (
|
||
<Popconfirm
|
||
title="恢复这个账号?将按当前 CK 情况重新判定状态"
|
||
onConfirm={() => void handleLifecycle('auto')}
|
||
>
|
||
<Button>恢复</Button>
|
||
</Popconfirm>
|
||
) : (
|
||
<>
|
||
<Popconfirm
|
||
title="停用这个账号?停用后不再参与任何自动化"
|
||
onConfirm={() => void handleLifecycle('disabled')}
|
||
>
|
||
<Button danger>停用</Button>
|
||
</Popconfirm>
|
||
<Popconfirm
|
||
title="归档这个账号?默认列表将不再显示它"
|
||
onConfirm={() => void handleLifecycle('archived')}
|
||
>
|
||
<Button>归档</Button>
|
||
</Popconfirm>
|
||
</>
|
||
)}
|
||
{overview?.auto_managed === false ? (
|
||
<Button onClick={() => void handleAutomation(true)}>恢复自动化</Button>
|
||
) : (
|
||
<Popconfirm
|
||
title="设为不参与自动化?账号仍可用,只是巡检/重登不再碰它"
|
||
onConfirm={() => void handleAutomation(false)}
|
||
>
|
||
<Button>不参与自动化</Button>
|
||
</Popconfirm>
|
||
)}
|
||
</Space>
|
||
|
||
<Spin spinning={loading}>
|
||
<Card
|
||
title={
|
||
<Space>
|
||
<Title level={5} style={{ margin: 0 }}>{overview?.username || '—'}</Title>
|
||
<Tag color={STATUS_TONES[status] ?? 'default'}>
|
||
{overview?.status_label || '—'}
|
||
</Tag>
|
||
{overview?.tag && <Tag color="blue">{overview.tag}</Tag>}
|
||
{overview?.auto_managed === false && <Tag color="orange">不参与自动化</Tag>}
|
||
</Space>
|
||
}
|
||
style={{ marginBottom: 12 }}
|
||
>
|
||
<Descriptions column={3} size="small" bordered>
|
||
<Descriptions.Item label="斗鱼昵称">{overview?.nickname || '-'}</Descriptions.Item>
|
||
<Descriptions.Item label="斗鱼 UID">{overview?.uid || '-'}</Descriptions.Item>
|
||
<Descriptions.Item label="导入时间">{formatTime(overview?.created_at)}</Descriptions.Item>
|
||
<Descriptions.Item label="游戏角色">
|
||
{overview?.game_name || '-'}
|
||
{overview?.game_channel ? <Text type="secondary"> ({overview.game_channel})</Text> : null}
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="绑定状态">{overview?.bind_status || '-'}</Descriptions.Item>
|
||
<Descriptions.Item label="备注">
|
||
{remarkEditing ? (
|
||
<Space.Compact style={{ width: '100%' }}>
|
||
<Input
|
||
size="small"
|
||
maxLength={256}
|
||
value={remarkDraft}
|
||
placeholder="给这个账号写点备注"
|
||
onChange={(e) => setRemarkDraft(e.target.value)}
|
||
onPressEnter={() => void handleSaveRemark()}
|
||
/>
|
||
<Button size="small" type="primary" onClick={() => void handleSaveRemark()}>
|
||
保存
|
||
</Button>
|
||
<Button size="small" onClick={() => setRemarkEditing(false)}>
|
||
取消
|
||
</Button>
|
||
</Space.Compact>
|
||
) : (
|
||
<Space size={4}>
|
||
<span>{overview?.remark || '-'}</span>
|
||
<Tooltip title="编辑备注">
|
||
<Button
|
||
type="text"
|
||
size="small"
|
||
icon={<EditOutlined />}
|
||
onClick={() => {
|
||
setRemarkDraft(overview?.remark || '');
|
||
setRemarkEditing(true);
|
||
}}
|
||
/>
|
||
</Tooltip>
|
||
</Space>
|
||
)}
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="状态变更">
|
||
{formatTime(overview?.status_changed_at)}
|
||
{overview?.status_reason ? (
|
||
<Text type="secondary">({overview.status_reason})</Text>
|
||
) : null}
|
||
</Descriptions.Item>
|
||
</Descriptions>
|
||
|
||
<div style={{ display: 'flex', gap: 32, marginTop: 16, flexWrap: 'wrap' }}>
|
||
<Statistic title="当前积分" value={overview?.points ?? '-'} />
|
||
<Statistic title="鱼翅余额" value={overview?.gold_balance ?? '-'} />
|
||
<Statistic title="兑换余额" value={overview?.exchange_balance ?? '-'} />
|
||
<Statistic
|
||
title="精英宝典"
|
||
value={overview?.elite_opened ? '已开通' : '未开通'}
|
||
valueStyle={{ color: overview?.elite_opened ? '#3f8600' : undefined, fontSize: 20 }}
|
||
/>
|
||
<Statistic
|
||
title="电竞手册"
|
||
value={overview?.esports_bind_status === 'esports_opened' ? '已开通' : (overview?.esports_bind_status || '未开通')}
|
||
valueStyle={{ fontSize: 20 }}
|
||
/>
|
||
</div>
|
||
|
||
<Descriptions column={3} size="small" style={{ marginTop: 16 }}>
|
||
<Descriptions.Item label="精英宝典开通时间">
|
||
{formatTime(overview?.opened_at?.elite)}
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="电竞手册开通时间">
|
||
{formatTime(overview?.opened_at?.esports)}
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label="最近 CK 检测">
|
||
{formatTime(stats?.last_ck_check_at)}
|
||
</Descriptions.Item>
|
||
</Descriptions>
|
||
|
||
<div style={{ display: 'flex', gap: 32, marginTop: 12, flexWrap: 'wrap' }}>
|
||
<Statistic title="累计充值" value={stats?.recharge_total ?? 0} prefix="¥" />
|
||
<Statistic title="兑换次数" value={stats?.exchange_count ?? 0} />
|
||
<Statistic title="累计消耗积分" value={stats?.points_spent ?? 0} />
|
||
<Statistic title="CK 失效次数" value={stats?.ck_invalid_count ?? 0} />
|
||
<Statistic title="事件总数" value={stats?.event_count ?? 0} />
|
||
</div>
|
||
|
||
<Text type="secondary" style={{ display: 'block', marginTop: 12, fontSize: 12 }}>
|
||
开通时间与累计统计来自「账号事件」,从该功能上线起开始记录;
|
||
更早的历史请到「登录任务 / 精英宝典 / 电竞手册 / 和平小店」页查看。
|
||
</Text>
|
||
</Card>
|
||
</Spin>
|
||
|
||
<Card
|
||
title="账号时间线"
|
||
extra={
|
||
<Space>
|
||
<Select
|
||
size="small"
|
||
allowClear
|
||
style={{ width: 120 }}
|
||
placeholder="全部分类"
|
||
value={category || undefined}
|
||
onChange={(v) => { setCategory(v || ''); setPage(1); }}
|
||
options={CATEGORY_OPTIONS}
|
||
/>
|
||
<Select
|
||
size="small"
|
||
style={{ width: 140 }}
|
||
value={eventType}
|
||
onChange={(v) => { setEventType(v); setPage(1); }}
|
||
options={[
|
||
{ value: '', label: '全部事件' },
|
||
...Object.entries(EVENT_STYLE).map(([value, s]) => ({ value, label: s.label })),
|
||
]}
|
||
/>
|
||
<Tooltip title="从上线起累积,不回填历史">
|
||
<Text type="secondary" style={{ fontSize: 12 }}>共 {eventTotal} 条</Text>
|
||
</Tooltip>
|
||
</Space>
|
||
}
|
||
>
|
||
<Table
|
||
columns={columns}
|
||
dataSource={events}
|
||
rowKey="id"
|
||
size="small"
|
||
loading={eventsLoading}
|
||
pagination={{
|
||
current: page,
|
||
pageSize,
|
||
total: eventTotal,
|
||
size: 'small',
|
||
showSizeChanger: true,
|
||
pageSizeOptions: ['20', '50', '100'],
|
||
showTotal: (t) => `共 ${t} 条`,
|
||
onChange: (p, s) => { setPage(p); setPageSize(s); },
|
||
}}
|
||
locale={{ emptyText: <Empty description="暂无事件(功能上线后开始记录)" /> }}
|
||
/>
|
||
</Card>
|
||
</div>
|
||
);
|
||
}
|