-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathBackupRestoreDialog.vue
More file actions
599 lines (531 loc) · 13.8 KB
/
BackupRestoreDialog.vue
File metadata and controls
599 lines (531 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
<template>
<a-modal
v-model:visible="visible"
title="数据备份还原"
width="520px"
:footer="false"
@cancel="handleCancel"
>
<div class="backup-restore-container">
<!-- 数据统计信息 -->
<div class="stats-section">
<h3 class="section-title">
<icon-info-circle class="title-icon" />
当前数据统计
</h3>
<div class="stats-grid">
<div class="stat-item">
<div class="stat-value">{{ stats.settings }}</div>
<div class="stat-label">设置项</div>
</div>
<div class="stat-item">
<div class="stat-value">{{ stats.favorites }}</div>
<div class="stat-label">收藏</div>
</div>
<div class="stat-item">
<div class="stat-value">{{ stats.watchHistory }}</div>
<div class="stat-label">观看记录</div>
</div>
<div class="stat-item">
<div class="stat-value">{{ stats.parsers }}</div>
<div class="stat-label">解析器</div>
</div>
<div class="stat-item">
<div class="stat-value">{{ stats.sites }}</div>
<div class="stat-label">站点</div>
</div>
</div>
<div class="data-size">
数据大小: {{ formatSize(stats.totalSize) }}
</div>
</div>
<!-- 备份操作 -->
<div class="operation-section">
<h3 class="section-title">
<icon-download class="title-icon" />
数据备份
</h3>
<div class="operation-content">
<p class="operation-desc">
将当前所有设置、收藏、历史记录、解析器等数据导出为JSON文件
</p>
<a-button
type="primary"
size="large"
:loading="backupLoading"
@click="handleBackup"
>
<template #icon>
<icon-download />
</template>
导出备份文件
</a-button>
</div>
</div>
<!-- 还原操作 -->
<div class="operation-section">
<h3 class="section-title">
<icon-upload class="title-icon" />
数据还原
</h3>
<div class="operation-content">
<p class="operation-desc">
选择之前导出的备份文件来还原设置、收藏、历史记录、解析器等数据
</p>
<div class="restore-actions">
<a-upload
ref="uploadRef"
:key="uploadKey"
:show-file-list="false"
:auto-upload="false"
:custom-request="() => {}"
:limit="1"
accept=".json"
@change="handleFileChange"
>
<a-button size="large">
<template #icon>
<icon-folder />
</template>
选择备份文件
</a-button>
</a-upload>
<!-- 手动显示文件列表 -->
<div v-if="fileList.length > 0" class="custom-file-list">
<div
v-for="fileItem in fileList"
:key="fileItem.uid"
class="custom-upload-item"
>
<div class="file-info">
<icon-file class="file-icon" />
<span class="file-name">{{ fileItem.name }}</span>
<span class="file-size">({{ formatSize(fileItem.size) }})</span>
</div>
<a-button
type="text"
size="small"
@click="handleRemoveFile(fileItem)"
class="remove-btn"
>
<template #icon>
<icon-close />
</template>
</a-button>
</div>
</div>
<a-button
type="primary"
size="large"
:loading="restoreLoading"
:disabled="!canRestore"
@click="handleRestore"
>
<template #icon>
<icon-upload />
</template>
还原数据
</a-button>
</div>
</div>
</div>
<!-- 警告提示 -->
<div class="warning-section">
<a-alert
type="warning"
show-icon
>
<template #icon>
<icon-exclamation-circle />
</template>
<div>
<strong>重要提示:</strong>
<ul class="warning-list">
<li>还原数据将覆盖当前所有设置和用户数据</li>
<li>建议在还原前先备份当前数据</li>
<li>还原完成后需要刷新页面才能生效</li>
</ul>
</div>
</a-alert>
</div>
</div>
</a-modal>
</template>
<script setup>
import { ref, computed, watch, onMounted } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import {
IconInfoCircle,
IconDownload,
IconUpload,
IconFolder,
IconFile,
IconClose,
IconExclamationCircle
} from '@arco-design/web-vue/es/icon'
import backupService from '@/services/backupService'
// Props
const props = defineProps({
visible: {
type: Boolean,
default: false
}
})
// Emits
const emit = defineEmits(['update:visible'])
// 响应式数据
const visible = computed({
get: () => props.visible,
set: (value) => emit('update:visible', value)
})
const stats = ref({
settings: 0,
favorites: 0,
watchHistory: 0,
sites: 0,
parsers: 0,
totalSize: 0
})
const backupLoading = ref(false)
const restoreLoading = ref(false)
const selectedFile = ref(null)
const fileList = ref([])
const uploadRef = ref(null)
const uploadKey = ref(0) // 用于强制重新渲染Upload组件
// 计算属性:是否可以还原
const canRestore = computed(() => {
const result = !!selectedFile.value
console.log('canRestore 计算结果:', result, 'selectedFile:', selectedFile.value)
return result
})
// 格式化文件大小
const formatSize = (bytes) => {
// 处理无效输入
if (bytes === null || bytes === undefined || isNaN(bytes) || bytes === 0) {
return '0 B'
}
const numBytes = Number(bytes)
const k = 1024
const sizes = ['B', 'KB', 'MB', 'GB']
const i = Math.floor(Math.log(numBytes) / Math.log(k))
return parseFloat((numBytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}
// 加载数据统计
const loadStats = () => {
try {
stats.value = backupService.getBackupStats()
} catch (error) {
console.error('加载数据统计失败:', error)
}
}
// 处理备份
const handleBackup = async () => {
backupLoading.value = true
try {
const result = await backupService.exportBackupData()
if (result.success) {
// 刷新统计数据
loadStats()
}
} catch (error) {
console.error('备份失败:', error)
Message.error('备份失败: ' + error.message)
} finally {
backupLoading.value = false
}
}
// 处理文件变化事件
const handleFileChange = (fileListParam, file) => {
console.log('=== 文件变化事件调试信息 ===')
console.log('fileListParam:', fileListParam)
console.log('file:', file)
// 如果文件列表为空,清空状态
if (!fileListParam || fileListParam.length === 0) {
selectedFile.value = null
fileList.value = []
console.log('文件列表已清空')
return
}
// 处理文件添加
if (file && file.file) {
const actualFile = file.file
console.log('实际文件对象:', actualFile)
console.log('文件名:', actualFile.name)
console.log('文件大小:', actualFile.size, typeof actualFile.size)
console.log('文件类型:', actualFile.type)
// 检查文件类型
if (!actualFile.name.endsWith('.json')) {
Message.error('请选择JSON格式的备份文件')
selectedFile.value = null
fileList.value = []
return
}
// 检查文件大小 (限制10MB)
if (actualFile.size > 10 * 1024 * 1024) {
Message.error('文件大小不能超过10MB')
selectedFile.value = null
fileList.value = []
return
}
// 更新选中的文件和文件列表
selectedFile.value = actualFile
fileList.value = [{
uid: file.uid || Date.now().toString(),
name: actualFile.name,
size: actualFile.size,
status: 'success',
file: actualFile,
percent: 100
}]
console.log('selectedFile 设置为:', selectedFile.value)
console.log('fileList 设置为:', fileList.value)
console.log('canRestore 计算结果:', !!selectedFile.value)
Message.success('文件选择成功,点击"还原数据"开始还原')
}
}
// 处理文件移除
const handleRemoveFile = (file) => {
selectedFile.value = null
fileList.value = []
// 强制重新渲染Upload组件
uploadKey.value++
Message.info('已移除选中的备份文件')
}
// 处理还原
const handleRestore = async () => {
if (!selectedFile.value) {
Message.error('请先选择备份文件')
return
}
// 确认对话框
Modal.confirm({
title: '确认还原数据',
content: '此操作将覆盖当前所有数据,是否继续?',
okText: '确认还原',
cancelText: '取消',
onOk: async () => {
restoreLoading.value = true
try {
const result = await backupService.importBackupFromFile(selectedFile.value)
if (result.success) {
selectedFile.value = null
fileList.value = []
loadStats()
if (result.needsReload) {
Modal.confirm({
title: '还原成功',
content: '数据还原成功!需要刷新页面才能生效,是否立即刷新?',
okText: '立即刷新',
cancelText: '稍后刷新',
onOk: () => {
window.location.reload()
}
})
}
}
} catch (error) {
console.error('还原失败:', error)
Message.error('还原失败: ' + error.message)
} finally {
restoreLoading.value = false
}
}
})
}
// 清空选中的文件
const clearSelectedFile = () => {
selectedFile.value = null
fileList.value = []
// 强制重新渲染Upload组件
uploadKey.value++
}
// 处理对话框关闭
const handleCancel = () => {
visible.value = false
clearSelectedFile()
}
// 监听对话框显示状态
watch(visible, (newVisible) => {
if (newVisible) {
loadStats()
}
})
// 组件挂载时加载统计数据
onMounted(() => {
loadStats()
})
</script>
<style scoped>
.backup-restore-container {
padding: 4px 0; /* 进一步减少容器内边距 */
}
.section-title {
display: flex;
align-items: center;
font-size: 16px;
font-weight: 600;
color: var(--color-text-1);
margin-bottom: 8px;
}
.title-icon {
margin-right: 6px;
color: var(--color-primary);
}
.stats-section {
margin-bottom: 16px;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 8px;
margin-bottom: 8px;
}
.stat-item {
text-align: center;
padding: 8px 4px;
background: var(--color-bg-2);
border-radius: 6px;
border: 1px solid var(--color-border-2);
}
.stat-value {
font-size: 18px;
font-weight: 600;
color: var(--color-primary);
margin-bottom: 2px;
line-height: 1.2;
}
.stat-label {
font-size: 11px;
color: var(--color-text-3);
line-height: 1.2;
}
.data-size {
text-align: center;
font-size: 12px;
color: var(--color-text-2);
padding: 6px;
background: var(--color-bg-1);
border-radius: 4px;
line-height: 1.2;
}
.operation-section {
margin-bottom: 16px;
}
.operation-content {
background: var(--color-bg-1);
padding: 12px;
border-radius: 8px;
border: 1px solid var(--color-border-2);
}
.operation-desc {
margin-bottom: 10px;
color: var(--color-text-2);
line-height: 1.3;
font-size: 13px;
}
.restore-actions {
display: flex;
flex-direction: column;
gap: 8px;
margin-bottom: 10px;
align-items: flex-start;
}
.selected-file {
display: flex;
align-items: center;
padding: 8px;
background: var(--color-bg-2);
border-radius: 6px;
border: 1px solid var(--color-border-2);
}
.file-icon {
margin-right: 6px;
color: var(--color-primary);
}
.file-name {
flex: 1;
font-size: 13px;
color: var(--color-text-1);
word-break: break-all;
line-height: 1.3;
}
.warning-section {
margin-top: 12px;
}
.warning-list {
margin: 4px 0 0 0;
padding-left: 16px;
}
.warning-list li {
margin-bottom: 2px;
color: var(--color-text-2);
font-size: 12px;
line-height: 1.3;
}
/* 自定义文件列表样式 */
.custom-file-list {
margin-top: 8px;
}
/* 强制隐藏 Upload 组件内部的文件列表 */
.restore-actions .arco-upload-list {
display: none !important;
}
.restore-actions .arco-upload-list-item {
display: none !important;
}
.custom-upload-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 8px 12px;
background: var(--color-fill-2);
border-radius: 4px;
margin-top: 6px;
min-height: 36px;
}
.file-info {
display: flex;
align-items: center;
flex: 1;
min-height: 20px;
}
.file-icon {
margin-right: 6px;
color: var(--color-text-3);
font-size: 14px;
}
.file-name {
color: var(--color-text-1);
font-weight: 500;
margin-right: 6px;
line-height: 1.3;
font-size: 13px;
}
.file-size {
color: var(--color-text-3);
font-size: 11px;
line-height: 1.3;
}
.remove-btn {
color: var(--color-text-3);
display: flex;
align-items: center;
justify-content: center;
min-width: 20px;
min-height: 20px;
}
.remove-btn:hover {
color: var(--color-danger);
}
/* 响应式设计 */
@media (max-width: 768px) {
.stats-grid {
grid-template-columns: repeat(3, 1fr);
}
}
@media (max-width: 480px) {
.stats-grid {
grid-template-columns: repeat(2, 1fr);
}
}
</style>