2026-07-25 18:14:50 +08:00

228 lines
5.5 KiB
Vue
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.

<!--
LogPanel — 操作日志面板(通用)
支持两种数据来源:
- apiMethod 传入 → 调用真实接口获取日志
- 无 apiMethod → 基于 row 生成模拟日志(开发调试用)
-->
<template>
<div class="log-panel">
<div class="section-title">操作日志</div>
<div v-if="apiLoading" v-loading="true" class="empty-tip">加载中…</div>
<div v-else-if="!logs.length" class="empty-tip">暂无操作日志</div>
<div v-else class="log-timeline">
<div v-for="(log, i) in logs" :key="i" class="log-item">
<div class="log-dot" :class="log.level" />
<div class="log-content">
<div class="log-header">
<span class="log-operator">{{ log.operator }}</span>
<span class="log-action">{{ log.action }}</span>
<el-tag v-if="log.result === '成功'" size="small" type="success">成功</el-tag>
<el-tag v-else-if="log.result === '失败'" size="small" type="danger">失败</el-tag>
<el-tag v-else size="small" type="info">{{ log.result }}</el-tag>
</div>
<div v-if="log.detail" class="log-detail">{{ log.detail }}</div>
<div class="log-time">{{ log.time }}</div>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { computed, ref, watch } from 'vue'
defineOptions({ name: 'LogPanel' })
interface LogItem {
operator: string
action: string
result: string
detail?: string
time: string
level: 'success' | 'info' | 'warning' | 'error'
}
const props = defineProps<{
/** 当前选中行数据(用于生成关联日志) */
row: Record<string, any> | null
/** 可选的日志 API 方法,传入后自动调用获取真实日志 */
apiMethod?: (...args: any[]) => Promise<any>
}>()
/** 通过 API 获取的真实日志列表 */
const apiLogs = ref<LogItem[] | null>(null)
const apiLoading = ref(false)
watch(() => props.row, async (newRow) => {
if (newRow && props.apiMethod) {
apiLoading.value = true
try {
const res = await props.apiMethod({
id: newRow.id,
code: newRow.code,
page: 1,
pageSize: 100
})
if (res && (res.code === 1 || res.code === 200)) {
const list = res.data?.list ?? res.data?.logs ?? res.list ?? res.logs ?? []
apiLogs.value = Array.isArray(list) ? list.map((item: any) => ({
operator: item.operator ?? item.creator ?? '—',
action: item.action ?? item.operation ?? '—',
result: item.result ?? item.status ?? '—',
detail: item.detail ?? item.description ?? '',
time: item.time ?? item.create_time ?? '—',
level: (item.result === '成功' || item.status === 'success') ? 'success'
: (item.result === '失败' || item.status === 'error') ? 'error'
: 'info'
})) : []
} else {
apiLogs.value = []
}
} catch {
apiLogs.value = []
} finally {
apiLoading.value = false
}
} else {
apiLogs.value = null
}
}, { immediate: true })
/** 基于 row 生成模拟日志(无 apiMethod 时使用) */
const mockLogs = computed<LogItem[]>(() => {
if (!props.row) return []
const rowName = props.row.name ?? props.row.code ?? '—'
const now = new Date()
const pad = (n: number) => String(n).padStart(2, '0')
const fmt = (d: Date) =>
`${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`
return [
{
operator: '系统',
action: `创建了「${rowName}」`,
result: '成功',
time: fmt(new Date(now.getTime() - 3600000 * 3)),
level: 'success'
},
{
operator: props.row.creator ?? '管理员',
action: `更新了「${rowName}」`,
result: '成功',
detail: `修改了状态和相关配置`,
time: fmt(new Date(now.getTime() - 3600000)),
level: 'success'
}
]
})
/** 最终展示的日志:优先使用 API 日志,否则使用 mock 日志 */
const logs = computed<LogItem[]>(() => {
if (apiLogs.value !== null) {
return apiLogs.value
}
return mockLogs.value
})
</script>
<style lang="scss" scoped>
.log-panel {
padding: 8px 0;
}
.empty-tip {
text-align: center;
font-size: 13px;
color: #909399;
padding: 40px 0;
}
.section-title {
font-size: 14px;
font-weight: 600;
color: #303133;
margin-bottom: 12px;
padding-left: 4px;
border-left: 3px solid #409eff;
}
.log-timeline {
padding-left: 8px;
.log-item {
display: flex;
gap: 12px;
margin-bottom: 12px;
padding-bottom: 8px;
border-bottom: 1px dashed #ebeef5;
&:last-child {
border-bottom: none;
margin-bottom: 0;
}
}
.log-dot {
flex-shrink: 0;
width: 8px;
height: 8px;
margin-top: 6px;
border-radius: 50%;
background: #c0c4cc;
&.success {
background: #67c23a;
}
&.warning {
background: #e6a23c;
}
&.error {
background: #f56c6c;
}
&.info {
background: #409eff;
}
}
.log-content {
flex: 1;
min-width: 0;
}
.log-header {
display: flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
}
.log-operator {
font-weight: 600;
font-size: 13px;
color: #303133;
}
.log-action {
font-size: 13px;
color: #606266;
}
.log-detail {
margin-top: 4px;
font-size: 12px;
color: #909399;
}
.log-time {
margin-top: 2px;
font-size: 12px;
color: #c0c4cc;
}
}
</style>