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-account-group"></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="openEdit(null)">
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="600"
28 >
29 <template #container_status="{ row }">
30 <t-tag :theme="csTheme(row.container_status)" variant="light" size="small">
31 {{ csText(row.container_status) }}
32 </t-tag>
33 </template>
34 <template #datae="{ row }">
35 {{ row.datae === '0000-00-00' ? '永久' : row.datae }}
36 </template>
37 <template #qk="{ row }">
38 <t-tag :theme="qkTheme(row.qk)" variant="light" size="small">{{ qkText(row.qk) }}</t-tag>
39 </template>
40 <template #operate="{ row }">
41 <div class="td-row-actions">
42 <t-button theme="default" variant="outline" size="small" @click="openEdit(row)">编辑</t-button>
43 <t-button theme="default" variant="outline" size="small" @click="openReset(row)">改密</t-button>
44 <t-button
45 v-if="row.qk === 'active'"
46 theme="warning"
47 variant="outline"
48 size="small"
49 @click="toggleStatus(row, true)"
50 >暂停</t-button>
51 <t-button
52 v-else
53 theme="success"
54 variant="outline"
55 size="small"
56 @click="toggleStatus(row, false)"
57 >恢复</t-button>
58 <t-button theme="danger" variant="outline" size="small" @click="del(row)">删除</t-button>
59 </div>
60 </template>
61 </t-table>
62 </div>
63
64 <!-- 添加/编辑用户弹窗 -->
65 <t-dialog
66 v-model:visible="editVisible"
67 :header="editForm && editForm.id ? '编辑用户' : '添加用户'"
68 :on-confirm="onEdit"
69 width="560px"
70 :confirm-btn="{ loading: saving }"
71 >
72 <div class="td-form" v-if="editForm">
73 <template v-if="!editForm.id">
74 <div class="td-form-row">
75 <label>账号 <span class="td-text-danger">*</span></label>
76 <t-input v-model="editForm.username" />
77 </div>
78 <div class="td-form-row">
79 <label>密码 <span class="td-text-danger">*</span></label>
80 <t-input v-model="editForm.password" type="password">
81 <template #suffix>
82 <t-link theme="primary" @click="editForm.password = randomStr(12)"><i class="mdi mdi-shuffle-variant"></i></t-link>
83 </template>
84 </t-input>
85 </div>
86 </template>
87
88 <div class="td-form-row">
89 <label>邮箱</label>
90 <t-input v-model="editForm.email" />
91 </div>
92
93 <div class="td-form-row">
94 <label>Docker 节点 <span class="td-text-danger">*</span></label>
95 <t-select v-model="editForm.ssbt" :loading="optionsLoading">
96 <t-option
97 v-for="n in nodeOptions"
98 :key="n.id"
99 :value="n.id"
100 :label="`${n.name} (${n.btip})`"
101 />
102 </t-select>
103 </div>
104
105 <div class="td-form-row">
106 <label>套餐</label>
107 <t-select v-model="editForm.plan_id" :loading="optionsLoading" clearable>
108 <t-option value="0" label="无套餐" />
109 <t-option
110 v-for="p in planOptions"
111 :key="p.id"
112 :value="p.id"
113 :label="planLabel(p)"
114 />
115 </t-select>
116 </div>
117
118 <div class="td-form-row">
119 <label>到期时间</label>
120 <t-date-picker
121 v-model="editForm.datae"
122 mode="date"
123 format="YYYY-MM-DD"
124 value-format="YYYY-MM-DD"
125 clearable
126 />
127 <div class="td-form-hint">0000-00-00 = 永久</div>
128 </div>
129
130 <div v-if="editForm.id" class="td-form-row">
131 <label>状态</label>
132 <t-select v-model="editForm.qk">
133 <t-option value="active" label="正常" />
134 <t-option value="paused" label="暂停" />
135 <t-option value="expired" label="到期" />
136 </t-select>
137 </div>
138 </div>
139 </t-dialog>
140
141 <!-- 重置密码弹窗 -->
142 <t-dialog
143 v-model:visible="resetVisible"
144 header="重置密码"
145 :on-confirm="onReset"
146 width="420px"
147 :confirm-btn="{ loading: resetting }"
148 >
149 <div class="td-form">
150 <div class="td-form-row">
151 <label>新密码 <span class="td-text-danger">*</span></label>
152 <t-input v-model="resetPassword" type="password">
153 <template #suffix>
154 <t-link theme="primary" @click="resetPassword = randomStr(12)"><i class="mdi mdi-shuffle-variant"></i></t-link>
155 </template>
156 </t-input>
157 </div>
158 </div>
159 </t-dialog>
160 </div>
161</template>
162
163<script setup>
164import { ref, reactive, onMounted } from 'vue'
165import { MessagePlugin, DialogPlugin } from 'tdesign-vue-next'
166import {
167 listDockerUser, addDockerUser, editDockerUser, delDockerUser,
168 resetDockerUser, setDockerUserStatus, dockerOptions,
169} from '@/admin/api/docker'
170
171const loading = ref(false)
172const saving = ref(false)
173const resetting = ref(false)
174const rows = ref([])
175
176const columns = [
177 { colKey: 'id', title: 'ID', width: 70 },
178 { colKey: 'username', title: '账号', minWidth: 120, ellipsis: true },
179 { colKey: 'email', title: '邮箱', minWidth: 140, ellipsis: true },
180 { colKey: 'node_name', title: '节点', minWidth: 130, ellipsis: true },
181 { colKey: 'plan_name', title: '套餐', minWidth: 110, ellipsis: true },
182 { colKey: 'app_name', title: '应用', minWidth: 110, ellipsis: true },
183 { colKey: 'container_status', title: '容器状态', width: 110, align: 'center' },
184 { colKey: 'disk_usage', title: '磁盘用量', width: 110, align: 'center', cell: (h, { row }) => formatDiskUsage(row.disk_usage) },
185 { colKey: 'data', title: '开通', width: 160 },
186 { colKey: 'datae', title: '到期', width: 110 },
187 { colKey: 'qk', title: '状态', width: 90, align: 'center' },
188 { colKey: 'operate', title: '操作', width: 260, fixed: 'right' },
189]
190
191// 选项
192const optionsLoading = ref(false)
193const nodeOptions = ref([])
194const planOptions = ref([])
195
196// 编辑弹窗
197const editVisible = ref(false)
198const editForm = ref(null)
199
200// 重置密码
201const resetVisible = ref(false)
202const resetUserId = ref(null)
203const resetPassword = ref('')
204
205const csMap = {
206 none: ['default', '未创建'],
207 creating: ['warning', '创建中'],
208 running: ['success', '运行中'],
209 stopped: ['default', '已停止'],
210 failed: ['danger', '失败'],
211}
212const qkMap = {
213 active: ['success', '正常'],
214 paused: ['warning', '已暂停'],
215 expired: ['danger', '已到期'],
216 pruned: ['default', '已清理'],
217}
218function planLabel(p) {
219 let s = `${p.name} (${p.cpu_max}核/${p.mem_max}MB`
220 if (p.disk_max && p.disk_max !== '0') s += `/${p.disk_max}MB磁盘`
221 if (p.proxy_max && p.proxy_max !== '0') s += `/${p.proxy_max}代理`
222 s += `/¥${p.jg})`
223 return s
224}
225
226function csText(v) { return (csMap[v] || ['default', v || '未知'])[1] }
227function csTheme(v) { return (csMap[v] || ['default'])[0] }
228function formatDiskUsage(n) {
229 n = parseInt(n, 10) || 0
230 if (n <= 0) return '-'
231 if (n < 1073741824) return (n / 1048576).toFixed(1) + ' MB'
232 return (n / 1073741824).toFixed(2) + ' GB'
233}
234
235function qkText(v) { return (qkMap[v] || ['default', v || '未知'])[1] }
236function qkTheme(v) { return (qkMap[v] || ['default'])[0] }
237
238function randomStr(len) {
239 const chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'
240 let s = ''
241 for (let i = 0; i < (len || 12); i++) s += chars.charAt(Math.floor(Math.random() * chars.length))
242 return s
243}
244
245async function load() {
246 loading.value = true
247 const r = await listDockerUser()
248 loading.value = false
249 if (r.ok && r.data) {
250 rows.value = r.data.data || []
251 } else if (!r.ok) {
252 MessagePlugin.error(r.message || '加载失败')
253 }
254}
255
256async function loadOptions() {
257 optionsLoading.value = true
258 const r = await dockerOptions()
259 optionsLoading.value = false
260 if (r.ok && r.data) {
261 nodeOptions.value = r.data.nodes || []
262 planOptions.value = r.data.plans || []
263 }
264}
265
266function openEdit(row) {
267 editForm.value = reactive({
268 id: row ? row.id : 0,
269 username: row ? row.username || '' : '',
270 password: '',
271 email: row ? row.email || '' : '',
272 ssbt: row ? Number(row.ssbt || 0) : '',
273 plan_id: row ? Number(row.plan_id || 0) : 0,
274 datae: row ? (row.datae && row.datae !== '0000-00-00' ? row.datae : '') : '',
275 qk: row ? row.qk || 'active' : 'active',
276 })
277 editVisible.value = true
278 if (!nodeOptions.value.length) loadOptions()
279}
280
281async function onEdit() {
282 const f = editForm.value
283 if (!f.id && (!f.username || !f.password)) {
284 MessagePlugin.warning('账号密码必填')
285 return
286 }
287 if (!f.ssbt) {
288 MessagePlugin.warning('请选择 Docker 节点')
289 return
290 }
291 saving.value = true
292 const data = {
293 id: f.id || undefined,
294 username: f.username,
295 password: f.password,
296 email: f.email,
297 ssbt: f.ssbt,
298 plan_id: Number(f.plan_id || 0),
299 datae: f.datae || '0000-00-00',
300 }
301 if (f.id) data.qk = f.qk
302 const r = f.id ? await editDockerUser(data) : await addDockerUser(data)
303 saving.value = false
304 if (r.ok) {
305 MessagePlugin.success(r.message || '保存成功')
306 editVisible.value = false
307 load()
308 } else {
309 MessagePlugin.error(r.message || '保存失败')
310 }
311}
312
313function openReset(row) {
314 resetUserId.value = row.id
315 resetPassword.value = randomStr(12)
316 resetVisible.value = true
317}
318
319async function onReset() {
320 if (!resetPassword.value) {
321 MessagePlugin.warning('密码不能为空')
322 return
323 }
324 resetting.value = true
325 const r = await resetDockerUser(resetUserId.value, resetPassword.value)
326 resetting.value = false
327 if (r.ok) {
328 MessagePlugin.success('密码已重置')
329 resetVisible.value = false
330 } else {
331 MessagePlugin.error(r.message || '重置失败')
332 }
333}
334
335async function toggleStatus(row, paused) {
336 const r = await setDockerUserStatus(row.id, paused)
337 if (r.ok) {
338 MessagePlugin.success(paused ? '已暂停' : '已恢复')
339 load()
340 } else {
341 MessagePlugin.error(r.message || '操作失败')
342 }
343}
344
345function del(row) {
346 const dlg = DialogPlugin.confirm({
347 header: '删除用户',
348 body: `确定删除 Docker 用户「${row.username || row.id}」吗?删除用户不会自动删除其容器。`,
349 theme: 'warning',
350 onConfirm: async () => {
351 const r = await delDockerUser(row.id)
352 if (r.ok) {
353 MessagePlugin.success('删除成功')
354 load()
355 } else {
356 MessagePlugin.error(r.message || '删除失败')
357 }
358 dlg.destroy()
359 },
360 })
361}
362
363onMounted(() => {
364 load()
365 loadOptions()
366})
367</script>
368
369<style scoped>
370.td-page-actions {
371 display: flex;
372 gap: 8px;
373}
374</style>