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-database-sync-outline"></i>SQL 数据备份</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" :loading="creating" @click="create">
13 <i class="mdi mdi-database-plus-outline"></i> 立即备份
14 </t-button>
15 <div class="td-toolbar-spacer"></div>
16 <t-button theme="default" variant="text" @click="load">
17 <i class="mdi mdi-refresh"></i> 刷新
18 </t-button>
19 </div>
20
21 <t-table
22 row-key="id"
23 :data="rows"
24 :columns="columns"
25 :loading="loading"
26 table-layout="auto"
27 stripe
28 bordered
29 >
30 <template #empty>
31 <div class="td-empty">
32 <i class="mdi mdi-database-search-outline"></i>
33 暂无备份记录
34 </div>
35 </template>
36 <template #size="{ row }">{{ fmtSize(row.size || row.dx) }}</template>
37 <template #time="{ row }">{{ fmtTime(row.time || row.data || row.created_at) }}</template>
38 <template #operate="{ row }">
39 <div class="td-row-actions">
40 <t-button theme="default" variant="outline" size="small" @click="download(row)" title="下载">
41 <i class="mdi mdi-download"></i>
42 </t-button>
43 <t-button theme="warning" variant="outline" size="small" @click="restore(row)" title="恢复">
44 <i class="mdi mdi-database-refresh"></i>
45 </t-button>
46 <t-button theme="danger" variant="outline" size="small" @click="del(row)" title="删除">
47 <i class="mdi mdi-delete"></i>
48 </t-button>
49 </div>
50 </template>
51 </t-table>
52 </div>
53 </div>
54</template>
55
56<script setup>
57import { ref, reactive, onMounted } from 'vue'
58import { MessagePlugin, DialogPlugin } from 'tdesign-vue-next'
59import {
60 listSqlBackup,
61 createBackup,
62 restoreBackup,
63 deleteBackup,
64} from '@/user/api/database'
65
66const loading = ref(false)
67const creating = ref(false)
68const rows = ref([])
69
70const columns = [
71 { colKey: 'id', title: 'ID', width: 70 },
72 { colKey: 'name', title: '文件名', minWidth: 240, ellipsis: true },
73 { colKey: 'size', title: '大小', width: 110 },
74 { colKey: 'time', title: '备份时间', width: 160 },
75 { colKey: 'operate', title: '操作', width: 170, fixed: 'right' },
76]
77
78function fmtTime(v) {
79 if (!v) return '-'
80 const d = new Date(v)
81 if (isNaN(d.getTime())) return String(v)
82 return d.toLocaleString('zh-CN', { hour12: false })
83}
84
85function fmtSize(v) {
86 const n = Number(v) || 0
87 if (n === 0) return '-'
88 const units = ['B', 'KB', 'MB', 'GB', 'TB']
89 const pow = Math.min(Math.floor(Math.log(n) / Math.log(1024)), units.length - 1)
90 return (n / Math.pow(1024, pow)).toFixed(2) + ' ' + units[pow]
91}
92
93async function load() {
94 loading.value = true
95 const r = await listSqlBackup()
96 loading.value = false
97 if (r.ok && r.data) {
98 const d = r.data
99 rows.value = Array.isArray(d) ? d : (d.rows || d.list || d.data || [])
100 } else {
101 rows.value = []
102 }
103}
104
105async function create() {
106 creating.value = true
107 const r = await createBackup()
108 creating.value = false
109 if (r.ok) {
110 MessagePlugin.success('备份成功')
111 load()
112 }
113}
114
115function download(row) {
116 // 下载通过新建链接跳转
117 const url = `./ajax.php?gn=database&act=download&id=${encodeURIComponent(row.id)}`
118 const a = document.createElement('a')
119 a.href = url
120 a.target = '_blank'
121 a.rel = 'noopener'
122 document.body.appendChild(a)
123 a.click()
124 document.body.removeChild(a)
125}
126
127function restore(row) {
128 const dlg = DialogPlugin.confirm({
129 header: '恢复备份',
130 body: `确定使用备份「${row.name || row.id}」恢复数据库吗?当前数据将被覆盖且不可恢复。`,
131 theme: 'warning',
132 confirmBtn: { content: '恢复', theme: 'danger' },
133 onConfirm: async () => {
134 const r = await restoreBackup(row.id)
135 dlg.destroy()
136 if (r.ok) {
137 MessagePlugin.success('恢复成功')
138 load()
139 }
140 },
141 onClose: () => dlg.destroy(),
142 })
143}
144
145function del(row) {
146 const dlg = DialogPlugin.confirm({
147 header: '删除备份',
148 body: `确定删除备份「${row.name || row.id}」吗?此操作不可恢复。`,
149 confirmBtn: { content: '删除', theme: 'danger' },
150 onConfirm: async () => {
151 const r = await deleteBackup(row.id)
152 dlg.destroy()
153 if (r.ok) {
154 MessagePlugin.success('删除成功')
155 load()
156 }
157 },
158 onClose: () => dlg.destroy(),
159 })
160}
161
162onMounted(load)
163</script>
164
165<style scoped>
166.td-empty {
167 text-align: center;
168 padding: 36px 16px;
169 color: var(--td-text-placeholder);
170 font-size: 13px;
171}
172.td-empty i {
173 font-size: 36px;
174 display: block;
175 margin-bottom: 8px;
176 color: #cbd5e1;
177}
178</style>