仰望星辰工作室

better-staridc-MNBT

better-staridc-MNBT/ templates/tdesign/spa/src/admin/views/node/NodeListView.vue 10.3 KB · 362 行 原始文件
Z zfhsh first commit 2 个月前
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-router-wireless"></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-input
13 v-model="keyword"
14 placeholder="搜索节点名称"
15 clearable
16 style="width: 200px"
17 @change="applyFilter"
18 >
19 <template #prefix-icon><i class="mdi mdi-magnify"></i></template>
20 </t-input>
21 <t-select v-model="statusFilter" style="width: 130px" @change="applyFilter">
22 <t-option value="" label="全部" />
23 <t-option value="enabled" label="已启用" />
24 <t-option value="disabled" label="已停用" />
25 </t-select>
26 <t-button theme="default" variant="outline" @click="load">
27 <i class="mdi mdi-refresh"></i> 刷新
28 </t-button>
29 <div class="td-toolbar-spacer"></div>
30 <t-button theme="primary" @click="openAdd">
31 <i class="mdi mdi-plus"></i> 新增节点
32 </t-button>
33 </div>
34
35 <t-table
36 row-key="id"
37 :data="filteredRows"
38 :columns="columns"
39 :loading="loading"
40 :pagination="pagination"
41 table-layout="auto"
42 stripe
43 bordered
44 @page-change="onPageChange"
45 >
46 <template #status="{ row }">
47 <span :class="row.status === 'enabled' ? 'td-chip td-chip-success' : 'td-chip td-chip-danger'">
48 {{ row.status === 'enabled' ? '已启用' : '已停用' }}
49 </span>
50 </template>
51 <template #created_at="{ row }">{{ fmtTime(row.created_at) }}</template>
52 <template #last_ping="{ row }">{{ fmtTime(row.last_ping) }}</template>
53 <template #operate="{ row }">
54 <div class="td-row-actions">
55 <t-button
56 v-if="row.status === 'enabled'"
57 theme="default"
58 variant="outline"
59 size="small"
60 @click="toggleStatus(row, false)"
61 title="停用"
62 >
63 <i class="mdi mdi-pause"></i>
64 </t-button>
65 <t-button
66 v-else
67 theme="default"
68 variant="outline"
69 size="small"
70 @click="toggleStatus(row, true)"
71 title="启用"
72 >
73 <i class="mdi mdi-play"></i>
74 </t-button>
75 <t-button
76 theme="default"
77 variant="outline"
78 size="small"
79 :loading="pingLoading[row.id]"
80 @click="ping(row)"
81 title="Ping 测试"
82 >
83 <i class="mdi mdi-pulse"></i>
84 </t-button>
85 <t-button
86 theme="default"
87 variant="outline"
88 size="small"
89 @click="viewConfig(row)"
90 title="查看配置"
91 >
92 <i class="mdi mdi-file-document-outline"></i>
93 </t-button>
94 <t-button
95 theme="danger"
96 variant="outline"
97 size="small"
98 @click="del(row)"
99 title="删除"
100 >
101 <i class="mdi mdi-delete"></i>
102 </t-button>
103 </div>
104 </template>
105 </t-table>
106 </div>
107
108 <t-dialog
109 v-model:visible="addVisible"
110 header="新增节点"
111 :on-confirm="onAdd"
112 width="500px"
113 :confirm-btn="{ loading: saving }"
114 >
115 <div class="td-form">
116 <div class="td-form-row">
117 <label>节点名称 <span class="td-text-danger">*</span></label>
118 <t-input v-model="addForm.name" />
119 </div>
120 <div class="td-form-row">
121 <label>所属宝塔</label>
122 <t-select v-model="addForm.bt_id" :loading="btLoading" clearable placeholder="可选">
123 <t-option
124 v-for="b in baotaList"
125 :key="b.id"
126 :value="b.id"
127 :label="`${b.btdh} (${b.btip})`"
128 />
129 </t-select>
130 </div>
131 <div class="td-form-row">
132 <label>Base URL</label>
133 <t-input v-model="addForm.base_url" placeholder="如 https://node.example.com" />
134 </div>
135 <div class="td-form-row">
136 <label>API Key</label>
137 <t-input v-model="addForm.api_key" />
138 </div>
139 </div>
140 </t-dialog>
141
142 <t-dialog v-model:visible="cfgVisible" header="节点配置" width="600px">
143 <t-loading :loading="cfgLoading" text="加载中…">
144 <pre v-if="cfgText" class="td-cfg-pre">{{ cfgText }}</pre>
145 <div v-else class="td-empty">
146 <i class="mdi mdi-information"></i>暂无配置
147 </div>
148 </t-loading>
149 <template #footer>
150 <t-button theme="default" @click="cfgVisible = false">关闭</t-button>
151 </template>
152 </t-dialog>
153 </div>
154</template>
155
156<script setup>
157import { ref, reactive, computed, onMounted } from 'vue'
158import { MessagePlugin, DialogPlugin } from 'tdesign-vue-next'
159import { listNode, addNode, deleteNode, setNodeStatus, nodeConfig, nodePing } from '@/admin/api/node'
160import { listBaota } from '@/admin/api/baota'
161
162const loading = ref(false)
163const saving = ref(false)
164const rows = ref([])
165const keyword = ref('')
166const statusFilter = ref('')
167const pingLoading = reactive({})
168const pagination = reactive({ current: 1, pageSize: 10, total: 0, showJumper: true })
169
170const addVisible = ref(false)
171const addForm = reactive({ name: '', bt_id: '', base_url: '', api_key: '' })
172const btLoading = ref(false)
173const baotaList = ref([])
174
175const cfgVisible = ref(false)
176const cfgLoading = ref(false)
177const cfgText = ref('')
178
179const columns = [
180 { colKey: 'id', title: 'ID', width: 70, sorter: true },
181 { colKey: 'name', title: '节点名称', width: 160, ellipsis: true },
182 { colKey: 'btdh', title: '所属宝塔', width: 140, ellipsis: true },
183 { colKey: 'btip', title: '节点IP', width: 140 },
184 { colKey: 'status', title: '状态', width: 100 },
185 { colKey: 'created_at', title: '创建时间', width: 160 },
186 { colKey: 'last_ping', title: '最后 ping', width: 160 },
187 { colKey: 'version', title: '版本', width: 100 },
188 { colKey: 'operate', title: '操作', width: 200, fixed: 'right' },
189]
190
191const filteredRows = computed(() => {
192 let arr = rows.value
193 if (keyword.value) {
194 const k = keyword.value.toLowerCase()
195 arr = arr.filter((r) => (r.name || '').toLowerCase().includes(k))
196 }
197 if (statusFilter.value) {
198 arr = arr.filter((r) => r.status === statusFilter.value)
199 }
200 return arr
201})
202
203function fmtTime(v) {
204 if (!v) return '-'
205 if (typeof v === 'number') {
206 if (v < 1e12) v *= 1000
207 return new Date(v).toLocaleString('zh-CN', { hour12: false })
208 }
209 const d = new Date(v)
210 if (isNaN(d.getTime())) return String(v)
211 return d.toLocaleString('zh-CN', { hour12: false })
212}
213
214async function load() {
215 loading.value = true
216 try {
217 const r = await listNode({ page: 1, limit: 500 })
218 if (r.ok && r.data) {
219 if (Array.isArray(r.data)) {
220 rows.value = r.data
221 pagination.total = r.data.length
222 } else if (Array.isArray(r.data.rows)) {
223 rows.value = r.data.rows
224 pagination.total = r.data.total || r.data.rows.length
225 } else {
226 rows.value = []
227 pagination.total = 0
228 }
229 } else {
230 rows.value = []
231 pagination.total = 0
232 if (!r.ok) MessagePlugin.error(r.message || '加载失败')
233 }
234 } catch (e) {
235 rows.value = []
236 pagination.total = 0
237 MessagePlugin.error('加载节点失败')
238 } finally {
239 loading.value = false
240 }
241}
242
243function applyFilter() {
244 pagination.current = 1
245}
246function onPageChange(p) {
247 pagination.current = p.current
248 pagination.pageSize = p.pageSize
249}
250
251async function loadBaota() {
252 btLoading.value = true
253 const r = await listBaota({ page: 1, limit: 200 })
254 btLoading.value = false
255 if (r.ok && r.data) {
256 baotaList.value = r.data.rows || []
257 }
258}
259
260function openAdd() {
261 addForm.name = ''
262 addForm.bt_id = ''
263 addForm.base_url = ''
264 addForm.api_key = ''
265 addVisible.value = true
266 if (!baotaList.value.length) loadBaota()
267}
268
269async function onAdd() {
270 if (!addForm.name) { MessagePlugin.warning('请输入节点名称'); return }
271 saving.value = true
272 const r = await addNode({
273 name: addForm.name,
274 bt_id: addForm.bt_id,
275 base_url: addForm.base_url,
276 api_key: addForm.api_key,
277 })
278 saving.value = false
279 if (r.ok) {
280 MessagePlugin.success('添加成功')
281 addVisible.value = false
282 load()
283 } else {
284 MessagePlugin.error(r.message || '添加失败')
285 }
286}
287
288async function toggleStatus(row, enabled) {
289 const r = await setNodeStatus(row.id, enabled)
290 if (r.ok) {
291 MessagePlugin.success(enabled ? '已启用' : '已停用')
292 load()
293 } else {
294 MessagePlugin.error(r.message || '操作失败')
295 }
296}
297
298async function ping(row) {
299 pingLoading[row.id] = true
300 MessagePlugin.info(`正在 ping ${row.name || row.id}…`)
301 const r = await nodePing(row.id)
302 pingLoading[row.id] = false
303 if (r.ok) {
304 MessagePlugin.success('Ping 任务已下发')
305 setTimeout(load, 1500)
306 } else {
307 MessagePlugin.error(r.message || 'Ping 失败')
308 }
309}
310
311async function viewConfig(row) {
312 cfgVisible.value = true
313 cfgLoading.value = true
314 cfgText.value = ''
315 const r = await nodeConfig(row.id)
316 cfgLoading.value = false
317 if (r.ok && r.data != null) {
318 if (typeof r.data === 'string') cfgText.value = r.data
319 else cfgText.value = JSON.stringify(r.data, null, 2)
320 } else {
321 cfgText.value = r.message || '加载失败'
322 }
323}
324
325function del(row) {
326 const dlg = DialogPlugin.confirm({
327 header: '删除节点',
328 body: `确定删除节点 #${row.id}(${row.name || ''})吗?`,
329 theme: 'warning',
330 onConfirm: async () => {
331 const r = await deleteNode(row.id)
332 if (r.ok) {
333 MessagePlugin.success('删除成功')
334 load()
335 } else {
336 MessagePlugin.error(r.message || '删除失败')
337 }
338 dlg.destroy()
339 },
340 })
341}
342
343onMounted(load)
344</script>
345
346<style scoped>
347.td-cfg-pre {
348 background: #f7f8fa;
349 border: 1px solid var(--td-border);
350 border-radius: var(--td-radius);
351 padding: 12px;
352 font-family: Consolas, Monaco, monospace;
353 font-size: 12px;
354 line-height: 1.6;
355 color: var(--td-text);
356 white-space: pre-wrap;
357 word-break: break-all;
358 max-height: 400px;
359 overflow: auto;
360 margin: 0;
361}
362</style>