/** * 审计日志的类型定义与展示辅助。 */ export interface AuditLogSpec { username: string method: string resourceType: string resourceName?: string | null path: string statusCode: number remoteIp: string userAgent?: string | null } export interface AuditLog { metadata: { name: string creationTimestamp: string version?: number } spec: AuditLogSpec } export interface ListResult { page: number size: number total: number items: T[] first?: boolean last?: boolean hasNext?: boolean hasPrevious?: boolean totalPages?: number } export const UNKNOWN_USERNAME = 'unknown' /** 展示用操作者名:登录失败的“未知用户”占位。 */ export function displayUsername(username?: string | null): string { if (!username) { return UNKNOWN_USERNAME } return username === UNKNOWN_USERNAME ? '未知用户' : username } /** 时间格式化:本地时区 YYYY-MM-DD HH:mm:ss。 */ export function formatTime(iso?: string | null): string { if (!iso) { return '-' } const date = new Date(iso) if (Number.isNaN(date.getTime())) { return iso } const pad = (n: number) => String(n).padStart(2, '0') return ( `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ` + `${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}` ) } /** 资源类型的中文展示名(描述用;未知类型回退原始值)。 */ const RESOURCE_LABELS: Record = { posts: '文章', singlepages: '页面', comments: '评论', replies: '回复', users: '用户', roles: '角色', settings: '设置', configmaps: '配置', plugins: '插件', themes: '主题', attachments: '附件', menus: '菜单', categories: '分类', tags: '标签', } /** 动作方法的基础动词(未知方法回退原始值)。 */ const METHOD_VERBS: Record = { POST: '新建', PUT: '修改', PATCH: '修改', DELETE: '删除', GET: '查看', } /** * 子资源动作段({@code /apis/.../{plural}/{name}/{action}} 的 action)→ 动词, * 与资源名拼接成短语,如 publish + 文章 = 发布文章。 */ const SUB_ACTION_VERBS: Record = { publish: '发布', unpublish: '取消发布', recycle: '移入回收站', restore: '恢复', enable: '启用', disable: '停用', upgrade: '升级', uninstall: '卸载', reload: '重载', activation: '启用', reply: '回复', recalculate: '重新统计', } /** 子资源动作段 → 完整短语(不拼接资源名)。 */ const SUB_ACTION_PHRASES: Record = { 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 推导中文短语, * 如 登录、登出、新建用户、修改文章、发布文章、启用插件、修改密码。 * *

纯展示层推导,不落库:历史记录同样适用;未识别的子动作回退为「动词 + 资源名」, * 未识别的资源类型直接使用原始 resourceType。 */ export function describeOperation( spec: Pick, ): 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 = { POST: 'method-post', PUT: 'method-put', DELETE: 'method-delete', PATCH: 'method-patch', } export function methodBadgeClass(method?: string | null): string { return method ? METHOD_BADGE_CLASS[method] ?? 'method-other' : 'method-other' } /** 结果语义:2xx/3xx 成功,其余失败。 */ export function isSuccessful(statusCode?: number | null): boolean { return statusCode != null && statusCode >= 200 && statusCode < 400 } /** datetime-local 输入值(本地时区)转 ISO-8601 Instant。 */ export function localDateTimeToInstant(value: string): string | null { if (!value) { return null } const date = new Date(value) return Number.isNaN(date.getTime()) ? null : date.toISOString() }