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-docker"></i>Docker 节点</h3>
6 <p class="td-page-subtitle">管理独立宝塔 Docker 面板节点实例</p>
7 </div>
8 <div class="td-page-actions">
9 <t-button theme="default" variant="outline" @click="load">
10 <i class="mdi mdi-refresh"></i> 刷新
11 </t-button>
12 <t-button theme="primary" @click="$router.push('/docker/node/add')">
13 <i class="mdi mdi-plus"></i> 添加节点
14 </t-button>
15 </div>
16 </div>
17
18 <div class="td-table-wrap">
19 <t-table
20 row-key="id"
21 :data="rows"
22 :columns="columns"
23 :loading="loading"
24 table-layout="auto"
25 stripe
26 bordered
27 :max-height="560"
28 >
29 <template #panel="{ row }">
30 <span class="dk-mono">{{ row.btip }}:{{ row.btdk }} {{ row.ptl === 'true' ? '(HTTPS)' : '(HTTP)' }}</span>
31 </template>
32 <template #btmy="{ row }">
33 <t-tag v-if="row.btmy" size="small" theme="success" variant="light">已设置</t-tag>
34 <t-tag v-else size="small" theme="danger" variant="light">未设置</t-tag>
35 </template>
36 <template #ktmy="{ row }">
37 {{ row.ktmy ? '已设置' : '-' }}
38 </template>
39 <template #qk="{ row }">
40 <t-tag :theme="row.qk === 'true' ? 'success' : 'default'" variant="light" size="small">
41 {{ row.qk === 'true' ? '启用' : '禁用' }}
42 </t-tag>
43 </template>
44 <template #operate="{ row }">
45 <div class="td-row-actions">
46 <t-button theme="default" variant="outline" size="small" @click="openEdit(row)">编辑</t-button>
47 <t-button theme="danger" variant="outline" size="small" @click="del(row)">删除</t-button>
48 </div>
49 </template>
50 </t-table>
51 </div>
52
53 <!-- 节点容器查询 -->
54 <div class="td-set-card" style="margin-top: 16px">
55 <div class="td-set-card-hd">
56 <div class="td-set-icon"><i class="mdi mdi-docker"></i></div>
57 <div>
58 <h4>节点容器查询</h4>
59 <p>选择节点查看 Docker 安装状态与该节点全部容器</p>
60 </div>
61 </div>
62 <div class="td-set-card-bd">
63 <div class="td-toolbar" style="margin-bottom: 12px">
64 <t-select
65 v-model="queryNodeId"
66 placeholder="请选择节点"
67 style="width: 260px"
68 :loading="loadingOptions"
69 >
70 <t-option
71 v-for="n in optionNodes"
72 :key="n.id"
73 :value="n.id"
74 :label="`${n.name} (${n.btip})`"
75 />
76 </t-select>
77 <t-button theme="primary" :loading="querying" @click="queryNode">
78 <i class="mdi mdi-magnify"></i> 查询
79 </t-button>
80 </div>
81
82 <t-alert
83 v-if="nodeConfigText"
84 :theme="dockerInstalled ? 'success' : 'warning'"
85 :message="nodeConfigText"
86 style="margin-bottom: 12px"
87 />
88
89 <t-table
90 row-key="name"
91 :data="containerRows"
92 :columns="containerColumns"
93 :loading="querying"
94 table-layout="auto"
95 stripe
96 bordered
97 size="small"
98 >
99 <template #empty>
100 <div class="td-empty">
101 <i class="mdi mdi-information-outline"></i>请选择节点后查询
102 </div>
103 </template>
104 </t-table>
105 </div>
106 </div>
107
108 <!-- 编辑弹窗 -->
109 <t-dialog
110 v-model:visible="editVisible"
111 header="编辑节点"
112 :on-confirm="onEdit"
113 width="560px"
114 :confirm-btn="{ loading: saving }"
115 >
116 <div class="td-form" v-if="editForm">
117 <div class="td-form-row">
118 <label>节点名称 <span class="td-text-danger">*</span></label>
119 <t-input v-model="editForm.name" />
120 </div>
121 <div class="td-form-row">
122 <label>宝塔面板地址 <span class="td-text-danger">*</span></label>
123 <t-input v-model="editForm.btip" placeholder="IP 或域名" />
124 </div>
125 <div class="td-form-grid">
126 <div class="td-form-row">
127 <label>端口</label>
128 <t-input-number v-model="editForm.btdk" :min="1" :max="65535" />
129 </div>
130 <div class="td-form-row">
131 <label>HTTPS</label>
132 <t-select v-model="editForm.ptl">
133 <t-option value="false" label="否" />
134 <t-option value="true" label="是" />
135 </t-select>
136 </div>
137 </div>
138 <div class="td-form-row">
139 <label>宝塔接口密钥 <span class="td-text-danger">*</span></label>
140 <t-textarea v-model="editForm.btmy" :autosize="{ minRows: 2, maxRows: 3 }" />
141 </div>
142 <div class="td-form-row">
143 <label>调用密钥(外部 API 鉴权)</label>
144 <t-input v-model="editForm.ktmy" placeholder="留空则不校验调用密钥" />
145 </div>
146 <div class="td-form-row">
147 <label>二级验证密钥</label>
148 <t-input v-model="editForm.qmk" placeholder="与调用密钥组合 md5 校验" />
149 </div>
150 <div class="td-form-row">
151 <label>启用</label>
152 <t-select v-model="editForm.qk">
153 <t-option value="true" label="启用" />
154 <t-option value="false" label="禁用" />
155 </t-select>
156 </div>
157 </div>
158 </t-dialog>
159 </div>
160</template>
161
162<script setup>
163import { ref, reactive, computed, onMounted } from 'vue'
164import { MessagePlugin, DialogPlugin } from 'tdesign-vue-next'
165import {
166 listDockerNode, editDockerNode, delDockerNode,
167 dockerNodeConfig, dockerNodeContainers, dockerOptions,
168} from '@/admin/api/docker'
169
170const loading = ref(false)
171const saving = ref(false)
172const rows = ref([])
173
174const columns = [
175 { colKey: 'id', title: 'ID', width: 70 },
176 { colKey: 'name', title: '节点名称', minWidth: 140, ellipsis: true },
177 { colKey: 'panel', title: '面板地址', minWidth: 200 },
178 { colKey: 'btmy', title: '接口密钥', width: 100, align: 'center' },
179 { colKey: 'ktmy', title: '调用密钥', width: 100, align: 'center' },
180 { colKey: 'qk', title: '状态', width: 90, align: 'center' },
181 { colKey: 'date', title: '添加时间', width: 160 },
182 { colKey: 'operate', title: '操作', width: 140, fixed: 'right' },
183]
184
185// 容器查询
186const loadingOptions = ref(false)
187const optionNodes = ref([])
188const queryNodeId = ref('')
189const querying = ref(false)
190const nodeConfigText = ref('')
191const dockerInstalled = ref(false)
192const containerRows = ref([])
193const containerColumns = [
194 { colKey: 'name', title: '容器名', minWidth: 160 },
195 { colKey: 'image', title: '镜像', minWidth: 180, ellipsis: true },
196 { colKey: 'status', title: '状态', width: 100 },
197 { colKey: 'ports', title: '端口', minWidth: 140, ellipsis: true },
198 { colKey: 'time', title: '创建时间', minWidth: 170 },
199]
200
201// 编辑弹窗
202const editVisible = ref(false)
203const editForm = ref(null)
204
205async function load() {
206 loading.value = true
207 const r = await listDockerNode()
208 loading.value = false
209 if (r.ok && r.data) {
210 rows.value = r.data.data || []
211 optionNodes.value = rows.value
212 } else if (!r.ok) {
213 MessagePlugin.error(r.message || '加载失败')
214 }
215}
216
217async function loadOptions() {
218 loadingOptions.value = true
219 const r = await dockerOptions()
220 loadingOptions.value = false
221 if (r.ok && r.data) {
222 optionNodes.value = r.data.nodes || []
223 }
224}
225
226function openEdit(row) {
227 editForm.value = reactive({
228 id: row.id,
229 name: row.name || '',
230 btip: row.btip || '',
231 btdk: Number(row.btdk || 8888),
232 ptl: row.ptl === 'true' ? 'true' : 'false',
233 btmy: row.btmy || '',
234 ktmy: row.ktmy || '',
235 qmk: row.qmk || '',
236 qk: row.qk === 'true' ? 'true' : 'false',
237 })
238 editVisible.value = true
239}
240
241async function onEdit() {
242 if (!editForm.value.name || !editForm.value.btip || !editForm.value.btmy) {
243 MessagePlugin.warning('节点名、面板地址、接口密钥必填')
244 return
245 }
246 saving.value = true
247 const r = await editDockerNode({ ...editForm.value })
248 saving.value = false
249 if (r.ok) {
250 MessagePlugin.success(r.message || '编辑成功')
251 editVisible.value = false
252 load()
253 } else {
254 MessagePlugin.error(r.message || '编辑失败')
255 }
256}
257
258function del(row) {
259 const dlg = DialogPlugin.confirm({
260 header: '删除节点',
261 body: `确定删除 Docker 节点「${row.name || row.id}」吗?节点下有用户时无法删除。`,
262 theme: 'warning',
263 onConfirm: async () => {
264 const r = await delDockerNode(row.id)
265 if (r.ok) {
266 MessagePlugin.success('删除成功')
267 load()
268 } else {
269 MessagePlugin.error(r.message || '删除失败')
270 }
271 dlg.destroy()
272 },
273 })
274}
275
276function pickContainerName(x) {
277 const name = x.name || (x.Names && x.Names[0]) || ''
278 return String(name).replace(/^\//, '')
279}
280
281async function queryNode() {
282 if (!queryNodeId.value) {
283 MessagePlugin.warning('请先选择节点')
284 return
285 }
286 querying.value = true
287 nodeConfigText.value = ''
288 containerRows.value = []
289
290 const [cfg, containers] = await Promise.all([
291 dockerNodeConfig(queryNodeId.value),
292 dockerNodeContainers(queryNodeId.value),
293 ])
294
295 if (cfg.ok && cfg.data) {
296 const d = cfg.data.data && cfg.data.data.data ? cfg.data.data.data : (cfg.data.data || cfg.data)
297 const installed = d && (d.docker_installed || (d.service_status && d.service_status.docker))
298 dockerInstalled.value = !!installed
299 nodeConfigText.value = 'Docker ' + (installed ? '已安装' : '未安装或异常')
300 + (d && d.service_status ? ' · Compose: ' + (d.service_status.docker_compose ? '已安装' : '未安装') : '')
301 } else {
302 dockerInstalled.value = false
303 nodeConfigText.value = cfg.message || '配置检测失败'
304 }
305
306 if (containers.ok && containers.data) {
307 const bt = containers.data.data || containers.data
308 const list = bt.data || bt || []
309 containerRows.value = (Array.isArray(list) ? list : []).map((x) => ({
310 name: pickContainerName(x),
311 image: x.image || x.Image || '-',
312 status: x.status || x.State || '-',
313 ports: x.ports || x.Ports || '-',
314 time: x.time || x.Created || '-',
315 }))
316 } else if (!containers.ok) {
317 MessagePlugin.error(containers.message || '容器查询失败')
318 }
319
320 querying.value = false
321}
322
323onMounted(() => {
324 load()
325 loadOptions()
326})
327</script>
328
329<style scoped>
330.td-page-actions {
331 display: flex;
332 gap: 8px;
333}
334.td-form-grid {
335 display: grid;
336 grid-template-columns: 1fr 1fr;
337 gap: 0 16px;
338}
339.dk-mono {
340 font-family: Consolas, Monaco, monospace;
341 font-size: 12.5px;
342}
343.td-empty {
344 text-align: center;
345 padding: 32px 16px;
346 color: var(--td-text-color-secondary, #6b7280);
347}
348.td-empty i {
349 font-size: 32px;
350 display: block;
351 margin-bottom: 8px;
352 opacity: 0.5;
353}
354</style>