仰望星辰工作室

miaopanel

miaopanel/ web/src/views/Database.vue 30.7 KB · 1,021 行 原始文件
1<script setup lang="ts">
2import { computed, inject, onMounted, reactive, ref } from 'vue'
3import { api, getCsrf } from '../api'
4import { navigate } from '../router'
5import { bytes, time } from '../format'
6import ConfirmDialog from '../components/ConfirmDialog.vue'
7
8interface ManagedDB {
9 id: number
10 name: string
11 username: string
12 password: string
13 host: string
14 charset: string
15 remark: string
16 managed: boolean
17 backup_count: number
18 created_at: number
19}
20
21interface Backup {
22 id: number
23 database_id: number
24 database_name: string
25 size: number
26 created_at: number
27}
28
29interface Info {
30 version: string
31 data_dir: string
32 port: number
33 socket: string
34 root_password_set: boolean
35 client: string
36}
37
38const notify = inject<(text: string, kind?: 'ok' | 'error') => void>('notify')!
39
40const rows = ref<ManagedDB[]>([])
41const loading = ref(false)
42const dbError = ref('')
43const q = ref('')
44const page = ref(1)
45const pageSize = ref(10)
46const selected = ref<number[]>([])
47const revealed = ref<Record<number, boolean>>({})
48
49const create = reactive({
50 open: false,
51 name: '',
52 username: '',
53 password: '',
54 charset: 'utf8mb4',
55 remark: '',
56 remote: false,
57 busy: false,
58})
59
60const root = reactive({ open: false, password: '', busy: false })
61const info = reactive({ open: false, busy: false, data: null as Info | null })
62const perms = reactive({ open: false, db: null as ManagedDB | null })
63const pwd = reactive({ open: false, db: null as ManagedDB | null, password: '', busy: false })
64const backups = reactive({
65 open: false,
66 db: null as ManagedDB | null,
67 items: [] as Backup[],
68 busy: false,
69})
70const recycle = reactive({ open: false, items: [] as Backup[], busy: false })
71const importTarget = ref<ManagedDB | null>(null)
72const importInput = ref<HTMLInputElement | null>(null)
73const backupBusy = ref(false)
74const restoring = ref(0)
75const importing = ref(false)
76const rowMenu = reactive({ open: false, db: null as ManagedDB | null })
77
78const dialog = reactive({
79 open: false,
80 title: '',
81 message: '',
82 confirmText: '确认',
83 danger: false,
84 requireText: '',
85 busy: false,
86 error: '',
87 run: (_payload: { text: string }) => Promise.resolve(),
88})
89
90function ask(
91 opts: {
92 title: string
93 message: string
94 confirmText?: string
95 danger?: boolean
96 requireText?: string
97 },
98 run: (payload: { text: string }) => Promise<void>,
99) {
100 dialog.title = opts.title
101 dialog.message = opts.message
102 dialog.confirmText = opts.confirmText ?? '确认'
103 dialog.danger = opts.danger ?? false
104 dialog.requireText = opts.requireText ?? ''
105 dialog.error = ''
106 dialog.busy = false
107 dialog.run = run
108 dialog.open = true
109}
110
111async function runDialog(payload: { text: string }) {
112 dialog.busy = true
113 dialog.error = ''
114 try {
115 await dialog.run(payload)
116 dialog.open = false
117 } catch (e) {
118 dialog.error = (e as Error).message
119 } finally {
120 dialog.busy = false
121 }
122}
123
124const filtered = computed(() => {
125 const needle = q.value.trim().toLowerCase()
126 if (!needle) return rows.value
127 return rows.value.filter(
128 (d) => d.name.toLowerCase().includes(needle) || d.username.toLowerCase().includes(needle),
129 )
130})
131const totalPages = computed(() => Math.max(1, Math.ceil(filtered.value.length / pageSize.value)))
132const paged = computed(() => {
133 const start = (page.value - 1) * pageSize.value
134 return filtered.value.slice(start, start + pageSize.value)
135})
136const allSelected = computed(() => paged.value.length > 0 && paged.value.every((d) => selected.value.includes(d.id)))
137
138async function load() {
139 loading.value = true
140 dbError.value = ''
141 try {
142 rows.value = await api.get<ManagedDB[]>('api/v1/databases')
143 page.value = Math.min(page.value, totalPages.value)
144 selected.value = selected.value.filter((id) => rows.value.some((d) => d.id === id))
145 } catch (e) {
146 // "MySQL 客户端未安装" is a normal first-run state, not a toast-worthy
147 // failure: keep it inline so users see the next step.
148 dbError.value = (e as Error).message
149 } finally {
150 loading.value = false
151 }
152}
153
154async function sync() {
155 loading.value = true
156 try {
157 rows.value = await api.post<ManagedDB[]>('api/v1/databases/sync')
158 notify('已同步服务器上的数据库')
159 } catch (e) {
160 notify((e as Error).message, 'error')
161 } finally {
162 loading.value = false
163 }
164}
165
166function openCreate(remote = false) {
167 create.open = true
168 create.name = ''
169 create.username = ''
170 create.password = randomPassword()
171 create.charset = 'utf8mb4'
172 create.remark = ''
173 create.remote = remote
174}
175
176function onName() {
177 if (!create.username || create.username === create.name) {
178 create.username = create.name
179 }
180}
181
182function randomPassword(): string {
183 const alphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789!@#$%^&*'
184 const buf = new Uint32Array(20)
185 crypto.getRandomValues(buf)
186 return Array.from(buf, (n) => alphabet[n % alphabet.length]).join('')
187}
188
189async function submitCreate() {
190 if (!create.name.trim()) {
191 notify('请填写数据库名', 'error')
192 return
193 }
194 create.busy = true
195 try {
196 await api.post('api/v1/databases', {
197 name: create.name.trim(),
198 username: create.username.trim() || create.name.trim(),
199 password: create.password,
200 charset: create.charset,
201 remark: create.remark,
202 remote: create.remote,
203 })
204 notify(`数据库 ${create.name} 已创建`)
205 create.open = false
206 await load()
207 } catch (e) {
208 notify((e as Error).message, 'error')
209 } finally {
210 create.busy = false
211 }
212}
213
214function togglePassword(id: number) {
215 revealed.value[id] = !revealed.value[id]
216}
217
218async function copy(text: string) {
219 if (!text) return
220 try {
221 await navigator.clipboard.writeText(text)
222 notify('已复制到剪贴板')
223 } catch {
224 notify('浏览器不允许访问剪贴板', 'error')
225 }
226}
227
228function openPMA(db?: ManagedDB) {
229 const base = location.pathname.endsWith('/') ? location.href : location.href + '/'
230 const url = new URL('api/v1/phpmyadmin/signon', base)
231 if (db?.name) url.searchParams.set('db', db.name)
232 window.open(url.toString(), '_blank')
233}
234
235function closeTool(ev: Event) {
236 ;(ev.currentTarget as HTMLElement).closest('details')?.removeAttribute('open')
237}
238
239async function openInfo() {
240 info.open = true
241 info.busy = true
242 try {
243 info.data = await api.get<Info>('api/v1/databases/info')
244 } catch (e) {
245 notify((e as Error).message, 'error')
246 } finally {
247 info.busy = false
248 }
249}
250
251async function openRoot() {
252 root.open = true
253 root.password = ''
254 if (!info.data) {
255 try {
256 info.data = await api.get<Info>('api/v1/databases/info')
257 } catch {
258 /* the form still works without the hint */
259 }
260 }
261}
262
263async function saveRoot() {
264 if (root.password.length < 8) {
265 notify('密码至少 8 位', 'error')
266 return
267 }
268 root.busy = true
269 try {
270 await api.post('api/v1/databases/root/password', { password: root.password })
271 notify('MySQL root 密码已更新')
272 root.open = false
273 info.data = null
274 } catch (e) {
275 notify((e as Error).message, 'error')
276 } finally {
277 root.busy = false
278 }
279}
280
281function openPerms(db: ManagedDB) {
282 perms.db = db
283 perms.open = true
284}
285
286function openPassword(db: ManagedDB) {
287 pwd.db = db
288 pwd.password = randomPassword()
289 pwd.open = true
290}
291
292async function savePassword() {
293 if (!pwd.db) return
294 if (pwd.password.length < 8) {
295 notify('密码至少 8 位', 'error')
296 return
297 }
298 pwd.busy = true
299 try {
300 const res = await api.post<{ password: string }>(`api/v1/databases/${pwd.db.id}/password`, {
301 password: pwd.password,
302 })
303 pwd.db.password = res.password
304 notify('密码已更新')
305 pwd.open = false
306 } catch (e) {
307 notify((e as Error).message, 'error')
308 } finally {
309 pwd.busy = false
310 }
311}
312
313function removeDatabase(db: ManagedDB) {
314 ask(
315 {
316 title: `删除数据库 ${db.name}`,
317 message: '数据库、同名用户以及该库的备份文件都会被删除,操作不可撤销。',
318 confirmText: '删除数据库',
319 danger: true,
320 requireText: db.name,
321 },
322 async () => {
323 await api.del(`api/v1/databases/${db.id}`)
324 notify(`数据库 ${db.name} 已删除`)
325 await load()
326 },
327 )
328}
329
330function removeSelected() {
331 const ids = [...selected.value]
332 if (!ids.length) return
333 ask(
334 {
335 title: `删除 ${ids.length} 个数据库`,
336 message: '选中的数据库、同名用户与备份都会被删除。批量操作不可撤销。',
337 confirmText: '全部删除',
338 danger: true,
339 requireText: 'DELETE',
340 },
341 async () => {
342 let failed = 0
343 for (const id of ids) {
344 try {
345 await api.del(`api/v1/databases/${id}`)
346 } catch {
347 failed++
348 }
349 }
350 selected.value = []
351 notify(
352 failed ? `已删除 ${ids.length - failed} 个,失败 ${failed} 个` : `已删除 ${ids.length} 个数据库`,
353 failed ? 'warn' : 'ok',
354 )
355 await load()
356 },
357 )
358}
359
360function toggleAll() {
361 if (allSelected.value) {
362 selected.value = selected.value.filter((id) => !paged.value.some((d) => d.id === id))
363 } else {
364 selected.value = Array.from(new Set([...selected.value, ...paged.value.map((d) => d.id)]))
365 }
366}
367
368async function openBackups(db: ManagedDB) {
369 backups.db = db
370 backups.open = true
371 await loadBackups(db.id)
372}
373
374async function loadBackups(id: number) {
375 backups.busy = true
376 try {
377 backups.items = await api.get<Backup[]>(`api/v1/databases/${id}/backups`)
378 } catch (e) {
379 notify((e as Error).message, 'error')
380 } finally {
381 backups.busy = false
382 }
383}
384
385async function createBackup() {
386 if (!backups.db) return
387 backups.busy = true
388 backupBusy.value = true
389 try {
390 await api.post(`api/v1/databases/${backups.db.id}/backup`)
391 notify('备份已生成')
392 await loadBackups(backups.db.id)
393 await load()
394 } catch (e) {
395 notify((e as Error).message, 'error')
396 } finally {
397 backups.busy = false
398 backupBusy.value = false
399 }
400}
401
402function restoreBackup(b: Backup) {
403 ask(
404 {
405 title: '恢复备份',
406 message: `将 ${b.database_name} 恢复到 ${time(b.created_at)} 的备份?当前数据会被完全覆盖,且无法撤销。`,
407 confirmText: '开始恢复',
408 danger: true,
409 requireText: b.database_name,
410 },
411 async () => {
412 restoring.value = b.id
413 try {
414 await api.post(`api/v1/databases/backups/${b.id}/restore`)
415 notify('恢复完成')
416 } finally {
417 restoring.value = 0
418 }
419 },
420 )
421}
422
423function deleteBackup(b: Backup) {
424 ask(
425 {
426 title: '删除备份',
427 message: `删除 ${b.database_name} 在 ${time(b.created_at)} 的备份?此操作不可恢复。`,
428 confirmText: '删除备份',
429 danger: true,
430 },
431 async () => {
432 await api.del(`api/v1/databases/backups/${b.id}`)
433 notify('备份已删除')
434 if (backups.open && backups.db) await loadBackups(backups.db.id)
435 if (recycle.open) await loadRecycle()
436 await load()
437 },
438 )
439}
440
441function downloadBackup(b: Backup) {
442 window.open(`api/v1/databases/backups/${b.id}/download`, '_blank')
443}
444
445async function loadRecycle() {
446 recycle.busy = true
447 try {
448 recycle.items = await api.get<Backup[]>('api/v1/databases/backups')
449 } catch (e) {
450 notify((e as Error).message, 'error')
451 } finally {
452 recycle.busy = false
453 }
454}
455
456async function openRecycle() {
457 recycle.open = true
458 await loadRecycle()
459}
460
461function triggerImport(db: ManagedDB) {
462 importTarget.value = db
463 importInput.value?.click()
464}
465
466function onImportFile(ev: Event) {
467 const input = ev.target as HTMLInputElement
468 const file = input.files?.[0]
469 const db = importTarget.value
470 input.value = ''
471 if (!file || !db) return
472 ask(
473 {
474 title: '导入 SQL 文件',
475 message: `将 ${file.name}(${bytes(file.size)})导入 ${db.name}?文件中已有的表会被覆盖。`,
476 confirmText: '开始导入',
477 },
478 async () => {
479 importing.value = true
480 try {
481 const fd = new FormData()
482 fd.append('file', file)
483 const res = await fetch(`api/v1/databases/${db.id}/import`, {
484 method: 'POST',
485 headers: { 'X-CSRF-Token': getCsrf() },
486 credentials: 'same-origin',
487 body: fd,
488 })
489 const body = (await res.json()) as { code: number; msg: string }
490 if (!res.ok || body.code !== 0) throw new Error(body.msg || '导入失败')
491 notify('SQL 文件已导入')
492 } finally {
493 importing.value = false
494 }
495 },
496 )
497}
498
499onMounted(load)
500</script>
501
502<template>
503 <div class="card">
504 <div class="row db-head">
505 <div>
506 <h2>数据库</h2>
507 <p class="hint">MySQL / MariaDB · 创建数据库、账号、备份与恢复</p>
508 </div>
509 <div class="spacer"></div>
510 <div class="row db-actions">
511 <button class="btn btn-primary btn-sm" @click="openCreate(false)">新建数据库</button>
512 <details class="tool-more">
513 <summary class="btn btn-ghost btn-sm">更多操作</summary>
514 <div class="tool-menu">
515 <button @click="openCreate(true); closeTool($event)">添加远程数据库</button>
516 <button @click="openPMA(); closeTool($event)">打开 phpMyAdmin</button>
517 <button @click="openInfo(); closeTool($event)">服务器信息</button>
518 <button @click="openRoot(); closeTool($event)">设置 root 密码</button>
519 <button @click="sync(); closeTool($event)">同步服务器数据库</button>
520 <button @click="openRecycle(); closeTool($event)">备份回收站</button>
521 </div>
522 </details>
523 </div>
524 </div>
525
526 <div v-if="dbError" class="alert warn db-error">
527 <div>
528 <b>数据库功能暂不可用</b>
529 <div class="hint" style="margin: 4px 0 0">{{ dbError }}</div>
530 </div>
531 <div class="row" style="gap: 8px">
532 <button class="btn btn-ghost btn-sm" :disabled="loading" @click="load">重试</button>
533 <button class="btn btn-primary btn-sm" @click="navigate('apps')">去应用商店安装</button>
534 </div>
535 </div>
536
537 <div class="row db-toolbar">
538 <input v-model="q" class="input" placeholder="搜索数据库名或用户名" style="max-width: 260px" @input="page = 1" />
539 <button class="btn btn-ghost btn-sm" :disabled="loading" @click="load">刷新</button>
540 <span class="muted">{{ loading ? '加载中…' : `共 ${filtered.length} 个数据库` }}</span>
541 <div class="spacer"></div>
542 <button v-if="selected.length" class="btn btn-danger btn-sm" @click="removeSelected">
543 批量删除({{ selected.length }})
544 </button>
545 </div>
546
547 <div class="table-wrap">
548 <table v-if="paged.length" class="table db-table">
549 <thead>
550 <tr>
551 <th style="width: 34px"><input type="checkbox" :checked="allSelected" @change="toggleAll" /></th>
552 <th>数据库名</th>
553 <th>用户名</th>
554 <th>密码</th>
555 <th>备份</th>
556 <th>数据库位置</th>
557 <th>备注</th>
558 <th style="width: 250px">操作</th>
559 </tr>
560 </thead>
561 <tbody>
562 <tr v-for="db in paged" :key="db.id">
563 <td><input v-model="selected" type="checkbox" :value="db.id" /></td>
564 <td>
565 <div class="db-name mono">{{ db.name }}</div>
566 <span v-if="!db.managed" class="badge off">未托管</span>
567 <span v-else class="badge ok">面板管理</span>
568 </td>
569 <td class="mono">{{ db.username }}</td>
570 <td>
571 <div class="pw">
572 <span class="mono">{{ db.password ? (revealed[db.id] ? db.password : '••••••••') : '未保存' }}</span>
573 <button v-if="db.password" class="icon-btn" title="显示密码" @click="togglePassword(db.id)">
574 {{ revealed[db.id] ? '隐藏' : '显示' }}
575 </button>
576 <button v-if="db.password" class="icon-btn" title="复制密码" @click="copy(db.password)">复制</button>
577 </div>
578 </td>
579 <td>
580 <button class="link" @click="openBackups(db)">
581 {{ db.backup_count ? `备份 (${db.backup_count})` : '备份' }}
582 </button>
583 </td>
584 <td>{{ db.host === '%' ? '远程数据库' : '本地数据库' }}</td>
585 <td class="remark">{{ db.remark || '—' }}</td>
586 <td>
587 <div class="row ops">
588 <button class="btn btn-sm btn-ghost" @click="openPMA(db)">管理</button>
589 <button class="btn btn-sm btn-ghost" @click="openBackups(db)">备份</button>
590 <button class="btn btn-sm btn-ghost" @click="openPassword(db)">改密</button>
591 <button class="btn btn-sm btn-ghost" @click="rowMenu.db = db; rowMenu.open = true">更多</button>
592 </div>
593 </td>
594 </tr>
595 </tbody>
596 </table>
597 <div v-else class="empty">
598 <p>{{ loading ? '正在加载…' : dbError ? '安装 MySQL / MariaDB 后即可在这里管理数据库。' : '还没有数据库。' }}</p>
599 <button v-if="!loading && !dbError" class="btn btn-primary btn-sm" @click="openCreate(false)">
600 新建数据库
601 </button>
602 </div>
603 </div>
604
605 <div class="pager">
606 <span class="muted">共 {{ filtered.length }} 条</span>
607 <select v-model.number="pageSize" class="select" style="width: 96px" @change="page = 1">
608 <option :value="10">10条/页</option>
609 <option :value="20">20条/页</option>
610 <option :value="50">50条/页</option>
611 </select>
612 <button class="btn btn-ghost btn-sm" :disabled="page <= 1" @click="page--">上一页</button>
613 <span class="mono">{{ page }} / {{ totalPages }}</span>
614 <button class="btn btn-ghost btn-sm" :disabled="page >= totalPages" @click="page++">下一页</button>
615 </div>
616 </div>
617
618 <!-- create -->
619 <div v-if="create.open" class="overlay" @click.self="create.open = false">
620 <div class="modal" style="width: min(520px, 100%)">
621 <div class="modal-head">
622 <h3>{{ create.remote ? '添加远程数据库' : '添加数据库' }}</h3>
623 <button class="icon-btn" @click="create.open = false">×</button>
624 </div>
625 <div class="form-grid">
626 <label class="field">
627 数据库名
628 <input v-model="create.name" class="input mono" placeholder="例如 blog" @input="onName" />
629 </label>
630 <label class="field">
631 用户名
632 <input v-model="create.username" class="input mono" placeholder="默认与数据库名相同" />
633 </label>
634 <label class="field full">
635 密码
636 <div class="row">
637 <input v-model="create.password" class="input mono" style="flex: 1" />
638 <button class="btn btn-ghost btn-sm" @click="create.password = randomPassword()">随机</button>
639 <button class="btn btn-ghost btn-sm" @click="copy(create.password)">复制</button>
640 </div>
641 </label>
642 <label class="field">
643 字符集
644 <select v-model="create.charset" class="select">
645 <option value="utf8mb4">utf8mb4</option>
646 <option value="utf8">utf8</option>
647 <option value="latin1">latin1</option>
648 <option value="gbk">gbk</option>
649 </select>
650 </label>
651 <label class="field">
652 备注
653 <input v-model="create.remark" class="input" placeholder="用途说明" />
654 </label>
655 <label class="check full">
656 <input v-model="create.remote" type="checkbox" />
657 允许远程访问(创建 user@'%',生产环境建议限制来源 IP)
658 </label>
659 </div>
660 <div class="modal-foot">
661 <p class="hint">创建后会同时生成同名数据库用户,并授予该库的全部权限。</p>
662 <div class="spacer"></div>
663 <button class="btn btn-ghost" @click="create.open = false">取消</button>
664 <button class="btn btn-primary" :disabled="create.busy" @click="submitCreate">
665 {{ create.busy ? '创建中…' : '创建' }}
666 </button>
667 </div>
668 </div>
669 </div>
670
671 <!-- root password -->
672 <div v-if="root.open" class="overlay" @click.self="root.open = false">
673 <div class="modal" style="width: min(480px, 100%)">
674 <div class="modal-head">
675 <h3>MySQL root 密码</h3>
676 <button class="icon-btn" @click="root.open = false">×</button>
677 </div>
678 <p class="hint">
679 当前状态:{{ info.data?.root_password_set ? '面板已保存密码' : '使用本机 socket 认证' }}。
680 修改后 root 将改为密码认证;phpMyAdmin 自动登录使用独立管理账号,不受这里影响。
681 </p>
682 <label class="field">
683 新密码
684 <input v-model="root.password" class="input mono" type="text" autocomplete="new-password" />
685 </label>
686 <div class="modal-foot">
687 <p class="hint">请先记录新密码;修改会立即生效。</p>
688 <div class="spacer"></div>
689 <button class="btn btn-ghost" @click="root.open = false">取消</button>
690 <button class="btn btn-primary" :disabled="root.busy" @click="saveRoot">保存</button>
691 </div>
692 </div>
693 </div>
694
695 <!-- advanced info -->
696 <div v-if="info.open" class="overlay" @click.self="info.open = false">
697 <div class="modal" style="width: min(560px, 100%)">
698 <div class="modal-head">
699 <h3>数据库高级设置</h3>
700 <button class="icon-btn" @click="info.open = false">×</button>
701 </div>
702 <div v-if="info.busy" class="empty">读取中…</div>
703 <dl v-else-if="info.data" class="kv">
704 <dt>客户端</dt><dd class="mono">{{ info.data.client }}</dd>
705 <dt>服务版本</dt><dd class="mono">{{ info.data.version }}</dd>
706 <dt>数据目录</dt><dd class="mono">{{ info.data.data_dir }}</dd>
707 <dt>端口</dt><dd class="mono">{{ info.data.port }}</dd>
708 <dt>Socket</dt><dd class="mono">{{ info.data.socket }}</dd>
709 <dt>root 密码</dt><dd>{{ info.data.root_password_set ? '已保存到面板' : '未设置(本机 socket 认证)' }}</dd>
710 </dl>
711 <div class="modal-foot">
712 <div class="spacer"></div>
713 <button class="btn btn-ghost" @click="info.open = false">关闭</button>
714 </div>
715 </div>
716 </div>
717
718 <!-- permissions -->
719 <div v-if="perms.open && perms.db" class="overlay" @click.self="perms.open = false">
720 <div class="modal" style="width: min(480px, 100%)">
721 <div class="modal-head">
722 <h3>权限 · {{ perms.db.name }}</h3>
723 <button class="icon-btn" @click="perms.open = false">×</button>
724 </div>
725 <dl class="kv">
726 <dt>用户</dt><dd class="mono">{{ perms.db.username }}</dd>
727 <dt>Host</dt><dd class="mono">{{ perms.db.host }}</dd>
728 <dt>权限</dt><dd>该用户对 <code>{{ perms.db.name }}.*</code> 拥有全部权限</dd>
729 </dl>
730 <p class="hint">如需更细粒度权限,可在 phpMyAdmin 中编辑用户权限。</p>
731 <div class="modal-foot">
732 <div class="spacer"></div>
733 <button class="btn btn-ghost" @click="perms.open = false">关闭</button>
734 </div>
735 </div>
736 </div>
737
738 <!-- change password -->
739 <div v-if="pwd.open && pwd.db" class="overlay" @click.self="pwd.open = false">
740 <div class="modal" style="width: min(480px, 100%)">
741 <div class="modal-head">
742 <h3>修改密码 · {{ pwd.db.name }}</h3>
743 <button class="icon-btn" @click="pwd.open = false">×</button>
744 </div>
745 <label class="field">
746 新密码
747 <div class="row">
748 <input v-model="pwd.password" class="input mono" style="flex: 1" />
749 <button class="btn btn-ghost btn-sm" @click="pwd.password = randomPassword()">随机</button>
750 </div>
751 </label>
752 <div class="modal-foot">
753 <div class="spacer"></div>
754 <button class="btn btn-ghost" @click="pwd.open = false">取消</button>
755 <button class="btn btn-primary" :disabled="pwd.busy" @click="savePassword">保存</button>
756 </div>
757 </div>
758 </div>
759
760 <!-- backups -->
761 <div v-if="backups.open && backups.db" class="overlay" @click.self="backups.open = false">
762 <div class="modal wide">
763 <div class="modal-head">
764 <div>
765 <h3>备份与工具 · {{ backups.db.name }}</h3>
766 <p class="hint">备份文件保存在面板 data/backups/db</p>
767 </div>
768 <div class="row">
769 <button class="btn btn-primary btn-sm" :disabled="backupBusy" @click="createBackup">
770 <span v-if="backupBusy" class="spin"></span>
771 {{ backupBusy ? '备份中…' : '立即备份' }}
772 </button>
773 <button class="btn btn-ghost btn-sm" :disabled="importing" @click="triggerImport(backups.db)">
774 {{ importing ? '导入中…' : '导入 SQL' }}
775 </button>
776 <button class="icon-btn" @click="backups.open = false">×</button>
777 </div>
778 </div>
779 <table v-if="backups.items.length" class="table">
780 <thead><tr><th>数据库</th><th>大小</th><th>时间</th><th style="width: 220px">操作</th></tr></thead>
781 <tbody>
782 <tr v-for="b in backups.items" :key="b.id">
783 <td class="mono">{{ b.database_name }}</td>
784 <td>{{ bytes(b.size) }}</td>
785 <td>{{ time(b.created_at) }}</td>
786 <td>
787 <div class="row ops">
788 <button class="btn btn-sm btn-ghost" @click="downloadBackup(b)">下载</button>
789 <button class="btn btn-sm btn-ghost" :disabled="restoring === b.id" @click="restoreBackup(b)">
790 {{ restoring === b.id ? '恢复中…' : '恢复' }}
791 </button>
792 <button class="btn btn-sm btn-danger" @click="deleteBackup(b)">删除</button>
793 </div>
794 </td>
795 </tr>
796 </tbody>
797 </table>
798 <div v-else class="empty">{{ backups.busy ? '读取中…' : '还没有备份' }}</div>
799 </div>
800 </div>
801
802 <!-- recycle bin (all backups) -->
803 <div v-if="recycle.open" class="overlay" @click.self="recycle.open = false">
804 <div class="modal wide">
805 <div class="modal-head">
806 <div>
807 <h3>备份回收站</h3>
808 <p class="hint">这里列出所有数据库备份;恢复会覆盖目标数据库。</p>
809 </div>
810 <button class="icon-btn" @click="recycle.open = false">×</button>
811 </div>
812 <table v-if="recycle.items.length" class="table">
813 <thead><tr><th>数据库</th><th>大小</th><th>时间</th><th style="width: 220px">操作</th></tr></thead>
814 <tbody>
815 <tr v-for="b in recycle.items" :key="b.id">
816 <td class="mono">{{ b.database_name }}</td>
817 <td>{{ bytes(b.size) }}</td>
818 <td>{{ time(b.created_at) }}</td>
819 <td>
820 <div class="row ops">
821 <button class="btn btn-sm btn-ghost" @click="downloadBackup(b)">下载</button>
822 <button class="btn btn-sm btn-ghost" :disabled="restoring === b.id" @click="restoreBackup(b)">
823 {{ restoring === b.id ? '恢复中…' : '恢复' }}
824 </button>
825 <button class="btn btn-sm btn-danger" @click="deleteBackup(b)">删除</button>
826 </div>
827 </td>
828 </tr>
829 </tbody>
830 </table>
831 <div v-else class="empty">{{ recycle.busy ? '读取中…' : '还没有备份文件' }}</div>
832 </div>
833 </div>
834
835 <div v-if="rowMenu.open && rowMenu.db" class="overlay" @click.self="rowMenu.open = false">
836 <div class="modal" style="width: min(420px, 100%)">
837 <div class="modal-head">
838 <div>
839 <h3>{{ rowMenu.db.name }}</h3>
840 <p class="hint mono">{{ rowMenu.db.username }}@{{ rowMenu.db.host }}</p>
841 </div>
842 <button class="icon-btn" @click="rowMenu.open = false">×</button>
843 </div>
844 <div class="action-list">
845 <button @click="openPerms(rowMenu.db); rowMenu.open = false">查看权限</button>
846 <button @click="triggerImport(rowMenu.db); rowMenu.open = false">导入 SQL 文件</button>
847 <button class="danger" @click="removeDatabase(rowMenu.db); rowMenu.open = false">删除数据库</button>
848 </div>
849 </div>
850 </div>
851
852 <ConfirmDialog
853 v-if="dialog.open"
854 :title="dialog.title"
855 :message="dialog.message"
856 :confirm-text="dialog.confirmText"
857 :danger="dialog.danger"
858 :require-text="dialog.requireText"
859 :busy="dialog.busy"
860 :error="dialog.error"
861 @close="dialog.open = false"
862 @confirm="runDialog"
863 />
864
865 <input ref="importInput" type="file" accept=".sql,.gz,.sql.gz" style="display: none" @change="onImportFile" />
866</template>
867
868<style scoped>
869.db-head {
870 align-items: flex-start;
871 flex-wrap: wrap;
872 gap: 10px;
873}
874.db-actions {
875 flex-wrap: wrap;
876 gap: 6px;
877}
878.db-toolbar {
879 margin: 16px 0 10px;
880 gap: 8px;
881 flex-wrap: wrap;
882}
883.db-error {
884 display: flex;
885 align-items: center;
886 justify-content: space-between;
887 gap: 12px;
888 flex-wrap: wrap;
889 margin-top: 14px;
890}
891.table-wrap {
892 overflow-x: auto;
893}
894.db-table td {
895 vertical-align: middle;
896}
897.db-name {
898 font-weight: 600;
899 margin-bottom: 3px;
900}
901.pw {
902 display: flex;
903 align-items: center;
904 gap: 4px;
905 white-space: nowrap;
906}
907.pw .icon-btn {
908 font-size: 11px;
909 padding: 2px 4px;
910}
911.remark {
912 max-width: 180px;
913 overflow: hidden;
914 text-overflow: ellipsis;
915 white-space: nowrap;
916}
917.ops {
918 gap: 4px;
919 flex-wrap: wrap;
920}
921.ops .btn {
922 padding: 3px 6px;
923}
924.link {
925 border: 0;
926 background: none;
927 color: var(--mint, #0ea5a4);
928 cursor: pointer;
929 padding: 0;
930 font: inherit;
931}
932.link:hover {
933 text-decoration: underline;
934}
935.sep {
936 color: #cbd5e1;
937 margin: 0 3px;
938}
939.pager {
940 display: flex;
941 align-items: center;
942 justify-content: flex-end;
943 gap: 8px;
944 margin-top: 14px;
945 flex-wrap: wrap;
946}
947.tool-more {
948 position: relative;
949}
950.tool-more summary {
951 list-style: none;
952}
953.tool-more summary::-webkit-details-marker {
954 display: none;
955}
956.tool-menu {
957 position: absolute;
958 right: 0;
959 top: calc(100% + 6px);
960 z-index: 25;
961 min-width: 200px;
962 padding: 6px;
963 border-radius: 12px;
964 border: 1px solid var(--line);
965 background: #fff;
966 box-shadow: 0 18px 40px rgba(23, 58, 67, 0.18);
967 display: flex;
968 flex-direction: column;
969}
970.tool-menu button {
971 text-align: left;
972 padding: 8px 10px;
973 border: none;
974 border-radius: 8px;
975 background: none;
976 font: inherit;
977 cursor: pointer;
978 color: var(--text);
979}
980.tool-menu button:hover {
981 background: #f1f9f8;
982}
983.action-list {
984 display: flex;
985 flex-direction: column;
986 gap: 6px;
987}
988.action-list button {
989 text-align: left;
990 padding: 10px 12px;
991 border-radius: 10px;
992 border: 1px solid var(--line);
993 background: #fbfefd;
994 color: var(--text);
995 cursor: pointer;
996 font: inherit;
997}
998.action-list button:hover {
999 border-color: rgba(52, 211, 153, 0.45);
1000 background: #f2fbf8;
1001 color: #0f766e;
1002}
1003.action-list button.danger {
1004 color: #dc2626;
1005 border-color: rgba(239, 68, 68, 0.25);
1006 background: #fff7f7;
1007}
1008.kv {
1009 display: grid;
1010 grid-template-columns: 110px 1fr;
1011 gap: 8px 14px;
1012 margin: 8px 0 16px;
1013}
1014.kv dt {
1015 color: #64748b;
1016}
1017.kv dd {
1018 margin: 0;
1019 word-break: break-all;
1020}
1021</style>