Skip to content

Commit 59c6670

Browse files
author
puhui999
committed
feat:新增公共操作日志详情组件
1 parent 26a0827 commit 59c6670

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

src/api/system/operatelog/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,31 @@ export type OperateLogVO = {
2323
resultData: string
2424
}
2525

26+
export type OperateLogV2VO = {
27+
id: number
28+
userNickname: string
29+
traceId: string
30+
userType: number
31+
userId: number
32+
module: string
33+
name: string
34+
bizId: number
35+
content: string
36+
extra: string
37+
requestMethod: string
38+
requestUrl: string
39+
userIp: string
40+
userAgent: string
41+
creator: string
42+
creatorName: string
43+
createTime: Date
44+
// 数据扩展-渲染时使用
45+
title: string // 操作标题(如果为空则取 name 值)
46+
colSize: number // 变更记录行数
47+
contentStrList: string[]
48+
tagsContentList: string[]
49+
}
50+
2651
// 查询操作日志列表
2752
export const getOperateLogPage = (params: PageParam) => {
2853
return request.get({ url: '/system/operate-log/page', params })

src/components/OperateLogV2/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import OperateLogV2 from './src/OperateLogV2.vue'
2+
3+
export { OperateLogV2 }
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<template>
2+
<div class="p-20px">
3+
<el-timeline>
4+
<el-timeline-item
5+
v-for="(log, index) in logDataList"
6+
:key="index"
7+
:timestamp="formatDate(log.createTime!)"
8+
placement="top"
9+
>
10+
<div class="el-timeline-right-content">
11+
<el-row>
12+
<el-col :span="24" class="mb-10px">
13+
=======================
14+
<el-tag class="mr-10px" type="success">{{ log.creatorName }}</el-tag>
15+
<span>{{ log.title }}</span>
16+
=======================
17+
</el-col>
18+
<!-- 先处理一下有几行-->
19+
<template v-for="colNum in log.colSize" :key="colNum + 'col'">
20+
<el-col :span="24" class="mb-10px">
21+
<!-- 处理每一行-->
22+
<template
23+
v-for="(tagVal, index2) in log.tagsContentList.slice(
24+
(colNum - 1) * 3,
25+
3 * colNum
26+
)"
27+
:key="index2"
28+
>
29+
<el-tag class="mx-10px"> {{ tagVal }}</el-tag>
30+
<span>{{ log.contentStrList[index2] }}</span>
31+
</template>
32+
</el-col>
33+
</template>
34+
</el-row>
35+
</div>
36+
<template #dot>
37+
<span :style="{ backgroundColor: getUserTypeColor(log.userType) }" class="dot-node-style">
38+
{{ getDictLabel(DICT_TYPE.USER_TYPE, log.userType)[0] }}
39+
</span>
40+
</template>
41+
</el-timeline-item>
42+
</el-timeline>
43+
</div>
44+
</template>
45+
46+
<script lang="ts" setup>
47+
import { OperateLogV2VO } from '@/api/system/operatelog'
48+
import { formatDate } from '@/utils/formatTime'
49+
import { DICT_TYPE, getDictLabel, getDictObj } from '@/utils/dict'
50+
import { ElTag } from 'element-plus'
51+
52+
const props = defineProps<{
53+
logList: OperateLogV2VO[] // 操作日志列表
54+
}>()
55+
defineOptions({ name: 'OperateLogV2' })
56+
57+
/** 获得 userType 颜色 */
58+
const getUserTypeColor = (type: number) => {
59+
const dict = getDictObj(DICT_TYPE.USER_TYPE, type)
60+
switch (dict?.colorType) {
61+
case 'success':
62+
return '#67C23A'
63+
case 'info':
64+
return '#909399'
65+
case 'warning':
66+
return '#E6A23C'
67+
case 'danger':
68+
return '#F56C6C'
69+
}
70+
return '#409EFF'
71+
}
72+
const logDataList = ref<OperateLogV2VO[]>([]) // 操作日志列表
73+
// 提取 tag 所需内容和位置
74+
const renderTags = (content: string) => {
75+
let newStr = unref(content).slice() // 去掉引用
76+
newStr = newStr.replaceAll('【】', '【空】').replaceAll('', '') // 处理掉分号 特殊:处理一下空的情况
77+
const regex = /【([^【】]+)】/g
78+
const fg = '|' // 原始位置替换符号
79+
let match: any[] | null
80+
let matchStr: string[] = []
81+
let oldStr: string[] = []
82+
while ((match = regex.exec(newStr)) !== null) {
83+
matchStr.push(match[1]) // 提取值
84+
oldStr.push(match[0]) // 原值
85+
}
86+
// 为什么重新循环不放在 while 中一起是因为替换重新赋值过后 match 值就不准确了
87+
oldStr.forEach((item) => {
88+
newStr = newStr.replace(item, fg)
89+
})
90+
return [newStr.split(fg), matchStr]
91+
}
92+
const initLog = () => {
93+
logDataList.value = props.logList.map((logItem) => {
94+
const keyValue = renderTags(logItem.content)
95+
// 挂载数据
96+
logItem.contentStrList = keyValue[0]
97+
if (keyValue[0][0] === '') {
98+
logItem.title = logItem.name
99+
} else {
100+
logItem.title = keyValue[0][0]
101+
logItem.contentStrList.splice(0, 1)
102+
}
103+
logItem.colSize = keyValue[0].length / 3 // 变更记录行数
104+
logItem.tagsContentList = keyValue[1]
105+
return logItem
106+
})
107+
}
108+
watch(
109+
() => props.logList.length,
110+
(newObj) => {
111+
if (newObj) {
112+
initLog()
113+
console.log(logDataList.value)
114+
}
115+
},
116+
{
117+
immediate: true,
118+
deep: true
119+
}
120+
)
121+
</script>
122+
123+
<style lang="scss" scoped>
124+
// 时间线样式调整
125+
:deep(.el-timeline) {
126+
margin: 10px 0 0 160px;
127+
128+
.el-timeline-item__wrapper {
129+
position: relative;
130+
top: -20px;
131+
132+
.el-timeline-item__timestamp {
133+
position: absolute !important;
134+
top: 10px;
135+
left: -150px;
136+
}
137+
}
138+
139+
.el-timeline-right-content {
140+
display: flex;
141+
align-items: center;
142+
min-height: 30px;
143+
padding: 10px;
144+
background-color: #fff;
145+
146+
&::before {
147+
position: absolute;
148+
top: 10px;
149+
left: 13px; /* 将伪元素水平居中 */
150+
border-color: transparent #fff transparent transparent; /* 尖角颜色,左侧朝向 */
151+
border-style: solid;
152+
border-width: 8px; /* 调整尖角大小 */
153+
content: ''; /* 必须设置 content 属性 */
154+
}
155+
}
156+
}
157+
158+
.dot-node-style {
159+
position: absolute;
160+
left: -5px;
161+
display: flex;
162+
width: 20px;
163+
height: 20px;
164+
font-size: 10px;
165+
color: #fff;
166+
border-radius: 50%;
167+
justify-content: center;
168+
align-items: center;
169+
}
170+
</style>

0 commit comments

Comments
 (0)