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-server"></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-button theme="primary" @click="$router.push('/baota/add')">
13 <i class="mdi mdi-plus"></i> 新增宝塔
14 </t-button>
15 <div class="td-toolbar-spacer"></div>
16 <t-button
17 theme="danger"
18 variant="outline"
19 :disabled="!selectedIds.length"
20 @click="batchDelete"
21 >
22 <i class="mdi mdi-delete"></i> 删除选中
23 </t-button>
24 </div>
25
26 <t-table
27 row-key="id"
28 :data="rows"
29 :columns="columns"
30 :loading="loading"
31 :pagination="pagination"
32 table-layout="auto"
33 stripe
34 bordered
35 @page-change="onPageChange"
36 @select-change="onSelectChange"
37 >
38 <template #status="{ row }">
39 <t-button
40 v-if="connectStatus[row.id] === undefined"
41 theme="default"
42 variant="outline"
43 size="small"
44 :loading="connectLoading[row.id]"
45 @click="checkConnect(row)"
46 >点我检测</t-button>
47 <span v-else-if="connectStatus[row.id]" class="td-chip td-chip-success">通信正常</span>
48 <span v-else class="td-chip td-chip-danger">通信失败</span>
49 </template>
50 <template #ktmy="{ row }">
51 <div class="td-flex-center td-gap-8">
52 <span class="td-mono">{{ showKtmy[row.id] ? (row.dymy || '-') : '********' }}</span>
53 <t-link theme="primary" @click="toggleKtmy(row.id)">
54 <i class="mdi" :class="showKtmy[row.id] ? 'mdi-eye-off' : 'mdi-eye'"></i>
55 </t-link>
56 <t-link v-if="showKtmy[row.id]" theme="default" @click="copyDymy(row)" title="复制调用密钥">
57 <i class="mdi mdi-content-copy"></i>
58 </t-link>
59 </div>
60 </template>
61 <template #btmy="{ row }">
62 <div class="td-flex-center td-gap-8">
63 <span class="td-mono">{{ showBtmy[row.id] ? (row.btmy || '-') : '********' }}</span>
64 <t-link theme="primary" @click="toggleBtmy(row.id)">
65 <i class="mdi" :class="showBtmy[row.id] ? 'mdi-eye-off' : 'mdi-eye'"></i>
66 </t-link>
67 </div>
68 </template>
69 <template #qk="{ row }">
70 <span :class="isOn(row.qk) ? 'td-chip td-chip-success' : 'td-chip td-chip-danger'">
71 {{ isOn(row.qk) ? '开启' : '关闭' }}
72 </span>
73 </template>
74 <template #date="{ row }">{{ fmtTime(row.date) }}</template>
75 <template #operate="{ row }">
76 <div class="td-row-actions">
77 <t-button theme="default" variant="outline" size="small" @click="edit(row)" title="编辑">
78 <i class="mdi mdi-pencil"></i>
79 </t-button>
80 <t-button theme="default" variant="outline" size="small" @click="openTutorial(row)" title="魔方对接文档">
81 <i class="mdi mdi-book-open-variant"></i>
82 </t-button>
83 <t-button theme="default" variant="outline" size="small" @click="openPhpList(row)" title="PHP 版本">
84 <i class="mdi mdi-language-php"></i>
85 </t-button>
86 <t-button theme="danger" variant="outline" size="small" @click="del(row)" title="删除">
87 <i class="mdi mdi-delete"></i>
88 </t-button>
89 </div>
90 </template>
91 </t-table>
92 </div>
93
94 <t-dialog
95 v-model:visible="editVisible"
96 header="编辑宝塔"
97 :on-confirm="onSave"
98 width="600px"
99 :confirm-btn="{ loading: saving }"
100 >
101 <div class="td-form">
102 <div class="td-form-row">
103 <label>宝塔 IP</label>
104 <t-input v-model="editForm.ip" />
105 </div>
106 <div class="td-form-row">
107 <label>宝塔端口</label>
108 <t-input v-model="editForm.dk" />
109 </div>
110 <div class="td-form-row">
111 <label>宝塔密钥</label>
112 <t-textarea v-model="editForm.key" :autosize="{ minRows: 2, maxRows: 4 }" />
113 </div>
114 <div class="td-form-row">
115 <label>操作系统</label>
116 <t-select v-model="editForm.btos">
117 <t-option :value="1" label="Linux" />
118 <t-option :value="2" label="Windows" />
119 </t-select>
120 </div>
121 <div class="td-form-row">
122 <label>域名解析说明</label>
123 <t-textarea v-model="editForm.urlla" :autosize="{ minRows: 2, maxRows: 4 }" />
124 </div>
125 <div class="td-form-row">
126 <label>FTP 地址</label>
127 <t-input v-model="editForm.ftpdz" />
128 </div>
129 <div class="td-form-switch">
130 <div class="td-form-switch-txt">
131 <strong>安全访问 HTTPS</strong>
132 <span>开启后通过 HTTPS 访问宝塔</span>
133 </div>
134 <t-switch v-model="editForm.xieyi" />
135 </div>
136 <div class="td-form-switch">
137 <div class="td-form-switch-txt">
138 <strong>宝塔接口开关</strong>
139 <span>关闭后无法调用宝塔</span>
140 </div>
141 <t-switch v-model="editForm.kg" />
142 </div>
143 </div>
144 </t-dialog>
145
146 <t-dialog v-model:visible="phpVisible" header="PHP 版本" width="520px">
147 <t-loading :loading="phpLoading" text="加载中…">
148 <div v-if="phpCurrent" class="td-php-current">
149 当前默认:<span class="td-chip td-chip-info">{{ phpCurrent }}</span>
150 </div>
151 <div v-if="phpList.length">
152 <t-radio-group v-model="phpSelected" class="td-php-list">
153 <t-radio v-for="v in phpList" :key="v.version" :value="v.version">
154 {{ v.name }} <span class="td-text-mute">({{ v.version }})</span>
155 </t-radio>
156 </t-radio-group>
157 </div>
158 <div v-else class="td-empty">
159 <i class="mdi mdi-information"></i>暂无 PHP 版本
160 </div>
161 </t-loading>
162 <template #footer>
163 <t-button theme="primary" :loading="autoLoading" @click="setPhp">
164 设为默认
165 </t-button>
166 <t-button theme="default" variant="outline" :loading="autoLoading" @click="autoSetPhp">
167 自动设置最新 PHP
168 </t-button>
169 <t-button theme="default" variant="outline" @click="phpVisible = false">关闭</t-button>
170 </template>
171 </t-dialog>
172 </div>
173</template>
174
175<script setup>
176import { ref, reactive, onMounted } from 'vue'
177import { useRouter } from 'vue-router'
178import { MessagePlugin, DialogPlugin } from 'tdesign-vue-next'
179import {
180 listBaota, deleteBaota, updateBaota, checkBaotaConnect,
181 listNodePhp, autoDetectNodePhp, setNodePhp,
182} from '@/admin/api/baota'
183
184const router = useRouter()
185const loading = ref(false)
186const saving = ref(false)
187const rows = ref([])
188const selectedIds = ref([])
189const showKtmy = reactive({})
190const showBtmy = reactive({})
191const connectStatus = reactive({})
192const connectLoading = reactive({})
193const pagination = reactive({ current: 1, pageSize: 10, total: 0, showJumper: true })
194
195const editVisible = ref(false)
196const editForm = reactive({
197 id: null, ip: '', dk: '', key: '', btos: 1,
198 urlla: '', ftpdz: '', xieyi: false, kg: true,
199})
200
201const phpVisible = ref(false)
202const phpList = ref([])
203const phpLoading = ref(false)
204const autoLoading = ref(false)
205const currentBtdh = ref('')
206const phpCurrent = ref('')
207const phpSelected = ref('')
208
209const columns = [
210 { colKey: 'row-select', type: 'multiple', width: 50 },
211 { colKey: 'id', title: 'ID', width: 70, sorter: true },
212 { colKey: 'status', title: '通信状态', width: 110 },
213 { colKey: 'btdh', title: '宝塔编号', width: 140, ellipsis: true },
214 { colKey: 'btip', title: '宝塔IP', width: 140 },
215 { colKey: 'ktmy', title: '调用密钥', width: 180 },
216 { colKey: 'date', title: '添加时间', width: 150 },
217 { colKey: 'btdk', title: '端口', width: 80 },
218 { colKey: 'btmy', title: '宝塔密钥', width: 180 },
219 { colKey: 'qk', title: '状态', width: 90 },
220 { colKey: 'operate', title: '操作', width: 180, fixed: 'right' },
221]
222
223function isOn(qk) {
224 return qk === true || qk === 'true'
225}
226
227function fmtTime(v) {
228 if (!v) return '-'
229 const d = new Date(v)
230 if (isNaN(d.getTime())) return String(v)
231 return d.toLocaleString('zh-CN', { hour12: false })
232}
233
234function toggleKtmy(id) {
235 showKtmy[id] = !showKtmy[id]
236}
237function copyDymy(row) {
238 const val = row.dymy || ''
239 navigator.clipboard.writeText(val).then(() => {
240 MessagePlugin.success('调用密钥已复制')
241 }).catch(() => {
242 const ta = document.createElement('textarea')
243 ta.value = val
244 ta.style.position = 'fixed'; ta.style.left = '-9999px'
245 document.body.appendChild(ta)
246 ta.select(); document.execCommand('copy')
247 document.body.removeChild(ta)
248 MessagePlugin.success('调用密钥已复制')
249 })
250}
251function toggleBtmy(id) {
252 showBtmy[id] = !showBtmy[id]
253}
254
255async function load() {
256 loading.value = true
257 const r = await listBaota({ page: pagination.current, limit: pagination.pageSize })
258 loading.value = false
259 if (r.ok && r.data) {
260 rows.value = r.data.rows || []
261 pagination.total = r.data.total || 0
262 } else {
263 if (!r.ok) MessagePlugin.error(r.message || '加载失败')
264 rows.value = []
265 pagination.total = 0
266 }
267}
268
269function onPageChange(p) {
270 pagination.current = p.current
271 pagination.pageSize = p.pageSize
272 load()
273}
274function onSelectChange(keys) {
275 selectedIds.value = keys
276}
277
278async function checkConnect(row) {
279 connectLoading[row.id] = true
280 const r = await checkBaotaConnect(row.id)
281 connectLoading[row.id] = false
282 connectStatus[row.id] = !!r.ok
283 if (!r.ok) MessagePlugin.warning(r.message || '通信失败')
284}
285
286function edit(row) {
287 editForm.id = row.id
288 editForm.ip = row.btip || ''
289 editForm.dk = row.btdk || ''
290 editForm.key = row.btmy || ''
291 editForm.btos = Number(row.btos) || 1
292 editForm.urlla = row.als || ''
293 editForm.ftpdz = row.ftpdz || ''
294 editForm.xieyi = row.ptl === true || row.ptl === 'true'
295 editForm.kg = isOn(row.qk)
296 editVisible.value = true
297}
298
299async function onSave() {
300 saving.value = true
301 const r = await updateBaota({
302 id: editForm.id,
303 ip: editForm.ip,
304 dk: editForm.dk,
305 key: editForm.key,
306 btos: editForm.btos,
307 urlla: editForm.urlla,
308 ftpdz: editForm.ftpdz,
309 xieyi: editForm.xieyi ? 'true' : 'false',
310 kg: editForm.kg ? 'true' : 'false',
311 })
312 saving.value = false
313 if (r.ok) {
314 MessagePlugin.success('保存成功')
315 editVisible.value = false
316 load()
317 } else {
318 MessagePlugin.error(r.message || '保存失败')
319 }
320}
321
322function del(row) {
323 const dlg = DialogPlugin.confirm({
324 header: '删除宝塔',
325 body: `确定删除宝塔 #${row.id}(${row.btdh || ''})吗?`,
326 theme: 'warning',
327 onConfirm: async () => {
328 const r = await deleteBaota(row.id)
329 if (r.ok) {
330 MessagePlugin.success('删除成功')
331 load()
332 } else {
333 MessagePlugin.error(r.message || '删除失败')
334 }
335 dlg.destroy()
336 },
337 })
338}
339
340function batchDelete() {
341 if (!selectedIds.value.length) return
342 const dlg = DialogPlugin.confirm({
343 header: '批量删除',
344 body: `确定删除选中的 ${selectedIds.value.length} 条宝塔吗?`,
345 theme: 'warning',
346 onConfirm: async () => {
347 let ok = 0, fail = 0
348 for (const id of selectedIds.value) {
349 const r = await deleteBaota(id)
350 if (r.ok) ok++
351 else fail++
352 }
353 if (fail === 0) MessagePlugin.success(`成功删除 ${ok} 条`)
354 else MessagePlugin.warning(`成功 ${ok} 条, 失败 ${fail} 条`)
355 selectedIds.value = []
356 load()
357 dlg.destroy()
358 },
359 })
360}
361
362function openTutorial(row) {
363 router.push({ path: '/tutorial', query: { gn: 'mr', sz: String(row.id) } })
364}
365
366async function openPhpList(row) {
367 currentBtdh.value = row.btdh
368 phpVisible.value = true
369 phpLoading.value = true
370 phpList.value = []
371 phpCurrent.value = ''
372 phpSelected.value = ''
373 const r = await listNodePhp(row.btdh)
374 phpLoading.value = false
375 if (r.ok && r.data) {
376 if (Array.isArray(r.data.versions)) {
377 phpList.value = r.data.versions
378 phpCurrent.value = r.data.current_default || ''
379 phpSelected.value = phpCurrent.value || (r.data.latest || '')
380 } else if (Array.isArray(r.data)) {
381 phpList.value = r.data
382 } else if (Array.isArray(r.data.list)) {
383 phpList.value = r.data.list
384 } else {
385 phpList.value = []
386 }
387 } else {
388 MessagePlugin.warning(r.message || '获取失败')
389 }
390}
391
392async function setPhp() {
393 if (!phpSelected.value) {
394 MessagePlugin.warning('请选择 PHP 版本')
395 return
396 }
397 autoLoading.value = true
398 const r = await setNodePhp(currentBtdh.value, phpSelected.value)
399 autoLoading.value = false
400 if (r.ok) {
401 MessagePlugin.success(r.message || '设置成功')
402 phpVisible.value = false
403 } else {
404 MessagePlugin.error(r.message || '设置失败')
405 }
406}
407
408async function autoSetPhp() {
409 autoLoading.value = true
410 const r = await autoDetectNodePhp(currentBtdh.value)
411 autoLoading.value = false
412 if (r.ok) {
413 MessagePlugin.success('已自动设置')
414 phpVisible.value = false
415 } else {
416 MessagePlugin.error(r.message || '设置失败')
417 }
418}
419
420onMounted(load)
421</script>
422
423<style scoped>
424.td-mono {
425 font-family: Consolas, Monaco, monospace;
426 font-size: 12px;
427 word-break: break-all;
428}
429.td-php-current {
430 margin-bottom: 14px;
431 font-size: 13px;
432}
433.td-php-list {
434 display: flex;
435 flex-direction: column;
436 gap: 10px;
437}
438.td-php-list :deep(.t-radio) {
439 font-size: 13px;
440}
441</style>