feat(account): 补上改备注接口,remark_changed 事件从此有发射点
`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、**新权限点确实在三个角色的默认权限里**
(防止「加了接口但没人能用」)
This commit is contained in:
@@ -15,6 +15,7 @@ PERMISSIONS = {
|
||||
"account:view_assigned": "查看分配给自己的账号",
|
||||
"account:import": "导入账号",
|
||||
"account:tag": "修改账号标签",
|
||||
"account:remark": "修改账号备注",
|
||||
"account:check": "账号检测",
|
||||
"account:assign": "分配账号给客服",
|
||||
"account:delete": "删除账号",
|
||||
@@ -67,6 +68,7 @@ ROLE_PERMISSIONS = {
|
||||
"account:view_all",
|
||||
"account:import",
|
||||
"account:tag",
|
||||
"account:remark",
|
||||
"account:check",
|
||||
"account:assign",
|
||||
"account:manage",
|
||||
@@ -100,6 +102,7 @@ ROLE_PERMISSIONS = {
|
||||
"support": [
|
||||
"account:view_assigned",
|
||||
"account:tag",
|
||||
"account:remark",
|
||||
"cookie:operate",
|
||||
"douyu:task",
|
||||
"yyb:session",
|
||||
|
||||
@@ -26,6 +26,7 @@ from ..schemas import (
|
||||
AccountImport,
|
||||
AccountLifecycleUpdate,
|
||||
AccountOut,
|
||||
AccountRemark,
|
||||
AccountTag,
|
||||
BatchAssign,
|
||||
)
|
||||
@@ -37,6 +38,7 @@ from ..services.account_events_service import (
|
||||
EVENT_ESPORTS_OPENED,
|
||||
EVENT_EXCHANGE,
|
||||
EVENT_RECHARGE,
|
||||
EVENT_REMARK_CHANGED,
|
||||
EVENT_TAG_CHANGED,
|
||||
emit_bulk,
|
||||
)
|
||||
@@ -563,6 +565,40 @@ def set_account_automation(
|
||||
}
|
||||
|
||||
|
||||
@router.put("/{account_id}/remark")
|
||||
def set_account_remark(
|
||||
account_id: int,
|
||||
req: AccountRemark,
|
||||
db: Session = Depends(get_db),
|
||||
current: User = Depends(require_permission("account:remark")),
|
||||
):
|
||||
"""设置单个账号的备注。
|
||||
|
||||
备注是自由文本(标签是短枚举),所以走单个接口、不做批量 —— 批量给一堆账号写
|
||||
同一句备注没有实际意义。与改标签一样:审计 + 账号时间线一次写完。
|
||||
"""
|
||||
acc = db.query(Account).filter(Account.id == account_id).first()
|
||||
if not acc:
|
||||
raise HTTPException(status_code=404, detail="账号不存在")
|
||||
|
||||
old_remark = acc.remark or ""
|
||||
new_remark = (req.remark or "").strip()[:256]
|
||||
if old_remark == new_remark:
|
||||
return {"message": "备注未变化", "success": True, "remark": new_remark}
|
||||
acc.remark = new_remark
|
||||
record_account_action(
|
||||
db,
|
||||
current,
|
||||
account=acc,
|
||||
action="account:remark",
|
||||
event_type=EVENT_REMARK_CHANGED,
|
||||
title=f"备注:{old_remark or '(空)'} → {new_remark or '(空)'}",
|
||||
detail={"from": old_remark, "to": new_remark},
|
||||
)
|
||||
db.commit()
|
||||
return {"message": "备注已更新", "success": True, "remark": new_remark}
|
||||
|
||||
|
||||
def _apply_tag_bulk(db: Session, current: User, ids: list[int], tag: str) -> None:
|
||||
"""批量改标签:更新 + 审计 + **逐账号写事件**。
|
||||
|
||||
|
||||
@@ -148,6 +148,10 @@ class AccountTag(BaseModel):
|
||||
account_ids: list[int] | None = None
|
||||
|
||||
|
||||
class AccountRemark(BaseModel):
|
||||
remark: str | None = None
|
||||
|
||||
|
||||
class AccountLifecycleUpdate(BaseModel):
|
||||
"""人工设定生命周期状态。
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ EVENT_ELITE_OPENED = "elite_opened" # 精英宝典开通
|
||||
EVENT_ESPORTS_OPENED = "esports_opened" # 电竞手册开通
|
||||
EVENT_ASSIGNED = "assigned" # 分配客服
|
||||
EVENT_TAG_CHANGED = "tag_changed" # 标签变更
|
||||
EVENT_REMARK_CHANGED = "remark_changed" # 备注变更
|
||||
EVENT_STATUS_CHANGED = "status_changed" # 生命周期状态变迁(停用/归档/失效…)
|
||||
|
||||
# 有符号金额的事件类型(用于页面「累计充值/累计消耗」):
|
||||
|
||||
@@ -28,6 +28,8 @@ export const accountApi = {
|
||||
api.post<MessageCountResponse, MessageCountResponse>('/accounts/batch-assign', { account_ids, assigned_to }),
|
||||
assignmentsSummary: () =>
|
||||
api.get<AssignmentsSummary, AssignmentsSummary>('/accounts/assignments/summary'),
|
||||
setRemark: (id: number, remark: string) =>
|
||||
api.put<MessageResponse, MessageResponse>(`/accounts/${id}/remark`, { remark }),
|
||||
setTag: (id: number, tag: string) =>
|
||||
api.put<MessageResponse, MessageResponse>(`/accounts/${id}/tag`, { tag }),
|
||||
batchTag: (account_ids: number[], tag: string) =>
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { useNavigate, useParams } from 'react-router-dom';
|
||||
import {
|
||||
Button, Card, Descriptions, Empty, Popconfirm, Select, Space, Spin, Statistic, Table, Tag, Tooltip, Typography,
|
||||
Button, Card, Descriptions, Empty, Input, Popconfirm, Select, Space, Spin, Statistic, Table, Tag, Tooltip, Typography,
|
||||
} from 'antd';
|
||||
import type { TableProps } from 'antd';
|
||||
import {
|
||||
ArrowLeftOutlined, ReloadOutlined,
|
||||
ArrowLeftOutlined, EditOutlined, ReloadOutlined,
|
||||
} from '@ant-design/icons';
|
||||
import { accountApi, type AccountEventItem, type AccountOverview } from '../api/modules';
|
||||
import { getErrorMessage } from '../utils/error';
|
||||
@@ -34,6 +34,7 @@ const EVENT_STYLE: Record<string, { label: string; color: string }> = {
|
||||
esports_opened: { label: '电竞手册', color: 'volcano' },
|
||||
assigned: { label: '分配', color: 'default' },
|
||||
tag_changed: { label: '标签', color: 'default' },
|
||||
remark_changed: { label: '备注', color: 'default' },
|
||||
};
|
||||
|
||||
/** 事件粗分类(与后端 `account_events_service.CATEGORY_*` 对应)。 */
|
||||
@@ -90,6 +91,8 @@ export default function AccountDetailPage() {
|
||||
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;
|
||||
@@ -181,6 +184,21 @@ export default function AccountDetailPage() {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/** 保存备注:备注是自由文本,走单个接口(标签那种短枚举才有批量)。 */
|
||||
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 {
|
||||
@@ -259,7 +277,41 @@ export default function AccountDetailPage() {
|
||||
{overview?.game_channel ? <Text type="secondary"> ({overview.game_channel})</Text> : null}
|
||||
</Descriptions.Item>
|
||||
<Descriptions.Item label="绑定状态">{overview?.bind_status || '-'}</Descriptions.Item>
|
||||
<Descriptions.Item label="备注">{overview?.remark || '-'}</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 ? (
|
||||
|
||||
Reference in New Issue
Block a user