Files
Artveris/apps/admin/src/views/Templates.vue
T

339 lines
12 KiB
Vue

<template>
<div class="page table-page">
<div class="page-toolbar">
<n-input
v-model:value="query.keyword"
:placeholder="t('common.label.keyword')"
clearable
style="width: 220px"
@keyup.enter="onSearch"
/>
<n-button type="primary" @click="onSearch">
<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="false"
class="table-fill"
flex-height
remote
:scroll-x="1120"
:row-key="(r) => r.id"
@update:sorter="onSorterChange"
/>
<div class="table-pagination">
<n-pagination v-bind="pagination" :disabled="loading" />
</div>
<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 v-if="editing" :label="t('common.label.status')">
<n-switch v-model:value="form.status" :checked-value="'ENABLED'" :unchecked-value="'DISABLED'">
<template #checked>{{ t('common.status.ENABLED') }}</template>
<template #unchecked>{{ t('common.status.DISABLED') }}</template>
</n-switch>
</n-form-item>
<n-form-item :label="t('pages.template.fields')">
<div class="field-editor">
<div class="field-hint">{{ t('pages.template.sortHint') }}</div>
<div v-for="(f, i) in form.fields" :key="i" class="field-row">
<span class="field-order">{{ i + 1 }}</span>
<n-input v-model:value="f.fieldKey" :placeholder="t('pages.template.fieldKeyTip')" style="width: 120px" />
<n-input v-model:value="f.label" :placeholder="t('pages.template.fieldLabel')" style="width: 110px" />
<n-select v-model:value="f.type" :options="fieldTypeOptions" style="width: 110px" />
<n-checkbox v-model:checked="f.required">{{ t('pages.template.required') }}</n-checkbox>
<span class="field-ops">
<n-button quaternary circle size="small" :disabled="i === 0" @click="moveField(i, -1)">
<template #icon><n-icon :component="ArrowUpOutline" /></template>
</n-button>
<n-button quaternary circle size="small" :disabled="i === form.fields.length - 1" @click="moveField(i, 1)">
<template #icon><n-icon :component="ArrowDownOutline" /></template>
</n-button>
<n-button quaternary circle type="error" size="small" @click="form.fields.splice(i, 1)">
<template #icon><n-icon :component="TrashOutline" /></template>
</n-button>
</span>
</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,
ArrowUpOutline,
ArrowDownOutline,
PowerOutline,
} from '@vicons/ionicons5';
import { FieldType, TemplateStatus } from '@artveris/shared';
import { templateApi } from '@/api';
import { useAuthStore } from '@/stores/auth';
import { message, dialog } from '@/utils/feedback';
import { fmt, statusType } from '@/utils/format';
import { useTablePage } from '@/composables/useTablePage';
import { renderTableFilterTitle } from '@/utils/tableFilter';
import { useRemoteSort } from '@/utils/tableSort';
const { t } = useI18n();
const auth = useAuthStore();
const rows = ref<any[]>([]);
const loading = ref(false);
const query = reactive<{ keyword: string; status: string | null }>({ keyword: '', status: null });
const { pagination, page, pageSize, resetPage, setPageResult } = useTablePage(load);
const { sortProps, onSorterChange, sortParams } = useRemoteSort(onSearch);
const fieldTypeOptions = Object.values(FieldType).map((v) => ({ label: t(`common.fieldType.${v}`), value: v }));
const statusOptions = Object.values(TemplateStatus).map((v) => ({ label: t(`common.status.${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; status: string; fields: any[] }>({ name: '', remark: '', status: 'ENABLED', fields: [] });
const rules: FormRules = { name: { required: true, message: () => t('pages.template.namePlaceholder'), trigger: 'blur' } };
const columns: DataTableColumns<any> = [
{
title: 'ID',
key: 'id',
width: 70,
align: 'center',
render: (r) => h(NButton, { text: true, type: 'primary', title: t('pages.template.edit'), onClick: () => openEdit(r) }, { default: () => String(r.id) }),
},
{
title: () => t('pages.template.name'),
key: 'name',
minWidth: 220,
ellipsis: { tooltip: true },
render: (r) => h(NButton, { text: true, type: 'primary', title: t('pages.template.edit'), onClick: () => openEdit(r) }, { default: () => r.name || '-' }),
},
{
title: () => t('pages.template.fields'),
key: 'fields',
minWidth: 380,
ellipsis: { tooltip: true },
render: (r) => (r.fields || []).map((f: any) => f.label).join('、') || '-',
},
{
title: () => renderTableFilterTitle(t('common.label.status'), query.status, statusOptions, (value) => setStatusFilter(value)),
key: 'status',
width: 96,
align: 'center',
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, align: 'center', render: (r) => r._count?.artworks ?? 0 },
{ title: () => t('common.label.createdAt'), key: 'createdAt', width: 160, align: 'center', render: (r) => fmt(r.createdAt), ...sortProps('createdAt') },
{
title: () => t('common.label.operation'),
key: 'ops',
width: 130,
fixed: 'right',
align: 'center',
render: (r) =>
auth.canWrite
? h('div', { class: 'row-actions' }, [
h(
NButton,
{ quaternary: true, circle: true, size: 'small', title: t('common.action.edit'), onClick: () => openEdit(r) },
{ icon: () => h(NIcon, null, { default: () => h(CreateOutline) }) },
),
h(
NButton,
{
quaternary: true,
circle: true,
size: 'small',
title: r.status === 'ENABLED' ? t('common.status.DISABLED') : t('common.status.ENABLED'),
type: r.status === 'ENABLED' ? 'warning' : 'success',
onClick: () => toggleStatus(r),
},
{ icon: () => h(NIcon, null, { default: () => h(PowerOutline) }) },
),
h(
NButton,
{ quaternary: true, circle: true, size: 'small', type: 'error', title: t('common.action.delete'), onClick: () => remove(r) },
{ icon: () => h(NIcon, null, { default: () => h(TrashOutline) }) },
),
])
: '-',
},
];
async function toggleStatus(r: any) {
const next = r.status === TemplateStatus.ENABLED ? TemplateStatus.DISABLED : TemplateStatus.ENABLED;
await templateApi.update(r.id, { status: next });
message.success(t('common.msg.updated'));
load();
}
async function load() {
loading.value = true;
try {
const res: any = await templateApi.list({
keyword: query.keyword,
status: query.status || undefined,
page: page.value,
pageSize: pageSize.value,
...sortParams(),
});
rows.value = res.list;
setPageResult(res);
} finally {
loading.value = false;
}
}
function onSearch() {
resetPage();
load();
}
function setStatusFilter(value: string | number | null) {
query.status = value ? String(value) : null;
onSearch();
}
function addField() {
form.fields.push({ fieldKey: '', label: '', type: FieldType.TEXT, required: false });
}
function moveField(i: number, dir: -1 | 1) {
const j = i + dir;
if (j < 0 || j >= form.fields.length) return;
const arr = form.fields;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
function openCreate() {
editing.value = null;
form.name = '';
form.remark = '';
form.status = 'ENABLED';
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.status = r.status || 'ENABLED';
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 {
// 排序:按当前顺序写 sortOrder
const fields = form.fields.map((f, i) => ({ ...f, sortOrder: i }));
const payload: any = { name: form.name, remark: form.remark, fields };
if (editing.value) payload.status = form.status;
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;
min-width: 560px;
}
.field-editor {
width: 100%;
overflow-x: auto;
padding-bottom: 2px;
}
.field-order {
width: 18px;
text-align: center;
color: #9ca3af;
font-size: 12px;
flex-shrink: 0;
}
.field-ops {
display: inline-flex;
gap: 2px;
}
.field-hint {
font-size: 12px;
color: #9ca3af;
margin-bottom: 8px;
}
.mr8 {
margin-right: 8px;
}
</style>