better-staridc-MNBT
1<template>
2 <div class="td-page">
3 <div class="td-page-head">
4 <div>
5 <h3 class="td-page-title"><i class="mdi mdi-file-document-outline"></i>操作日志</h3>
6 <p class="td-page-subtitle">查看、检索与清理系统操作日志</p>
7 </div>
8 </div>
9
10 <div class="td-table-wrap">
11 <div class="td-toolbar">
12 <t-select v-model="search.field" class="td-search-field">
13 <t-option value="czuser" label="操作用户" />
14 <t-option value="lx" label="操作类型" />
15 <t-option value="lr" label="操作内容" />
16 <t-option value="qk" label="操作情况" />
17 </t-select>
18 <t-select v-model="search.mode" class="td-search-mode">
19 <t-option value="like" label="模糊匹配" />
20 <t-option value="eq" label="精确匹配" />
21 </t-select>
22 <t-input
23 v-model="search.kw"
24 placeholder="输入关键字搜索"
25 clearable
26 @enter="doSearch"
27 class="td-search-kw"
28 />
29 <t-button theme="primary" @click="doSearch">
30 <i class="mdi mdi-magnify"></i> 搜索
31 </t-button>
32 <t-button theme="default" variant="outline" @click="resetSearch">
33 <i class="mdi mdi-refresh"></i> 重置
34 </t-button>
35 <div class="td-toolbar-spacer"></div>
36 <t-button theme="danger" variant="outline" @click="clearAll">
37 <i class="mdi mdi-broom"></i> 清空日志
38 </t-button>
39 </div>
40
41 <t-table
42 row-key="id"
43 :data="rows"
44 :columns="columns"
45 :loading="loading"
46 :pagination="pagination"
47 stripe
48 bordered
49 @page-change="onPageChange"
50 >
51 <template #lr="{ row }">
52 <span class="cell-lr" :title="row.lr">{{ row.lr }}</span>
53 </template>
54
55 <template #qk="{ row }">
56 <span class="td-chip td-chip-success" v-if="isSuccess(row.qk)">
57 <i class="mdi mdi-check-circle"></i> {{ row.qk }}
58 </span>
59 <span class="td-chip td-chip-danger" v-else-if="isFail(row.qk)">
60 <i class="mdi mdi-alert-circle"></i> {{ row.qk }}
61 </span>
62 <span class="td-chip td-chip-default" v-else>{{ row.qk || '-' }}</span>
63 </template>
64
65 <template #op="{ row }">
66 <div class="td-row-actions">
67 <t-button theme="danger" variant="text" size="small" @click="deleteRow(row)">
68 <i class="mdi mdi-delete"></i> 删除
69 </t-button>
70 </div>
71 </template>
72 </t-table>
73 </div>
74 </div>
75</template>
76
77<script setup>
78import { ref, reactive, onMounted } from 'vue'
79import { MessagePlugin, DialogPlugin } from 'tdesign-vue-next'
80import { listLog, deleteLog, clearLog } from '@/admin/api/log'
81
82const loading = ref(false)
83const rows = ref([])
84
85const pagination = reactive({
86 current: 1,
87 pageSize: 10,
88 total: 0,
89 showJumper: true,
90})
91
92const search = reactive({
93 field: 'czuser',
94 mode: 'like',
95 kw: '',
96})
97
98const columns = [
99 { colKey: 'id', title: 'ID', width: 70, sorter: true },
100 { colKey: 'czuser', title: '操作用户', width: 130, sorter: true },
101 { colKey: 'lx', title: '操作类型', width: 130, sorter: true },
102 { colKey: 'lr', title: '操作内容', minWidth: 260, col: 'lr' },
103 { colKey: 'qk', title: '操作情况', width: 130, sorter: true },
104 { colKey: 'ip', title: 'IP地址', width: 140 },
105 { colKey: 'date', title: '操作时间', width: 170, sorter: true },
106 { colKey: 'op', title: '操作', width: 90, fixed: 'right' },
107]
108
109function isSuccess(qk) {
110 if (qk == null) return false
111 const s = String(qk)
112 return s === '成功' || s === '1' || s.includes('成功')
113}
114
115function isFail(qk) {
116 if (qk == null) return false
117 const s = String(qk)
118 return s === '失败' || s === '0' || s.includes('失败') || s.includes('错误')
119}
120
121function buildWhere() {
122 if (!search.kw) return null
123 const w = {}
124 if (search.mode === 'eq') {
125 w[search.field] = search.kw
126 } else {
127 w[search.field] = { like: search.kw }
128 }
129 return w
130}
131
132async function load() {
133 loading.value = true
134 const params = {
135 page: pagination.current,
136 limit: pagination.pageSize,
137 }
138 const where = buildWhere()
139 if (where) params.where = where
140 const r = await listLog(params)
141 loading.value = false
142 if (r.ok && r.data) {
143 rows.value = r.data.rows || []
144 pagination.total = r.data.total || 0
145 }
146}
147
148function onPageChange(p) {
149 pagination.current = p.current
150 pagination.pageSize = p.pageSize
151 load()
152}
153
154function doSearch() {
155 pagination.current = 1
156 load()
157}
158
159function resetSearch() {
160 search.kw = ''
161 search.field = 'czuser'
162 search.mode = 'like'
163 pagination.current = 1
164 load()
165}
166
167function deleteRow(row) {
168 const dialog = DialogPlugin.confirm({
169 header: '删除日志',
170 body: `确定删除日志 #${row.id} 吗?`,
171 confirmBtn: { content: '删除', theme: 'danger' },
172 onConfirm: async () => {
173 const r = await deleteLog(row.id)
174 dialog.destroy()
175 if (r.ok) {
176 MessagePlugin.success('删除成功')
177 load()
178 }
179 },
180 onClose: () => dialog.destroy(),
181 })
182}
183
184function clearAll() {
185 const dialog = DialogPlugin.confirm({
186 header: '清空日志',
187 body: '将删除全部操作日志,此操作不可恢复。确定继续吗?',
188 confirmBtn: { content: '清空全部', theme: 'danger' },
189 onConfirm: async () => {
190 const r = await clearLog()
191 dialog.destroy()
192 if (r.ok) {
193 MessagePlugin.success('已清空')
194 pagination.current = 1
195 load()
196 }
197 },
198 onClose: () => dialog.destroy(),
199 })
200}
201
202onMounted(load)
203</script>
204
205<style scoped>
206.td-search-field {
207 min-width: 130px !important;
208 width: 130px !important;
209}
210.td-search-mode {
211 min-width: 120px !important;
212 width: 120px !important;
213}
214.td-search-kw {
215 min-width: 220px !important;
216 width: 220px !important;
217}
218.cell-lr {
219 display: inline-block;
220 max-width: 100%;
221 word-break: break-all;
222 font-size: 12px;
223 line-height: 1.5;
224 color: var(--td-text);
225}
226</style>