feat: 实现后台管理前端全部页面与登录布局

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Nixevol
2026-06-28 16:52:41 +08:00
co-authored by Cursor
parent fa61840cfb
commit 5fa84f346b
40 changed files with 4481 additions and 1 deletions
+226
View File
@@ -0,0 +1,226 @@
<template>
<div class="page">
<div class="page-toolbar">
<n-input
v-model:value="query.keyword"
:placeholder="t('common.label.keyword')"
clearable
style="width: 220px"
@keyup.enter="load"
/>
<n-button type="primary" @click="load">
<template #icon><n-icon :component="SearchOutline" /></template>
{{ t('common.action.search') }}
</n-button>
<div class="spacer" />
<n-button v-if="auth.canWrite" type="primary" @click="openCreate">
<template #icon><n-icon :component="AddOutline" /></template>
{{ t('pages.template.create') }}
</n-button>
</div>
<n-data-table
:columns="columns"
:data="rows"
:loading="loading"
:pagination="pagination"
remote
:row-key="(r) => r.id"
@update:page="onPage"
/>
<n-modal
v-model:show="showModal"
preset="card"
:title="editing ? t('pages.template.edit') : t('pages.template.create')"
style="width: 720px; max-width: 94vw"
>
<n-form ref="formRef" :model="form" :rules="rules">
<n-form-item path="name" :label="t('pages.template.name')">
<n-input v-model:value="form.name" :placeholder="t('pages.template.namePlaceholder')" />
</n-form-item>
<n-form-item :label="t('common.label.remark')">
<n-input v-model:value="form.remark" type="textarea" :rows="2" />
</n-form-item>
<n-form-item :label="t('pages.template.fields')">
<div style="width: 100%">
<div v-for="(f, i) in form.fields" :key="i" class="field-row">
<n-input v-model:value="f.fieldKey" :placeholder="t('pages.template.fieldKeyTip')" style="width: 130px" />
<n-input v-model:value="f.label" :placeholder="t('pages.template.fieldLabel')" style="width: 120px" />
<n-select v-model:value="f.type" :options="fieldTypeOptions" style="width: 120px" />
<n-checkbox v-model:checked="f.required">{{ t('pages.template.required') }}</n-checkbox>
<n-button quaternary circle type="error" @click="form.fields.splice(i, 1)">
<template #icon><n-icon :component="TrashOutline" /></template>
</n-button>
</div>
<n-button dashed block @click="addField">
<template #icon><n-icon :component="AddOutline" /></template>
{{ t('pages.template.addField') }}
</n-button>
</div>
</n-form-item>
</n-form>
<template #footer>
<div style="text-align: right">
<n-button class="mr8" @click="showModal = false">{{ t('common.action.cancel') }}</n-button>
<n-button type="primary" :loading="saving" @click="save">{{ t('common.action.save') }}</n-button>
</div>
</template>
</n-modal>
</div>
</template>
<script setup lang="ts">
import { h, onMounted, reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { NButton, NIcon, NTag, type DataTableColumns, type FormInst, type FormRules } from 'naive-ui';
import { SearchOutline, AddOutline, TrashOutline, CreateOutline } from '@vicons/ionicons5';
import { FieldType } from '@artveris/shared';
import { templateApi } from '@/api';
import { useAuthStore } from '@/stores/auth';
import { message, dialog } from '@/utils/feedback';
import { fmt, statusType } from '@/utils/format';
const { t } = useI18n();
const auth = useAuthStore();
const rows = ref<any[]>([]);
const loading = ref(false);
const query = reactive({ keyword: '', page: 1, pageSize: 10 });
const pagination = reactive({ page: 1, pageCount: 1, pageSize: 10, itemCount: 0 });
const fieldTypeOptions = Object.values(FieldType).map((v) => ({ label: t(`common.fieldType.${v}`), value: v }));
const showModal = ref(false);
const editing = ref<number | null>(null);
const saving = ref(false);
const formRef = ref<FormInst | null>(null);
const form = reactive<{ name: string; remark: string; fields: any[] }>({ name: '', remark: '', fields: [] });
const rules: FormRules = { name: { required: true, message: () => t('pages.template.namePlaceholder'), trigger: 'blur' } };
const columns: DataTableColumns<any> = [
{ title: 'ID', key: 'id', width: 70 },
{ title: () => t('pages.template.name'), key: 'name' },
{
title: () => t('pages.template.fields'),
key: 'fields',
render: (r) => (r.fields || []).map((f: any) => f.label).join('、') || '-',
},
{
title: () => t('common.label.status'),
key: 'status',
width: 90,
render: (r) => h(NTag, { type: statusType(r.status), size: 'small' }, { default: () => t(`common.status.${r.status}`) }),
},
{ title: () => t('pages.template.useCount'), key: 'useCount', width: 100, render: (r) => r._count?.artworks ?? 0 },
{ title: () => t('common.label.createdAt'), key: 'createdAt', width: 160, render: (r) => fmt(r.createdAt) },
{
title: () => t('common.label.operation'),
key: 'ops',
width: 130,
render: (r) =>
auth.canWrite
? h('div', { style: 'display:flex;gap:4px' }, [
h(
NButton,
{ quaternary: true, circle: true, size: 'small', onClick: () => openEdit(r) },
{ icon: () => h(NIcon, null, { default: () => h(CreateOutline) }) },
),
h(
NButton,
{ quaternary: true, circle: true, size: 'small', type: 'error', onClick: () => remove(r) },
{ icon: () => h(NIcon, null, { default: () => h(TrashOutline) }) },
),
])
: '-',
},
];
async function load() {
loading.value = true;
try {
const res: any = await templateApi.list({ keyword: query.keyword, page: query.page, pageSize: query.pageSize });
rows.value = res.list;
pagination.itemCount = res.total;
pagination.page = res.page;
pagination.pageSize = res.pageSize;
} finally {
loading.value = false;
}
}
function onPage(p: number) {
query.page = p;
load();
}
function addField() {
form.fields.push({ fieldKey: '', label: '', type: FieldType.TEXT, required: false });
}
function openCreate() {
editing.value = null;
form.name = '';
form.remark = '';
form.fields = [{ fieldKey: '', label: '', type: FieldType.TEXT, required: false }];
showModal.value = true;
}
function openEdit(r: any) {
editing.value = r.id;
form.name = r.name;
form.remark = r.remark || '';
form.fields = (r.fields || []).map((f: any) => ({ ...f }));
showModal.value = true;
}
async function save() {
try {
await formRef.value?.validate();
} catch {
return;
}
if (!form.fields.length || form.fields.some((f) => !f.fieldKey || !f.label)) {
message.error(t('pages.template.fields') + ' ✕');
return;
}
saving.value = true;
try {
const payload = { name: form.name, remark: form.remark, fields: form.fields };
if (editing.value) {
await templateApi.update(editing.value, payload);
message.success(t('common.msg.updated'));
} else {
await templateApi.create(payload);
message.success(t('common.msg.created'));
}
showModal.value = false;
load();
} finally {
saving.value = false;
}
}
function remove(r: any) {
dialog.warning({
title: t('common.action.delete'),
content: t('common.msg.confirmDelete'),
positiveText: t('common.action.confirm'),
negativeText: t('common.action.cancel'),
onPositiveClick: async () => {
await templateApi.remove(r.id);
message.success(t('common.msg.deleted'));
load();
},
});
}
onMounted(load);
</script>
<style scoped>
.field-row {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
}
.mr8 {
margin-right: 8px;
}
</style>