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-shield-search"></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
13 v-model="filter.node_id"
14 placeholder="节点筛选"
15 clearable
16 style="width: 180px"
17 :loading="nodesLoading"
18 @change="reload"
19 >
20 <t-option v-for="n in nodes" :key="n.id" :value="n.id" :label="n.name || `#${n.id}`" />
21 </t-select>
22 <t-select v-model="filter.days" placeholder="时间" style="width: 130px" @change="reload">
23 <t-option :value="0" label="全部" />
24 <t-option :value="1" label="今天" />
25 <t-option :value="2" label="昨天" />
26 <t-option :value="7" label="最近7天" />
27 <t-option :value="30" label="最近30天" />
28 </t-select>
29 <t-select v-model="filter.status" placeholder="状态" style="width: 130px" @change="reload">
30 <t-option value="" label="全部" />
31 <t-option value="success" label="成功" />
32 <t-option value="failed" label="失败" />
33 <t-option value="matched" label="有命中" />
34 </t-select>
35 <t-button theme="primary" variant="outline" @click="openBatchScan">
36 <i class="mdi mdi-radar"></i> 批量扫描
37 </t-button>
38 <t-button theme="default" variant="outline" @click="openClear">
39 <i class="mdi mdi-broom"></i> 清理旧记录
40 </t-button>
41 <div class="td-toolbar-spacer"></div>
42 <t-button theme="default" variant="outline" @click="reload">
43 <i class="mdi mdi-refresh"></i> 刷新
44 </t-button>
45 </div>
46
47 <t-tabs v-model="tab" class="td-scan-tabs">
48 <t-tab-panel value="scan" label="扫描记录">
49 <t-table
50 row-key="id"
51 :data="scanRows"
52 :columns="scanColumns"
53 :loading="scanLoading"
54 :pagination="scanPagination"
55 table-layout="auto"
56 stripe
57 bordered
58 @page-change="onScanPage"
59 >
60 <template #status="{ row }">
61 <span :class="statusClass(row)">{{ statusText(row) }}</span>
62 </template>
63 <template #started_at="{ row }">{{ fmtTime(row.started_at) }}</template>
64 <template #finished_at="{ row }">{{ fmtTime(row.finished_at) }}</template>
65 </t-table>
66 </t-tab-panel>
67
68 <t-tab-panel value="match" label="命中记录">
69 <t-table
70 row-key="id"
71 :data="matchRows"
72 :columns="matchColumns"
73 :loading="matchLoading"
74 :pagination="matchPagination"
75 table-layout="auto"
76 stripe
77 bordered
78 @page-change="onMatchPage"
79 >
80 <template #created_at="{ row }">{{ fmtTime(row.created_at) }}</template>
81 <template #operate="{ row }">
82 <t-button theme="default" variant="outline" size="small" @click="viewMatch(row)">
83 <i class="mdi mdi-eye"></i> 详情
84 </t-button>
85 </template>
86 </t-table>
87 </t-tab-panel>
88 </t-tabs>
89 </div>
90
91 <t-dialog
92 v-model:visible="batchVisible"
93 header="批量扫描"
94 :on-confirm="doBatchScan"
95 width="500px"
96 :confirm-btn="{ loading: scanBusy }"
97 >
98 <div class="td-form">
99 <div class="td-form-row">
100 <label>选择节点 <span class="td-text-danger">*</span></label>
101 <t-select v-model="batchForm.node_id" placeholder="选择节点" :loading="nodesLoading">
102 <t-option v-for="n in nodes" :key="n.id" :value="n.id" :label="n.name || `#${n.id}`" />
103 </t-select>
104 <div class="td-form-hint">将向该节点下发违禁词扫描任务</div>
105 </div>
106 </div>
107 </t-dialog>
108
109 <t-dialog
110 v-model:visible="clearVisible"
111 header="清理旧扫描记录"
112 :on-confirm="doClear"
113 width="450px"
114 :confirm-btn="{ loading: clearBusy }"
115 >
116 <div class="td-form">
117 <div class="td-form-row">
118 <label>清理多少天前的记录</label>
119 <t-input-number v-model="clearDays" :min="1" theme="normal" />
120 <div class="td-form-hint">将删除该天数之前的所有扫描与命中记录</div>
121 </div>
122 </div>
123 </t-dialog>
124
125 <t-dialog v-model:visible="matchVisible" header="命中详情" width="600px">
126 <div v-if="currentMatch" class="td-form">
127 <div class="td-form-row">
128 <label>文件路径</label>
129 <div class="td-code-block">{{ currentMatch.file_path || '-' }}</div>
130 </div>
131 <div class="td-form-row">
132 <label>命中关键词</label>
133 <div class="td-code-block">{{ currentMatch.matched_keyword || '-' }}</div>
134 </div>
135 <div class="td-form-row">
136 <label>命中行内容</label>
137 <pre class="td-match-pre">{{ currentMatch.matched_line || '-' }}</pre>
138 </div>
139 </div>
140 <template #footer>
141 <t-button theme="default" @click="matchVisible = false">关闭</t-button>
142 </template>
143 </t-dialog>
144 </div>
145</template>
146
147<script setup>
148import { ref, reactive, onMounted, watch } from 'vue'
149import { MessagePlugin } from 'tdesign-vue-next'
150import {
151 listNode, listForbiddenScan, listForbiddenMatch,
152 nodeForbiddenScan, clearOldScans,
153} from '@/admin/api/node'
154
155const tab = ref('scan')
156const nodes = ref([])
157const nodesLoading = ref(false)
158
159const filter = reactive({ node_id: '', days: 0, status: '' })
160
161const scanRows = ref([])
162const scanLoading = ref(false)
163const scanPagination = reactive({ current: 1, pageSize: 10, total: 0, showJumper: true })
164const matchRows = ref([])
165const matchLoading = ref(false)
166const matchPagination = reactive({ current: 1, pageSize: 10, total: 0, showJumper: true })
167
168const batchVisible = ref(false)
169const batchForm = reactive({ node_id: '' })
170const scanBusy = ref(false)
171
172const clearVisible = ref(false)
173const clearDays = ref(30)
174const clearBusy = ref(false)
175
176const matchVisible = ref(false)
177const currentMatch = ref(null)
178
179const scanColumns = [
180 { colKey: 'id', title: 'ID', width: 70 },
181 { colKey: 'node_id', title: '节点ID', width: 90 },
182 { colKey: 'node_name', title: '节点名称', width: 140, ellipsis: true },
183 { colKey: 'status', title: '状态', width: 100 },
184 { colKey: 'started_at', title: '开始时间', width: 160 },
185 { colKey: 'finished_at', title: '完成时间', width: 160 },
186 { colKey: 'scanned_count', title: '扫描文件', width: 100 },
187 { colKey: 'matched_count', title: '命中数', width: 100 },
188]
189const matchColumns = [
190 { colKey: 'id', title: 'ID', width: 70 },
191 { colKey: 'scan_id', title: '扫描ID', width: 90 },
192 { colKey: 'node_id', title: '节点ID', width: 90 },
193 { colKey: 'file_path', title: '文件路径', width: 260, ellipsis: true },
194 { colKey: 'matched_keyword', title: '关键词', width: 140, ellipsis: true },
195 { colKey: 'created_at', title: '命中时间', width: 160 },
196 { colKey: 'operate', title: '操作', width: 110, fixed: 'right' },
197]
198
199function fmtTime(v) {
200 if (!v) return '-'
201 if (typeof v === 'number') {
202 if (v < 1e12) v *= 1000
203 return new Date(v).toLocaleString('zh-CN', { hour12: false })
204 }
205 const d = new Date(v)
206 if (isNaN(d.getTime())) return String(v)
207 return d.toLocaleString('zh-CN', { hour12: false })
208}
209
210function statusClass(row) {
211 if (row.status === 'success' && Number(row.matched_count) > 0) return 'td-chip td-chip-warning'
212 if (row.status === 'success') return 'td-chip td-chip-success'
213 if (row.status === 'failed') return 'td-chip td-chip-danger'
214 return 'td-chip td-chip-default'
215}
216function statusText(row) {
217 if (row.status === 'success' && Number(row.matched_count) > 0) return '有命中'
218 if (row.status === 'success') return '成功'
219 if (row.status === 'failed') return '失败'
220 return row.status || '-'
221}
222
223async function loadNodes() {
224 nodesLoading.value = true
225 try {
226 const r = await listNode({ page: 1, limit: 200 })
227 if (r.ok && r.data) {
228 nodes.value = Array.isArray(r.data) ? r.data : (r.data.rows || [])
229 } else {
230 nodes.value = []
231 }
232 } catch (e) {
233 nodes.value = []
234 } finally {
235 nodesLoading.value = false
236 }
237}
238
239function buildParams(pag) {
240 const params = { page: pag.current, limit: pag.pageSize }
241 if (filter.node_id !== '' && filter.node_id != null) params.node_id = filter.node_id
242 if (filter.status) params.status = filter.status
243 if (filter.days > 0) params.days = filter.days
244 return params
245}
246
247async function loadScan() {
248 scanLoading.value = true
249 try {
250 const r = await listForbiddenScan(buildParams(scanPagination))
251 if (r.ok && r.data) {
252 scanRows.value = Array.isArray(r.data) ? r.data : (r.data.rows || [])
253 scanPagination.total = (r.data && r.data.total != null) ? r.data.total : scanRows.value.length
254 } else {
255 scanRows.value = []
256 scanPagination.total = 0
257 if (!r.ok) MessagePlugin.error(r.message || '加载失败')
258 }
259 } catch (e) {
260 scanRows.value = []
261 scanPagination.total = 0
262 MessagePlugin.error('加载扫描记录失败')
263 } finally {
264 scanLoading.value = false
265 }
266}
267
268async function loadMatch() {
269 matchLoading.value = true
270 try {
271 const r = await listForbiddenMatch(buildParams(matchPagination))
272 if (r.ok && r.data) {
273 matchRows.value = Array.isArray(r.data) ? r.data : (r.data.rows || [])
274 matchPagination.total = (r.data && r.data.total != null) ? r.data.total : matchRows.value.length
275 } else {
276 matchRows.value = []
277 matchPagination.total = 0
278 }
279 } catch (e) {
280 matchRows.value = []
281 matchPagination.total = 0
282 MessagePlugin.error('加载命中记录失败')
283 } finally {
284 matchLoading.value = false
285 }
286}
287
288function reload() {
289 scanPagination.current = 1
290 matchPagination.current = 1
291 loadScan()
292 loadMatch()
293}
294
295function onScanPage(p) {
296 scanPagination.current = p.current
297 scanPagination.pageSize = p.pageSize
298 loadScan()
299}
300function onMatchPage(p) {
301 matchPagination.current = p.current
302 matchPagination.pageSize = p.pageSize
303 loadMatch()
304}
305
306watch(tab, (v) => {
307 if (v === 'scan' && !scanRows.value.length) loadScan()
308 if (v === 'match' && !matchRows.value.length) loadMatch()
309})
310
311function openBatchScan() {
312 batchForm.node_id = ''
313 batchVisible.value = true
314 if (!nodes.value.length) loadNodes()
315}
316
317async function doBatchScan() {
318 if (!batchForm.node_id) { MessagePlugin.warning('请选择节点'); return }
319 scanBusy.value = true
320 const r = await nodeForbiddenScan({ node_id: batchForm.node_id })
321 scanBusy.value = false
322 if (r.ok) {
323 MessagePlugin.success('扫描任务已下发')
324 batchVisible.value = false
325 setTimeout(loadScan, 1500)
326 } else {
327 MessagePlugin.error(r.message || '下发失败')
328 }
329}
330
331function openClear() {
332 clearDays.value = 30
333 clearVisible.value = true
334}
335
336async function doClear() {
337 if (!clearDays.value || clearDays.value < 1) { MessagePlugin.warning('请输入有效天数'); return }
338 clearBusy.value = true
339 const r = await clearOldScans(clearDays.value)
340 clearBusy.value = false
341 if (r.ok) {
342 MessagePlugin.success('清理完成')
343 clearVisible.value = false
344 reload()
345 } else {
346 MessagePlugin.error(r.message || '清理失败')
347 }
348}
349
350function viewMatch(row) {
351 currentMatch.value = row
352 matchVisible.value = true
353}
354
355onMounted(() => {
356 loadNodes()
357 loadScan()
358})
359</script>
360
361<style scoped>
362.td-scan-tabs {
363 padding: 0 12px;
364}
365.td-match-pre {
366 background: #f7f8fa;
367 border: 1px solid var(--td-border);
368 border-radius: var(--td-radius);
369 padding: 10px 12px;
370 font-family: Consolas, Monaco, monospace;
371 font-size: 12px;
372 line-height: 1.6;
373 color: var(--td-text);
374 white-space: pre-wrap;
375 word-break: break-all;
376 max-height: 240px;
377 overflow: auto;
378 margin: 0;
379}
380</style>