保存三数据库日志与三十天清理实现,继续补充验收
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-08 22:30:50 +08:00
1 parent 9bdf866a87
commit 5faadcd6eb
41 files changed
+1994 -59

No files matched your search

@@ -164,7 +164,13 @@
<div v-for="{ entry: item } in auditRows" :key="item.id || `${item.createdAt}-${item.operation}`" class="audit-item">
<div>
<strong>{{ operationLabel(item.operation) }}</strong>
<span>{{ sectionLabel(item.section) }} · {{ item.actorId || '系统' }}</span>
<span>{{ sectionLabel(item.section) }} · {{ item.actorName || item.actorId || '系统' }}</span>
<details v-if="item.changes?.length" class="audit-changes">
<summary>{{ item.changes.length }} 个字段变更</summary>
<p v-for="change in item.changes" :key="change.field">
{{ change.field }}:{{ change.before }} → {{ change.after }}
</p>
</details>
</div>
<time>{{ formatTime(item.createdAt) }}</time>
</div>
@@ -1,5 +1,5 @@
import {EpMenu} from "vue-icons-plus/ep";
import { Lock, Connection, Setting, FolderOpened, Filter, User, Grid } from '@element-plus/icons-vue';
import { Lock, Connection, Setting, FolderOpened, Filter, User, Grid, Document } from '@element-plus/icons-vue';
import {computed, ref, onMounted, onUnmounted, defineAsyncComponent} from 'vue'
import SettingsPageStatus from '../SettingsPageStatus/SettingsPageStatus.vue'
@@ -25,6 +25,7 @@ const GroupSettings = lazySettings(() => import('../GroupSettings/GroupSettings.
const RuleSettings = lazySettings(() => import('../RuleSettings/RuleSettings.vue'))
const UserManagement = lazySettings(() => import('../UserManagement/UserManagement.vue'))
const PluginSettings = lazySettings(() => import('../PluginSettings/PluginSettings.vue'))
const LogSettings = lazySettings(() => import('../LogSettings/LogSettings.vue'))
export function usePage() {
const globalStatus = useGlobalStatusStore();
@@ -53,6 +54,7 @@ export function usePage() {
{ key: 'rule', label: lang.rule_setting, hint: '邮件处理规则', icon: Filter, component: RuleSettings, capability: 'user.read' },
{ key: 'users', label: lang.user_management, hint: '账号与权限管理', icon: User, component: UserManagement, capability: 'user.manage' },
{ key: 'plugins', label: lang.plugin_settings, hint: '扩展功能管理', icon: Grid, component: PluginSettings, capability: 'plugin.read' },
{ key: 'logs', label: '日志', hint: '操作、收发信与运行记录', icon: Document, component: LogSettings, capability: 'audit.read' },
].filter(item => globalStatus.hasCapability(item.capability) ||
(item.key === 'plugins' && globalStatus.hasCapability('config.read'))))
@@ -0,0 +1,94 @@
.log-settings {
height: 100%;
min-height: 0;
min-width: 0;
display: flex;
flex-direction: column;
gap: 14px;
padding: 18px;
box-sizing: border-box;
}
.log-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
flex-wrap: wrap;
gap: 8px;
}
.log-heading h2 {
margin: 0;
font-size: 20px;
}
.log-heading span,
.log-pagination span {
color: var(--el-text-color-secondary);
font-size: 12px;
}
.log-categories {
flex-wrap: wrap;
}
.log-filters {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 10px;
}
.log-filters :deep(.el-date-editor) {
flex: 2 1 290px;
min-width: 0;
width: auto;
}
.log-filters .el-input {
flex: 1 1 130px;
max-width: 190px;
}
.log-filters .el-select {
flex: 1;
min-width: 200px;
}
.log-content {
flex: 1;
min-height: 160px;
overflow: hidden;
border: 1px solid var(--el-border-color-lighter);
border-radius: 8px;
}
.runtime-lines {
height: 100%;
overflow: auto;
box-sizing: border-box;
margin: 0;
padding: 12px;
font: 12px/1.7 Consolas, monospace;
white-space: pre-wrap;
overflow-wrap: anywhere;
background: var(--el-fill-color-light);
}
.log-pagination {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 8px;
}
.log-detail-grid {
display: grid;
grid-template-columns: 110px minmax(0, 1fr);
gap: 12px;
}
.log-detail-grid dd {
margin: 0;
overflow-wrap: anywhere;
}
@@ -0,0 +1,114 @@
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import http from '@/utils/axios'
export function useLogs() {
const category = ref('config')
const loading = ref(false)
const error = ref('')
const items = ref([])
const files = ref([])
const selectedFile = ref('')
const lines = ref([])
const offset = ref(0)
const hasMore = ref(false)
const actorId = ref('')
const dateRange = ref([])
const detail = ref(null)
const pageSize = 50
let requestId = 0
let aborter
const categories = [
{ label: '配置审计', value: 'config' },
{ label: '操作记录', value: 'operation' },
{ label: '收发信记录', value: 'mail' },
{ label: '运行日志', value: 'runtime' }
]
const isRuntime = computed(() => category.value === 'runtime')
const formatTime = value => value ? new Date(value).toLocaleString('zh-CN', { hour12: false }) : '—'
const resultLabel = value => ({ success: '成功', failure: '失败', unknown: '结果未知', accepted: '已接收' }[value] || value)
const actionLabel = value => ({ update: '保存配置', rollback: '回滚配置', migration: '迁移配置' }[value] || value)
const displayActor = item => item.payload?.actorName || item.actorId || '系统'
const details = computed(() => detail.value?.payload || {})
const changes = computed(() => details.value.changes || [])
function requireData(result) {
if (result?.errorNo) throw new Error(result.errorMsg || '日志查询失败')
return result?.data || {}
}
async function load() {
const id = ++requestId
aborter?.abort()
aborter = new AbortController()
loading.value = true
error.value = ''
try {
const params = { kind: category.value, offset: offset.value, limit: pageSize }
if (isRuntime.value) {
const listing = requireData(await http.get('/api/v1/logs', {
params: { kind: 'runtime' }, signal: aborter.signal
}))
if (id !== requestId) return
files.value = listing.files || []
if (!files.value.some(file => file.name === selectedFile.value)) {
selectedFile.value = files.value[0]?.name || ''
}
if (!selectedFile.value) {
lines.value = []
hasMore.value = false
return
}
params.file = selectedFile.value
} else {
if (actorId.value.trim()) params.actorId = actorId.value.trim()
if (dateRange.value?.length === 2) {
params.from = new Date(dateRange.value[0]).toISOString()
params.to = new Date(dateRange.value[1]).toISOString()
}
}
const data = requireData(await http.get('/api/v1/logs', { params, signal: aborter.signal }))
if (id !== requestId) return
items.value = data.items || []
lines.value = data.lines || []
hasMore.value = Boolean(data.hasMore)
} catch (cause) {
if (id === requestId && cause?.code !== 'ERR_CANCELED') {
error.value = cause.message || '日志查询失败'
items.value = []
lines.value = []
hasMore.value = false
}
} finally {
if (id === requestId) loading.value = false
}
}
function search() {
offset.value = 0
load()
}
function next() {
offset.value += isRuntime.value ? lines.value.length : pageSize
load()
}
function previous() {
offset.value = Math.max(0, offset.value - pageSize)
load()
}
watch(category, () => {
detail.value = null
search()
})
onMounted(load)
onBeforeUnmount(() => {
++requestId
aborter?.abort()
})
return {
category, categories, loading, error, items, files, selectedFile, lines, offset,
hasMore, actorId, dateRange, detail, details, changes, isRuntime,
formatTime, resultLabel, actionLabel, displayActor, search, next, previous
}
}
@@ -0,0 +1,103 @@
<template>
<section class="log-settings">
<header class="log-heading">
<h2>日志</h2>
<span>保留最近 30 天 · 运行日志满 7 天压缩</span>
</header>
<el-radio-group v-model="category" class="log-categories" aria-label="日志分类">
<el-radio-button v-for="item in categories" :key="item.value" :value="item.value">
{{ item.label }}
</el-radio-button>
</el-radio-group>
<div class="log-filters">
<template v-if="isRuntime">
<el-select v-model="selectedFile" placeholder="选择日志文件" aria-label="日志文件" @change="search">
<el-option
v-for="file in files"
:key="file.name"
:value="file.name"
:label="`${file.name}${file.compressed ? '(已压缩)' : ''}`"
/>
</el-select>
</template>
<template v-else>
<el-date-picker
v-model="dateRange"
type="datetimerange"
start-placeholder="开始时间"
end-placeholder="结束时间"
format="YYYY-MM-DD HH:mm"
aria-label="日志时间范围"
/>
<el-input v-model="actorId" clearable placeholder="操作者编号" aria-label="操作者编号" @keyup.enter="search" />
</template>
<el-button type="primary" :loading="loading" @click="search">查询</el-button>
</div>
<el-alert v-if="error" :title="error" type="error" :closable="false" show-icon />
<div v-loading="loading" class="log-content">
<template v-if="isRuntime">
<pre v-if="lines.length" class="runtime-lines">{{ lines.join('\n') }}</pre>
<el-empty v-else description="暂无运行日志" />
</template>
<el-table v-else :data="items" height="100%" empty-text="暂无日志记录" @row-click="item => detail = item">
<el-table-column label="时间" min-width="165">
<template #default="{ row }">{{ formatTime(row.createdAt) }}</template>
</el-table-column>
<el-table-column label="操作" min-width="140">
<template #default="{ row }">{{ actionLabel(row.action) }}</template>
</el-table-column>
<el-table-column label="操作者" min-width="110">
<template #default="{ row }">{{ displayActor(row) }}</template>
</el-table-column>
<el-table-column label="结果" width="90">
<template #default="{ row }">
<el-tag :type="row.result === 'failure' ? 'danger' : 'info'" size="small">{{ resultLabel(row.result) }}</el-tag>
</template>
</el-table-column>
<el-table-column label="详情" width="72">
<template #default="{ row }"><el-button link type="primary" @click.stop="detail = row">查看</el-button></template>
</el-table-column>
</el-table>
</div>
<footer class="log-pagination">
<span>从第 {{ offset + 1 }} 条开始</span>
<el-button :disabled="loading || offset === 0" @click="previous">上一页</el-button>
<el-button :disabled="loading || !hasMore" @click="next">下一页</el-button>
</footer>
<el-dialog :model-value="Boolean(detail)" title="日志详情" width="min(760px, 92vw)" append-to-body @close="detail = null">
<template v-if="detail">
<p>{{ formatTime(detail.createdAt) }} · {{ displayActor(detail) }} · {{ actionLabel(detail.action) }}</p>
<el-table v-if="changes.length" :data="changes" max-height="440">
<el-table-column prop="field" label="变更字段" min-width="200" />
<el-table-column prop="before" label="修改前" min-width="150" />
<el-table-column prop="after" label="修改后" min-width="150" />
</el-table>
<p v-else-if="category === 'config'">
{{ detail.id.startsWith('legacy-') || !details.actorName ? '此记录未保存字段前后值,不补造历史详情。' : '此次保存没有业务字段变更。' }}
</p>
<dl v-else-if="category === 'mail'" class="log-detail-grid">
<dt>发件人</dt><dd>{{ details.sender || '—' }}</dd>
<dt>收件人数</dt><dd>{{ details.recipientCount }}</dd>
<dt>邮件大小</dt><dd>{{ details.bytes }} 字节</dd>
<dt>结果</dt><dd>{{ resultLabel(detail.result) }}</dd>
</dl>
<dl v-else class="log-detail-grid">
<dt>权限角色</dt><dd>{{ details.actorRole || '—' }}</dd>
<dt>结果代码</dt><dd>{{ details.errorCode ?? '—' }}</dd>
</dl>
</template>
</el-dialog>
</section>
</template>
<script setup>
import { useLogs } from './LogSettings.js'
const {
category, categories, loading, error, items, files, selectedFile, lines, offset,
hasMore, actorId, dateRange, detail, details, changes, isRuntime,
formatTime, resultLabel, actionLabel, displayActor, search, next, previous
} = useLogs()
</script>
<style scoped src="./LogSettings.css"></style>