import type { H3Event } from 'h3' import { getDb } from './db' import { getCurrentAdmin } from './adminAuth' /** * 记录后台操作审计日志(增删改内容/用户/评论/联系/订阅/上传等)。 * 操作者取后台管理员会话;写入失败不影响主流程(仅记录日志)。 */ export function writeAudit(event: H3Event, action: string, target: string, detail?: string): void { try { const admin = getCurrentAdmin(event) getDb() .prepare('INSERT INTO audit_logs (actor_id, actor_name, action, target, detail) VALUES (?, ?, ?, ?, ?)') .run(admin?.id ?? null, admin?.username ?? 'system', action, target, detail ?? null) } catch (err) { console.error('[audit] write failed:', err) } }