feat(audit): 审计日志业务化——对象名与字段中文化、操作来源与类型筛选
一、target_name 业务化(后端 audit_listener.py)
优先级:业务标识(request_no/outbound_no/borrow_no/sku/bom_no…)→
名称字段 → 关联物料名 → 「中文表名 - 业务号或ID」。
新增 TABLE_LABELS 表名到中文的映射(18 张白名单表全覆盖)。
用户看到的从 scrap_approval ID:371 变为
「出库申请单 - APR-OUT-20260805-1550-0005」这类可理解的对象描述。
二、前端字段中文化(AuditLog.vue)
· fieldMap 由 11 项扩充到 110+ 项,覆盖审批单/流水/库存/主数据/系统管理五类;
· 修复关键 bug:fieldMap 原先只作用于「变更对比」区,「删除快照」与
「新增详情」两区直接渲染原始 key(label 直接取 String(key)),
这正是详情里满是英文列名的直接原因。现三区统一经 fieldLabel() 取值。
三、时间显示修正
后端已改写北京时间,前端原先补 Z 当 UTC 解析会造成二次 +8 小时,
改为按 +08:00 理解该字符串。
四、新增两类筛选
· 操作来源(真实用户 / 系统操作 / 全部),默认「真实用户」。
历史存量含约 1.8 万条 username=system 的噪声日志,会把列表刷屏。
· 操作类型别名归一:历史数据中 action 有两套写法(早期装饰器写中文
新增/修改/删除,现行监听器写大写 CREATE/UPDATE/DELETE),导致下拉
同时出现二者、且选中文项只能搜到 3-4 月的老数据。现 ACTION_ALIASES
把任意写法归一化后展开匹配,下拉只暴露 3 个规范值,
历史数据无需迁移即可被正确检索。
This commit is contained in:
@ -11,6 +11,38 @@ import json
|
||||
audit_bp = Blueprint('audit', __name__)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# 操作类型归一化
|
||||
#
|
||||
# 问题背景:历史数据里 action 有两套写法 —— 早期装饰器(已废弃)写入中文
|
||||
# (新增/修改/删除/批量删除…),现行监听器写入大写英文(CREATE/UPDATE/DELETE)。
|
||||
# 前端下拉框直接取 DISTINCT action,于是同时出现「CREATE」和「新增」两个选项,
|
||||
# 而表格里二者又都显示为「新增」(actionMap 做了映射),用户无法分辨。
|
||||
#
|
||||
# 后果:用户选了看得懂的中文项,只能搜到 3-4 月的历史数据,误以为"没有最近的内容"。
|
||||
#
|
||||
# 处理:对外只暴露规范值(CREATE/UPDATE/DELETE),筛选时自动展开到全部别名,
|
||||
# 历史数据无需迁移即可被正确检索。
|
||||
# =============================================================================
|
||||
ACTION_ALIASES = {
|
||||
'CREATE': ('CREATE', 'create', 'INSERT', 'insert', '新增', '批量生成'),
|
||||
'UPDATE': ('UPDATE', 'update', '修改', '分配', '归还'),
|
||||
'DELETE': ('DELETE', 'delete', '删除', '批量删除'),
|
||||
}
|
||||
|
||||
# 反向索引:任意别名 → 规范值
|
||||
_ALIAS_TO_CANON = {
|
||||
alias: canon
|
||||
for canon, aliases in ACTION_ALIASES.items()
|
||||
for alias in aliases
|
||||
}
|
||||
|
||||
|
||||
def canon_action(action):
|
||||
"""把任意写法的 action 归一化为规范值;无法识别时原样返回"""
|
||||
return _ALIAS_TO_CANON.get((action or '').strip(), (action or '').strip())
|
||||
|
||||
|
||||
@audit_bp.route('/logs', methods=['GET'])
|
||||
@jwt_required()
|
||||
@permission_required('system_audit')
|
||||
@ -29,15 +61,38 @@ def get_audit_logs():
|
||||
start_date = request.args.get('start_date', '').strip()
|
||||
end_date = request.args.get('end_date', '').strip()
|
||||
|
||||
# ★ 操作人类型:all(默认) / user(仅真实用户) / system(仅系统)
|
||||
#
|
||||
# 背景:改造前的全局监听器没有请求上下文守卫,系统初始化与后台定时任务
|
||||
# 产生了大量 username='system' 的日志(历史存量约 1.8 万条),会把列表刷屏。
|
||||
# 新监听器已加守卫不再产生此类记录,但存量数据仍需要能筛掉。
|
||||
operator_type = request.args.get('operator_type', 'all').strip().lower()
|
||||
if operator_type not in ('all', 'user', 'system'):
|
||||
operator_type = 'all'
|
||||
|
||||
# 构建查询
|
||||
query = AuditLog.query
|
||||
|
||||
if operator_type == 'user':
|
||||
# 真实用户:排除 system 占位账号
|
||||
query = query.filter(AuditLog.username != 'system')
|
||||
elif operator_type == 'system':
|
||||
query = query.filter(AuditLog.username == 'system')
|
||||
|
||||
if username:
|
||||
query = query.filter(AuditLog.username.like(f'%{username}%'))
|
||||
if module:
|
||||
query = query.filter(AuditLog.module == module)
|
||||
if action:
|
||||
query = query.filter(AuditLog.action == action)
|
||||
# ★ 兼容历史别名:先把任意写法(中文/小写)归一化为规范值,
|
||||
# 再展开为该值的全部等价写法一起匹配。
|
||||
# 否则选「新增」只能搜到早期中文 action 的数据(约 3-4 月),
|
||||
# 会让用户误以为"没有最近的内容"。
|
||||
canon = canon_action(action)
|
||||
if canon in ACTION_ALIASES:
|
||||
query = query.filter(AuditLog.action.in_(ACTION_ALIASES[canon]))
|
||||
else:
|
||||
query = query.filter(AuditLog.action == action)
|
||||
if target_id:
|
||||
query = query.filter(AuditLog.target_id == target_id)
|
||||
if start_date:
|
||||
@ -84,7 +139,10 @@ def get_audit_logs():
|
||||
actions_query = actions_query.join(SysUser, func.split_part(SysUser.username, '/', 2) == AuditLog.username) \
|
||||
.filter(SysUser.department == company_limit)
|
||||
modules = [m[0] for m in modules_query.all() if m[0]]
|
||||
actions = [a[0] for a in actions_query.all() if a[0]]
|
||||
|
||||
# ★ action 下拉项归一化:把 CREATE/create/新增 等别名合并为一个规范值,
|
||||
# 避免下拉框出现「CREATE」与「新增」两个语义重复的选项。
|
||||
actions = sorted({canon_action(a[0]) for a in actions_query.all() if a[0]})
|
||||
|
||||
return jsonify({
|
||||
'code': 200,
|
||||
|
||||
Reference in New Issue
Block a user