Compare commits

...
2 Commits
Author SHA1 Message Date
836061373 7cca63b9d9 feat: 操作审计描述拆为独立列(置于资源/路径与结果之间)
动作列仅保留方法徽标并收窄至 6rem,describeOperation 推导的中文描述
移入新的描述列(宽 10rem,超宽截断),题头与数据行同步调整;
外观回归测试更新为六列结构。
2026-11-21 20:33:58 +08:00
836061373 8aae68c95c feat: 操作审计动作列与详情增加中文操作描述(登录/新建用户/发布文章等)
- utils/audit.ts 新增 describeOperation 纯函数:由 method/resourceType/path
  推导中文短语(登录、登出、认证操作、动词+资源中文名、子动作拼接如
  发布文章/启用插件、完整短语如修改密码),纯展示层推导,历史记录同样适用
- 列表动作列在徽标后渲染描述(列宽 6rem→10rem),题头改为「动作 / 描述」
- 详情抽屉顶部新增「描述」行
- 新增 utils/audit.spec.ts 10 项用例覆盖全部推导与回退路径,
  外观回归测试同步列宽与动作列结构
2026-11-21 20:28:09 +08:00
5 changed files with 277 additions and 3 deletions
@@ -0,0 +1,135 @@
import { describe, expect, it } from 'vitest'
import type { AuditLogSpec } from './audit'
import { describeOperation } from './audit'
// 操作描述推导(这条操作真正干了什么):由 method / resourceType / path 推导中文短语。
// 历史记录不落库新字段,描述纯展示层推导,因此必须覆盖所有回退路径,防止出现英文原始值。
function spec(overrides: Partial<AuditLogSpec>): AuditLogSpec {
return {
username: 'admin',
method: 'POST',
resourceType: 'other',
path: '/apis/api.console.halo.run/v1alpha1/other',
statusCode: 200,
remoteIp: '127.0.0.1',
...overrides,
}
}
describe('describeOperation', () => {
it('登录端点(含查询串)描述为 登录', () => {
expect(describeOperation(spec({
method: 'POST',
resourceType: 'auth',
path: '/login',
}))).toBe('登录')
// GET /login?redirect_uri=... 也应识别为登录(spec.path 含查询串)
expect(describeOperation(spec({
method: 'GET',
resourceType: 'auth',
path: '/login?redirect_uri=https%3A%2F%2Fwww.guichumo.com%2Fconsole',
}))).toBe('登录')
})
it('登出端点描述为 登出', () => {
expect(describeOperation(spec({
method: 'POST',
resourceType: 'auth',
path: '/logout',
}))).toBe('登出')
})
it('auth 类型但非登录/登出路径回退为 认证操作', () => {
expect(describeOperation(spec({
method: 'POST',
resourceType: 'auth',
path: '/auth/other',
}))).toBe('认证操作')
})
it('基础动作用 方法动词 + 资源中文名(新建用户 / 修改文章 / 删除插件)', () => {
expect(describeOperation(spec({
method: 'POST',
resourceType: 'users',
path: '/apis/api.console.halo.run/v1alpha1/users',
}))).toBe('新建用户')
expect(describeOperation(spec({
method: 'PUT',
resourceType: 'posts',
path: '/apis/content.halo.run/v1alpha1/posts/post-abc',
}))).toBe('修改文章')
expect(describeOperation(spec({
method: 'PATCH',
resourceType: 'themes',
path: '/apis/api.console.halo.run/v1alpha1/themes/theme-abc',
}))).toBe('修改主题')
expect(describeOperation(spec({
method: 'DELETE',
resourceType: 'plugins',
path: '/apis/api.console.halo.run/v1alpha1/plugins/plugin-abc',
}))).toBe('删除插件')
})
it('子动作段与资源名拼接(发布文章 / 启用插件 / 停用插件)', () => {
expect(describeOperation(spec({
method: 'POST',
resourceType: 'posts',
path: '/apis/api.console.halo.run/v1alpha1/posts/post-abc/publish',
}))).toBe('发布文章')
expect(describeOperation(spec({
method: 'POST',
resourceType: 'plugins',
path: '/apis/api.console.halo.run/v1alpha1/plugins/plugin-abc/enable',
}))).toBe('启用插件')
expect(describeOperation(spec({
method: 'POST',
resourceType: 'plugins',
path: '/apis/api.console.halo.run/v1alpha1/plugins/plugin-abc/disable',
}))).toBe('停用插件')
expect(describeOperation(spec({
method: 'POST',
resourceType: 'posts',
path: '/apis/api.console.halo.run/v1alpha1/posts/post-abc/recycle',
}))).toBe('移入回收站文章')
})
it('子动作为完整短语时不拼接资源名(修改密码)', () => {
expect(describeOperation(spec({
method: 'PUT',
resourceType: 'users',
path: '/apis/api.console.halo.run/v1alpha1/users/admin/password',
}))).toBe('修改密码')
})
it('未识别的子动作回退为基础动词(/posts/{name}/content → 修改文章)', () => {
expect(describeOperation(spec({
method: 'PUT',
resourceType: 'posts',
path: '/apis/content.halo.run/v1alpha1/posts/post-abc/content',
}))).toBe('修改文章')
})
it('未识别的资源类型回退为原始值(动词 + resourceType)', () => {
expect(describeOperation(spec({
method: 'POST',
resourceType: 'workflows',
path: '/apis/audit.guichumo.com/v1alpha1/workflows',
}))).toBe('新建workflows')
})
it('未识别的 HTTP 方法回退为原始方法', () => {
expect(describeOperation(spec({
method: 'TRACE',
resourceType: 'posts',
path: '/apis/content.halo.run/v1alpha1/posts/post-abc',
}))).toBe('TRACE文章')
})
it('无法解析路径(other)回退为 动词 + other', () => {
expect(describeOperation(spec({
method: 'DELETE',
resourceType: 'other',
path: '/somewhere/odd',
}))).toBe('删除other')
})
})
@@ -60,6 +60,107 @@ export function formatTime(iso?: string | null): string {
)
}
/** 资源类型的中文展示名(描述用;未知类型回退原始值)。 */
const RESOURCE_LABELS: Record<string, string> = {
posts: '文章',
singlepages: '页面',
comments: '评论',
replies: '回复',
users: '用户',
roles: '角色',
settings: '设置',
configmaps: '配置',
plugins: '插件',
themes: '主题',
attachments: '附件',
menus: '菜单',
categories: '分类',
tags: '标签',
}
/** 动作方法的基础动词(未知方法回退原始值)。 */
const METHOD_VERBS: Record<string, string> = {
POST: '新建',
PUT: '修改',
PATCH: '修改',
DELETE: '删除',
GET: '查看',
}
/**
* 子资源动作段({@code /apis/.../{plural}/{name}/{action}} 的 action)→ 动词,
* 与资源名拼接成短语,如 publish + 文章 = 发布文章。
*/
const SUB_ACTION_VERBS: Record<string, string> = {
publish: '发布',
unpublish: '取消发布',
recycle: '移入回收站',
restore: '恢复',
enable: '启用',
disable: '停用',
upgrade: '升级',
uninstall: '卸载',
reload: '重载',
activation: '启用',
reply: '回复',
recalculate: '重新统计',
}
/** 子资源动作段 → 完整短语(不拼接资源名)。 */
const SUB_ACTION_PHRASES: Record<string, string> = {
password: '修改密码',
resetpassword: '重置密码',
}
/** 剥离路径的查询串(spec.path 存的是含查询串的完整路径)。 */
function pathWithoutQuery(path?: string | null): string {
if (!path) {
return ''
}
const queryStart = path.indexOf('?')
return queryStart >= 0 ? path.slice(0, queryStart) : path
}
/**
* 操作描述(这条操作真正干了什么):由 method / resourceType / path 推导中文短语,
* 如 登录、登出、新建用户、修改文章、发布文章、启用插件、修改密码。
*
* <p>纯展示层推导,不落库:历史记录同样适用;未识别的子动作回退为「动词 + 资源名」,
* 未识别的资源类型直接使用原始 resourceType。
*/
export function describeOperation(
spec: Pick<AuditLogSpec, 'method' | 'resourceType' | 'path'>,
): string {
const cleanPath = pathWithoutQuery(spec.path)
if (spec.resourceType === 'auth' || cleanPath === '/login' || cleanPath === '/logout') {
if (cleanPath === '/login') {
return '登录'
}
if (cleanPath === '/logout') {
return '登出'
}
return '认证操作'
}
const label = RESOURCE_LABELS[spec.resourceType] ?? spec.resourceType
const verb = METHOD_VERBS[spec.method] ?? spec.method
// /apis/{group}/{version}/{plural}/{name}/{action}:
// filter(Boolean) 去掉首段空串后,segments 为 [apis, group, version, plural, name, action],
// 子动作段为 segments[5]
const segments = cleanPath.split('/').filter(Boolean)
const action = segments.length >= 6 ? segments[5].toLowerCase() : null
if (action) {
const phrase = SUB_ACTION_PHRASES[action]
if (phrase) {
return phrase
}
const subVerb = SUB_ACTION_VERBS[action]
if (subVerb) {
return `${subVerb}${label}`
}
}
return `${verb}${label}`
}
const METHOD_BADGE_CLASS: Record<string, string> = {
POST: 'method-post',
PUT: 'method-put',
@@ -3,6 +3,7 @@ import { computed } from 'vue'
import { VButton, VSpace } from '@halo-dev/components'
import type { AuditLog } from '@/utils/audit'
import {
describeOperation,
displayUsername,
formatTime,
isSuccessful,
@@ -61,6 +62,10 @@ const rawJson = computed(() =>
<dt>操作者</dt>
<dd>{{ displayUsername(log.spec.username) }}</dd>
</div>
<div class="detail-row">
<dt>描述</dt>
<dd>{{ describeOperation(log.spec) }}</dd>
</div>
<div class="detail-row">
<dt>动作</dt>
<dd>
@@ -49,11 +49,12 @@ describe('AuditLogList 页面外观', () => {
expect(rule).toMatch(/border-top:\s*1px\s+solid\s+#eaecf0/)
})
it('列表数据行上方渲染列题头,五个列标签齐全', () => {
it('列表数据行上方渲染列题头,六个列标签齐全', () => {
expect(sfc).toContain('<div class="audit-header">')
expect(sfc).toContain('操作者 / 时间')
expect(sfc).toContain('>动作<')
expect(sfc).toContain('资源 / 路径')
expect(sfc).toContain('>描述<')
expect(sfc).toContain('>结果<')
expect(sfc).toContain('>IP<')
})
@@ -63,6 +64,7 @@ describe('AuditLogList 页面外观', () => {
['col-username', '12rem'],
['col-method', '6rem'],
['col-resource', '20rem'],
['col-desc', '10rem'],
['col-status', '4rem'],
['col-ip', '9rem'],
]
@@ -88,11 +90,24 @@ describe('AuditLogList 页面外观', () => {
expect(header).toMatch(/border-bottom:\s*1px\s+solid\s+#eaecf0/)
})
it('动作列仅保留彩色徽标、结果列徽标经 #title 渲染', () => {
it('动作列渲染彩色徽标,描述独立成列且位于资源/路径与结果之间', () => {
// 组件(@halo-dev/components 2.26.0)在无 title/#title 时不渲染 #extra,
// 原先结果徽标实际不可见;原先方法列 title 与徽标重复渲染同一文案。
// 描述原先与徽标同行挤在动作列,现拆为独立列(describeOperation 推导的
// 中文描述,如 登录/新建用户/发布文章),题头与数据行均置于资源/路径之后、结果之前。
expect(sfc).not.toContain(':title="log.spec.method"')
expect(sfc).not.toContain('action-cell')
expect(sfc).toMatch(/<template #title>\s+<span\s+class="method-badge"/)
expect(sfc).toContain('describeOperation(log.spec)')
expect(sfc).toMatch(/<span class="action-desc">\{\{ describeOperation\(log\.spec\) \}\}<\/span>/)
expect(sfc).toMatch(/<template #title>\s+<span\s+class="status-badge"/)
// 数据行内 resource-text(资源/路径列)在 action-desc(描述列)之前,
// action-desc(描述列)在 status-badge(结果列)之前
const resourceIdx = sfc.indexOf('class="resource-text"')
const descIdx = sfc.indexOf('class="action-desc"')
const statusIdx = sfc.indexOf('class="status-badge"')
expect(resourceIdx).toBeGreaterThan(-1)
expect(descIdx).toBeGreaterThan(resourceIdx)
expect(statusIdx).toBeGreaterThan(descIdx)
})
})
@@ -15,6 +15,7 @@ import {
} from '@halo-dev/components'
import type { AuditLog, ListResult } from '@/utils/audit'
import {
describeOperation,
displayUsername,
formatTime,
isSuccessful,
@@ -248,6 +249,7 @@ onMounted(fetchLogs)
<span class="header-col col-username">操作者 / 时间</span>
<span class="header-col col-method">动作</span>
<span class="header-col col-resource">资源 / 路径</span>
<span class="header-col col-desc">描述</span>
</div>
<div class="header-end">
<span class="header-col col-status">结果</span>
@@ -266,7 +268,7 @@ onMounted(fetchLogs)
<span class="row-sub">{{ formatTime(log.metadata.creationTimestamp) }}</span>
</template>
</VEntityField>
<!-- 动作列:仅保留彩色徽标(原先 title 与徽标重复渲染两个相同文案) -->
<!-- 动作列:彩色徽标标注请求方法 -->
<VEntityField width="6rem">
<template #title>
<span class="method-badge" :class="methodBadgeClass(log.spec.method)">
@@ -285,6 +287,12 @@ onMounted(fetchLogs)
<span class="row-sub">{{ log.spec.path }}</span>
</template>
</VEntityField>
<!-- 描述列:该操作实际语义的中文描述(如 登录/新建用户/发布文章) -->
<VEntityField width="10rem">
<template #title>
<span class="action-desc">{{ describeOperation(log.spec) }}</span>
</template>
</VEntityField>
</template>
<template #end>
<!-- 结果列:徽标经 #title 插槽渲染(当前版本组件在无 title 时
@@ -409,6 +417,7 @@ onMounted(fetchLogs)
.col-username { width: 12rem; }
.col-method { width: 6rem; }
.col-resource { width: 20rem; }
.col-desc { width: 10rem; }
.col-status { width: 4rem; }
.col-ip { width: 9rem; }
.audit-error {
@@ -441,6 +450,15 @@ onMounted(fetchLogs)
color: #fff;
background: #6b7280;
}
/* 描述列:中文操作描述,超宽时截断(列宽固定 10rem) */
.action-desc {
display: inline-block;
max-width: 9.75rem;
font-size: 0.8125rem;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.method-post { background: #1e7ee0; }
.method-put { background: #d97706; }
.method-delete { background: var(--audit-red); }