Files
henan-backend/ruoyi-ui/src/utils/operationAudit.js
T
2026-09-21 19:21:35 +08:00

40 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const states = {
unchanged: '已核对,业务字段没有变化',
unavailable: '未能取得完整的修改前后数据',
failed: '操作失败,未确认生效的改动',
notCaptured: '此操作未记录字段对比'
}
function toParamRows(params) {
if (!params || typeof params !== 'object') return []
return Object.keys(params).map(key => ({ label: key, value: String(params[key]) }))
}
export function parseOperationAudit(text) {
const empty = { action: '', target: '', changes: [], params: [], notice: '', structured: false }
if (!text) return empty
try {
const data = JSON.parse(text)
if (data && data.auditVersion === 1) {
const target = data.target || {}
const changes = data.state === 'changed' && Array.isArray(data.changes)
? data.changes.filter(c => c && typeof c.label === 'string' && typeof c.before === 'string' && typeof c.after === 'string') : []
return {
action: typeof data.action === 'string' ? data.action : '操作',
target: target.id == null ? '' : '用户ID:' + target.id + (typeof target.name === 'string' && target.name ? '(' + target.name + ')' : ''),
changes,
params: toParamRows(data.params),
notice: [states[data.state] || '', data.truncated ? '内容超出存储上限,部分改动未展示' : ''].filter(Boolean).join(';'),
structured: true
}
}
return { ...empty, action: '历史记录', notice: '历史记录未保存可验证的前后值' }
} catch (e) { /* Legacy action strings are not JSON. */ }
if (!text.startsWith('点击【')) return { ...empty, action: '历史记录', notice: '历史记录未保存可验证的前后值' }
const separator = text.indexOf('|')
if (separator < 0) return { ...empty, action: text, notice: '此记录未保存字段对比' }
const changes = text.slice(separator + 1).split('\n').map(line => line.split('\t'))
.filter(parts => parts.length === 3).map(parts => ({ label: parts[0], before: parts[1], after: parts[2] }))
return { ...empty, action: text.slice(0, separator), changes, notice: changes.length ? '' : '历史记录未保存可验证的前后值' }
}