fix: 优化后台列表与系统设置布局
This commit is contained in:
@@ -0,0 +1,256 @@
|
||||
<template>
|
||||
<div class="backup-panel">
|
||||
<div class="backup-toolbar">
|
||||
<n-button type="primary" :loading="creating" @click="createBackup">创建备份</n-button>
|
||||
<n-upload :show-file-list="false" accept=".zip" :custom-request="previewRestore" :disabled="restoring">
|
||||
<n-button :loading="restoring">上传备份包预校验</n-button>
|
||||
</n-upload>
|
||||
<n-button @click="load">刷新</n-button>
|
||||
</div>
|
||||
|
||||
<n-alert v-if="restorePreviewResult" type="warning" class="restore-alert" :show-icon="true">
|
||||
<div class="restore-content">
|
||||
<span>
|
||||
已通过备份包预校验:{{ restorePreviewResult.manifest?.createdAt || '-' }}。恢复会整库覆盖当前系统,确认前会自动生成当前快照。
|
||||
</span>
|
||||
<n-button size="small" type="error" :loading="restoring" @click="commitRestore">确认恢复</n-button>
|
||||
</div>
|
||||
</n-alert>
|
||||
|
||||
<n-data-table
|
||||
:columns="columns"
|
||||
:data="rows"
|
||||
:loading="loading"
|
||||
:pagination="false"
|
||||
class="table-fill backup-table"
|
||||
flex-height
|
||||
remote
|
||||
:scroll-x="1180"
|
||||
:row-key="(r) => r.id"
|
||||
/>
|
||||
<div class="table-pagination">
|
||||
<n-pagination v-bind="pagination" :disabled="loading" />
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, h, onMounted, reactive, ref } from 'vue';
|
||||
import type { UploadCustomRequestOptions } from 'naive-ui';
|
||||
import { NButton, NDropdown, NIcon, NTag, type DataTableColumns, type DropdownOption } from 'naive-ui';
|
||||
import { DownloadOutline, FilterOutline, TrashOutline } from '@vicons/ionicons5';
|
||||
import { backupApi } from '@/api';
|
||||
import { dialog, message } from '@/utils/feedback';
|
||||
import { downloadFile } from '@/utils/download';
|
||||
import { fmt, statusType } from '@/utils/format';
|
||||
import { useTablePage } from '@/composables/useTablePage';
|
||||
|
||||
const rows = ref<any[]>([]);
|
||||
const loading = ref(false);
|
||||
const creating = ref(false);
|
||||
const restoring = ref(false);
|
||||
const restorePreviewResult = ref<any>(null);
|
||||
const query = reactive({ status: null as string | null, mode: null as string | null });
|
||||
const { pagination, page, pageSize, resetPage, setPageResult } = useTablePage(load, 20);
|
||||
|
||||
const statusOptions = ['PENDING', 'PROCESSING', 'SUCCESS', 'FAILED'].map((value) => ({ label: value, value }));
|
||||
const modeOptions = ['BACKUP', 'RESTORE', 'SCHEDULED', 'AUTO_BEFORE_RESTORE'].map((value) => ({ label: value, value }));
|
||||
|
||||
const columns = computed<DataTableColumns<any>>(() => [
|
||||
{ title: '创建时间', key: 'createdAt', width: 170, render: (row) => fmt(row.createdAt) },
|
||||
{ title: '文件名', key: 'fileName', minWidth: 260, ellipsis: { tooltip: true } },
|
||||
{
|
||||
title: () => renderFilterTitle('类型', query.mode, modeOptions, (value) => setFilter('mode', value)),
|
||||
key: 'mode',
|
||||
width: 170,
|
||||
},
|
||||
{
|
||||
title: () => renderFilterTitle('状态', query.status, statusOptions, (value) => setFilter('status', value)),
|
||||
key: 'status',
|
||||
width: 130,
|
||||
align: 'center',
|
||||
render: (row) => h(NTag, { size: 'small', type: statusType(row.status) }, { default: () => row.status }),
|
||||
},
|
||||
{ title: '大小', key: 'size', width: 120, render: (row) => formatSize(row.size) },
|
||||
{ title: '错误', key: 'error', minWidth: 240, ellipsis: { tooltip: true }, render: (row) => row.error || '-' },
|
||||
{
|
||||
title: '操作',
|
||||
key: 'ops',
|
||||
width: 110,
|
||||
align: 'center',
|
||||
fixed: 'right',
|
||||
render: (row) =>
|
||||
h('div', { class: 'row-actions' }, [
|
||||
h(
|
||||
NButton,
|
||||
{
|
||||
quaternary: true,
|
||||
circle: true,
|
||||
size: 'small',
|
||||
disabled: row.status !== 'SUCCESS',
|
||||
title: '下载',
|
||||
onClick: () => downloadBackup(row),
|
||||
},
|
||||
{ icon: () => h(NIcon, null, { default: () => h(DownloadOutline) }) },
|
||||
),
|
||||
h(
|
||||
NButton,
|
||||
{ quaternary: true, circle: true, size: 'small', type: 'error', title: '删除', onClick: () => removeBackup(row) },
|
||||
{ icon: () => h(NIcon, null, { default: () => h(TrashOutline) }) },
|
||||
),
|
||||
]),
|
||||
},
|
||||
]);
|
||||
|
||||
function renderFilterTitle(label: string, value: string | null, options: { label: string; value: string }[], onSelect: (value: string | null) => void) {
|
||||
const dropdownOptions: DropdownOption[] = [{ label: '全部', key: '__all__' }, ...options.map((item) => ({ label: item.label, key: item.value }))];
|
||||
return h('div', { class: 'filter-title' }, [
|
||||
h('span', { class: 'filter-title-text' }, label),
|
||||
h(
|
||||
NDropdown,
|
||||
{ trigger: 'click', options: dropdownOptions, onSelect: (key) => onSelect(key === '__all__' ? null : String(key)) },
|
||||
{
|
||||
default: () =>
|
||||
h(
|
||||
NButton,
|
||||
{ quaternary: true, circle: true, size: 'tiny', class: ['filter-trigger', { active: !!value }], title: '筛选' },
|
||||
{ icon: () => h(NIcon, null, { default: () => h(FilterOutline) }) },
|
||||
),
|
||||
},
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
function setFilter(field: 'status' | 'mode', value: string | null) {
|
||||
query[field] = value;
|
||||
resetPage();
|
||||
void load();
|
||||
}
|
||||
|
||||
function formatSize(value: unknown) {
|
||||
const n = Number(value || 0);
|
||||
if (!n) return '-';
|
||||
if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`;
|
||||
return `${(n / 1024 / 1024).toFixed(1)} MB`;
|
||||
}
|
||||
|
||||
async function load() {
|
||||
loading.value = true;
|
||||
try {
|
||||
const res: any = await backupApi.list({
|
||||
status: query.status || undefined,
|
||||
mode: query.mode || undefined,
|
||||
page: page.value,
|
||||
pageSize: pageSize.value,
|
||||
});
|
||||
rows.value = res.list;
|
||||
setPageResult(res);
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function createBackup() {
|
||||
creating.value = true;
|
||||
try {
|
||||
await backupApi.create();
|
||||
message.success('备份已创建');
|
||||
await load();
|
||||
} finally {
|
||||
creating.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function downloadBackup(row: any) {
|
||||
await downloadFile(`/backups/${row.id}/download`, row.fileName || 'artveris-backup.zip');
|
||||
}
|
||||
|
||||
function removeBackup(row: any) {
|
||||
dialog.warning({
|
||||
title: '删除备份',
|
||||
content: '确认删除该备份记录和服务器上的备份包?',
|
||||
positiveText: '确认',
|
||||
negativeText: '取消',
|
||||
onPositiveClick: async () => {
|
||||
await backupApi.remove(row.id);
|
||||
message.success('已删除');
|
||||
await load();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function previewRestore({ file, onFinish, onError }: UploadCustomRequestOptions) {
|
||||
restoring.value = true;
|
||||
try {
|
||||
const fd = new FormData();
|
||||
fd.append('file', file.file as File);
|
||||
restorePreviewResult.value = await backupApi.restorePreview(fd);
|
||||
message.success('备份包预校验通过');
|
||||
await load();
|
||||
onFinish();
|
||||
} catch (e) {
|
||||
message.error((e as Error)?.message || '预校验失败');
|
||||
onError();
|
||||
} finally {
|
||||
restoring.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function commitRestore() {
|
||||
if (!restorePreviewResult.value?.restoreId) return;
|
||||
dialog.warning({
|
||||
title: '确认恢复',
|
||||
content: '恢复会覆盖当前数据库和上传文件。系统会先自动备份当前状态,确认继续?',
|
||||
positiveText: '确认恢复',
|
||||
negativeText: '取消',
|
||||
onPositiveClick: async () => {
|
||||
restoring.value = true;
|
||||
try {
|
||||
await backupApi.restoreCommit(restorePreviewResult.value.restoreId);
|
||||
restorePreviewResult.value = null;
|
||||
message.success('恢复完成,请重新检查数据');
|
||||
await load();
|
||||
} finally {
|
||||
restoring.value = false;
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
onMounted(load);
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.backup-panel {
|
||||
height: 100%;
|
||||
min-height: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
overflow: hidden;
|
||||
}
|
||||
.backup-toolbar {
|
||||
flex: 0 0 auto;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
flex-wrap: wrap;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
.backup-toolbar :deep(.n-upload) {
|
||||
width: auto;
|
||||
}
|
||||
.restore-alert {
|
||||
flex: 0 0 auto;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
.restore-content {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
.backup-table {
|
||||
flex: 1 1 auto;
|
||||
min-height: 0;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user