refactor: 合并档案与实物码管理入口
This commit is contained in:
@@ -0,0 +1,522 @@
|
||||
<template>
|
||||
<div :class="['trace-code-panel', { embedded }]">
|
||||
<div class="panel-toolbar">
|
||||
<n-select
|
||||
v-model:value="query.groupId"
|
||||
:options="groupFilterOptions"
|
||||
clearable
|
||||
:placeholder="t('pages.traceCode.allGroups')"
|
||||
class="toolbar-control"
|
||||
@update:value="onSearch"
|
||||
/>
|
||||
<n-select
|
||||
v-model:value="query.batchId"
|
||||
:options="batchOptions"
|
||||
clearable
|
||||
:placeholder="t('pages.traceCode.batchNo')"
|
||||
class="toolbar-control wide"
|
||||
@update:value="onSearch"
|
||||
/>
|
||||
<n-select
|
||||
v-model:value="query.status"
|
||||
:options="statusOptions"
|
||||
clearable
|
||||
:placeholder="t('common.label.status')"
|
||||
class="toolbar-control narrow"
|
||||
@update:value="onSearch"
|
||||
/>
|
||||
<n-input
|
||||
v-model:value="query.keyword"
|
||||
:placeholder="t('pages.traceCode.shortCode')"
|
||||
clearable
|
||||
class="toolbar-control"
|
||||
@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" @click="groupModal = true">
|
||||
<template #icon><n-icon :component="FolderOutline" /></template>
|
||||
{{ t('pages.traceCode.manageGroups') }}
|
||||
</n-button>
|
||||
<n-dropdown v-if="auth.canWrite" trigger="click" :options="exportOptions" @select="onExport">
|
||||
<n-button>
|
||||
<template #icon><n-icon :component="DownloadOutline" /></template>
|
||||
{{ t('common.action.export') }}
|
||||
</n-button>
|
||||
</n-dropdown>
|
||||
<n-button v-if="auth.canWrite" type="primary" @click="openGenerate">
|
||||
<template #icon><n-icon :component="AddOutline" /></template>
|
||||
{{ t('pages.traceCode.generate') }}
|
||||
</n-button>
|
||||
</div>
|
||||
|
||||
<n-card v-if="auth.canWrite && checkedKeys.length" size="small" class="batch-bar" :bordered="false">
|
||||
<n-space align="center">
|
||||
<span>{{ t('pages.traceCode.selected', { n: checkedKeys.length }) }}</span>
|
||||
<n-dropdown trigger="click" :options="groupAssignOptions" @select="batchAssignGroup">
|
||||
<n-button size="small">
|
||||
<template #icon><n-icon :component="FolderOutline" /></template>
|
||||
{{ t('pages.traceCode.batchGroup') }}
|
||||
</n-button>
|
||||
</n-dropdown>
|
||||
<n-button size="small" @click="batchStatus(false)">{{ t('pages.traceCode.batchEnable') }}</n-button>
|
||||
<n-button size="small" type="warning" @click="batchStatus(true)">{{ t('pages.traceCode.batchDisable') }}</n-button>
|
||||
</n-space>
|
||||
</n-card>
|
||||
|
||||
<n-data-table
|
||||
:columns="columns"
|
||||
:data="rows"
|
||||
:loading="loading"
|
||||
:pagination="pagination"
|
||||
remote
|
||||
:row-key="(r) => r.id"
|
||||
v-model:checked-row-keys="checkedKeys"
|
||||
@update:page="onPage"
|
||||
/>
|
||||
|
||||
<n-modal v-model:show="genModal" preset="card" :title="t('pages.traceCode.generate')" style="width: 440px">
|
||||
<n-form :model="genForm" label-placement="top">
|
||||
<n-form-item v-if="!fixedArtworkId" :label="t('pages.artwork.selectArtwork')" required>
|
||||
<n-select
|
||||
v-model:value="genForm.artworkId"
|
||||
filterable
|
||||
remote
|
||||
clearable
|
||||
:loading="artworkSearching"
|
||||
:options="artworkOptions"
|
||||
:placeholder="t('pages.artwork.selectArtworkTip')"
|
||||
@search="searchArtworks"
|
||||
/>
|
||||
</n-form-item>
|
||||
<n-form-item :label="t('pages.traceCode.quantity')">
|
||||
<n-input-number v-model:value="genForm.quantity" :min="1" :max="10000" style="width: 100%" />
|
||||
</n-form-item>
|
||||
<n-form-item :label="t('pages.traceCode.group')">
|
||||
<n-select v-model:value="genForm.groupId" :options="groupPlainOptions" clearable :placeholder="t('pages.traceCode.noGroup')" />
|
||||
</n-form-item>
|
||||
<n-form-item :label="t('pages.traceCode.batchNo')">
|
||||
<n-input v-model:value="genForm.batchNo" placeholder="(auto)" />
|
||||
</n-form-item>
|
||||
</n-form>
|
||||
<n-text v-if="generating" depth="3">{{ t('pages.traceCode.generating') }}</n-text>
|
||||
<template #footer>
|
||||
<div style="text-align: right">
|
||||
<n-button class="mr8" @click="genModal = false">{{ t('common.action.cancel') }}</n-button>
|
||||
<n-button type="primary" :loading="generating" @click="generate">{{ t('common.action.generate') }}</n-button>
|
||||
</div>
|
||||
</template>
|
||||
</n-modal>
|
||||
|
||||
<n-modal v-model:show="qrModal" preset="card" :title="t('pages.traceCode.qrPreview')" style="width: 360px">
|
||||
<div style="text-align: center">
|
||||
<img :src="qrSrc" alt="qrcode" style="width: 280px; height: 280px" />
|
||||
<div style="margin-top: 8px; opacity: 0.6; word-break: break-all">{{ qrUrl }}</div>
|
||||
</div>
|
||||
</n-modal>
|
||||
|
||||
<n-modal v-model:show="groupModal" preset="card" :title="t('pages.traceCode.manageGroups')" style="width: 520px">
|
||||
<div class="panel-toolbar group-toolbar">
|
||||
<n-input v-model:value="groupForm.name" :placeholder="t('pages.traceCode.groupName')" style="width: 200px" />
|
||||
<n-button type="primary" :disabled="!groupForm.name" @click="saveGroup">
|
||||
{{ editingGroup ? t('common.action.save') : t('pages.traceCode.createGroup') }}
|
||||
</n-button>
|
||||
<n-button v-if="editingGroup" @click="resetGroupForm">{{ t('common.action.cancel') }}</n-button>
|
||||
</div>
|
||||
<n-list bordered>
|
||||
<n-list-item v-for="g in groups" :key="g.id">
|
||||
<div class="group-row">
|
||||
<span>{{ g.name }}<n-text depth="3">({{ g._count?.traceCodes ?? 0 }})</n-text></span>
|
||||
<span>
|
||||
<n-button text type="primary" size="small" @click="editGroup(g)">{{ t('common.action.edit') }}</n-button>
|
||||
<n-button text type="error" size="small" style="margin-left: 10px" @click="removeGroup(g)">{{ t('common.action.delete') }}</n-button>
|
||||
</span>
|
||||
</div>
|
||||
</n-list-item>
|
||||
</n-list>
|
||||
</n-modal>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, h, onMounted, reactive, ref, watch } from 'vue';
|
||||
import { useRoute } from 'vue-router';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { NButton, NIcon, NTag, NText, type DataTableColumns } from 'naive-ui';
|
||||
import { SearchOutline, AddOutline, DownloadOutline, QrCodeOutline, FolderOutline, OpenOutline } from '@vicons/ionicons5';
|
||||
import { TraceCodeStatus } from '@artveris/shared';
|
||||
import { traceCodeApi, jobApi, artworkApi } from '@/api';
|
||||
import { useAuthStore } from '@/stores/auth';
|
||||
import { message, dialog } from '@/utils/feedback';
|
||||
import { fmt, statusType } from '@/utils/format';
|
||||
import { downloadFile } from '@/utils/download';
|
||||
|
||||
const props = defineProps<{
|
||||
artworkId?: number;
|
||||
embedded?: boolean;
|
||||
}>();
|
||||
|
||||
const { t } = useI18n();
|
||||
const route = useRoute();
|
||||
const auth = useAuthStore();
|
||||
|
||||
const fixedArtworkId = computed(() => props.artworkId || null);
|
||||
const embedded = computed(() => props.embedded === true);
|
||||
|
||||
const rows = ref<any[]>([]);
|
||||
const loading = ref(false);
|
||||
const checkedKeys = ref<Array<string | number>>([]);
|
||||
const query = reactive<{
|
||||
artworkId: number | null;
|
||||
groupId: number | null;
|
||||
batchId: number | null;
|
||||
status: string | null;
|
||||
keyword: string;
|
||||
page: number;
|
||||
pageSize: number;
|
||||
}>({
|
||||
artworkId: fixedArtworkId.value,
|
||||
groupId: null,
|
||||
batchId: null,
|
||||
status: null,
|
||||
keyword: '',
|
||||
page: 1,
|
||||
pageSize: embedded.value ? 8 : 10,
|
||||
});
|
||||
const pagination = reactive({ page: 1, pageSize: query.pageSize, itemCount: 0 });
|
||||
const statusOptions = Object.values(TraceCodeStatus).map((v) => ({ label: t(`common.status.${v}`), value: v }));
|
||||
|
||||
const batchOptions = ref<{ label: string; value: number }[]>([]);
|
||||
const groups = ref<any[]>([]);
|
||||
const groupFilterOptions = computed(() => groups.value.map((g) => ({ label: g.name, value: g.id })));
|
||||
const groupPlainOptions = computed(() => groups.value.map((g) => ({ label: g.name, value: g.id })));
|
||||
const groupAssignOptions = computed(() => [
|
||||
{ label: t('pages.traceCode.noGroup'), key: '__none__' },
|
||||
...groups.value.map((g) => ({ label: g.name, key: String(g.id) })),
|
||||
]);
|
||||
|
||||
const genModal = ref(false);
|
||||
const generating = ref(false);
|
||||
const genForm = reactive<{ artworkId: number | null; quantity: number; batchNo: string; groupId: number | null }>({
|
||||
artworkId: fixedArtworkId.value,
|
||||
quantity: 100,
|
||||
batchNo: '',
|
||||
groupId: null,
|
||||
});
|
||||
|
||||
const artworkSearching = ref(false);
|
||||
const artworkOptions = ref<{ label: string; value: number }[]>([]);
|
||||
let artworkTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
const qrModal = ref(false);
|
||||
const qrSrc = ref('');
|
||||
const qrUrl = ref('');
|
||||
|
||||
const groupModal = ref(false);
|
||||
const groupForm = reactive<{ name: string }>({ name: '' });
|
||||
const editingGroup = ref<number | null>(null);
|
||||
|
||||
const columns = computed<DataTableColumns<any>>(() => {
|
||||
const base: DataTableColumns<any> = [
|
||||
{ type: 'selection' },
|
||||
{ title: () => t('pages.traceCode.batchNo'), key: 'batch', width: 150, render: (r) => r.batch?.batchNo || '-' },
|
||||
{ title: () => t('pages.traceCode.shortCode'), key: 'shortCode', width: 120 },
|
||||
{
|
||||
title: () => t('pages.traceCode.url'),
|
||||
key: 'url',
|
||||
ellipsis: { tooltip: true },
|
||||
render: (r) =>
|
||||
h(
|
||||
'a',
|
||||
{ href: r.url, target: '_blank', rel: 'noopener', style: 'color: var(--brand); text-decoration: none' },
|
||||
[r.url, h(NIcon, { component: OpenOutline, style: 'vertical-align:-2px;margin-left:4px' })],
|
||||
),
|
||||
},
|
||||
{ title: () => t('pages.traceCode.group'), key: 'group', width: 110, render: (r) => r.group?.name || h(NText, { depth: 3 }, { default: () => t('pages.traceCode.noGroup') }) },
|
||||
{
|
||||
title: () => t('common.label.status'),
|
||||
key: 'status',
|
||||
width: 96,
|
||||
render: (r) => h(NTag, { size: 'small', type: statusType(r.status) }, { default: () => t(`common.status.${r.status}`) }),
|
||||
},
|
||||
{ title: () => t('pages.traceCode.queryCount'), key: 'queryCount', width: 84 },
|
||||
{ title: () => t('pages.traceCode.firstActivatedAt'), key: 'firstActivatedAt', width: 150, render: (r) => fmt(r.firstActivatedAt) },
|
||||
{
|
||||
title: () => t('common.label.operation'),
|
||||
key: 'ops',
|
||||
width: 70,
|
||||
render: (r) =>
|
||||
h(NButton, { quaternary: true, circle: true, size: 'small', onClick: () => showQr(r) }, { icon: () => h(NIcon, null, { default: () => h(QrCodeOutline) }) }),
|
||||
},
|
||||
];
|
||||
|
||||
if (!fixedArtworkId.value) {
|
||||
base.splice(
|
||||
4,
|
||||
0,
|
||||
{ title: () => t('pages.artwork.archiveNo'), key: 'archiveNo', width: 130, render: (r) => r.artwork?.archiveNo || '-' },
|
||||
{ title: () => t('pages.artwork.title'), key: 'artwork', width: 130, render: (r) => r.artwork?.title || '-' },
|
||||
);
|
||||
}
|
||||
return base;
|
||||
});
|
||||
|
||||
function currentArtworkId() {
|
||||
return fixedArtworkId.value || query.artworkId || null;
|
||||
}
|
||||
|
||||
async function loadBatches() {
|
||||
const params: Record<string, unknown> = { pageSize: 100 };
|
||||
const aid = currentArtworkId();
|
||||
if (aid) params.artworkId = aid;
|
||||
const res: any = await traceCodeApi.batches(params);
|
||||
batchOptions.value = res.list.map((b: any) => ({ label: `${b.batchNo} (${b.generatedCount}/${b.quantity})`, value: b.id }));
|
||||
}
|
||||
|
||||
async function loadGroups() {
|
||||
groups.value = await traceCodeApi.groups();
|
||||
}
|
||||
|
||||
async function load() {
|
||||
loading.value = true;
|
||||
checkedKeys.value = [];
|
||||
try {
|
||||
const res: any = await traceCodeApi.list({
|
||||
artworkId: currentArtworkId() || undefined,
|
||||
groupId: query.groupId || undefined,
|
||||
batchId: query.batchId || undefined,
|
||||
status: query.status || undefined,
|
||||
keyword: query.keyword || undefined,
|
||||
page: query.page,
|
||||
pageSize: query.pageSize,
|
||||
});
|
||||
rows.value = res.list;
|
||||
pagination.itemCount = res.total;
|
||||
pagination.page = res.page;
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function onSearch() {
|
||||
query.page = 1;
|
||||
load();
|
||||
}
|
||||
|
||||
function onPage(p: number) {
|
||||
query.page = p;
|
||||
load();
|
||||
}
|
||||
|
||||
function openGenerate() {
|
||||
genForm.artworkId = fixedArtworkId.value || query.artworkId;
|
||||
genModal.value = true;
|
||||
}
|
||||
|
||||
function searchArtworks(keyword: string) {
|
||||
if (artworkTimer) clearTimeout(artworkTimer);
|
||||
artworkTimer = setTimeout(async () => {
|
||||
artworkSearching.value = true;
|
||||
try {
|
||||
const res: any = await artworkApi.list({ keyword: keyword || undefined, pageSize: 20 });
|
||||
artworkOptions.value = res.list.map((a: any) => ({
|
||||
label: `${a.archiveNo || '-'} - ${a.title}`,
|
||||
value: a.id,
|
||||
}));
|
||||
} finally {
|
||||
artworkSearching.value = false;
|
||||
}
|
||||
}, 300);
|
||||
}
|
||||
|
||||
async function generate() {
|
||||
const targetArtworkId = fixedArtworkId.value || genForm.artworkId;
|
||||
if (!targetArtworkId) {
|
||||
message.error(t('pages.artwork.selectArtworkTip'));
|
||||
return;
|
||||
}
|
||||
generating.value = true;
|
||||
try {
|
||||
const res: any = await traceCodeApi.generate({
|
||||
artworkId: targetArtworkId,
|
||||
quantity: genForm.quantity,
|
||||
batchNo: genForm.batchNo || undefined,
|
||||
groupId: genForm.groupId || undefined,
|
||||
});
|
||||
message.success(t('pages.traceCode.genSuccess'));
|
||||
for (let i = 0; i < 30; i++) {
|
||||
await new Promise((r) => setTimeout(r, 700));
|
||||
const job: any = await jobApi.get(res.jobId);
|
||||
if (['SUCCESS', 'FAILED', 'PARTIAL'].includes(job.status)) break;
|
||||
}
|
||||
genModal.value = false;
|
||||
genForm.batchNo = '';
|
||||
await loadBatches();
|
||||
await load();
|
||||
} finally {
|
||||
generating.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function showQr(r: any) {
|
||||
qrSrc.value = traceCodeApi.qrUrl(r.id);
|
||||
qrUrl.value = r.url;
|
||||
qrModal.value = true;
|
||||
}
|
||||
|
||||
const exportOptions = computed(() => {
|
||||
const cur = t('pages.traceCode.exportCurrent');
|
||||
const opts: Array<{ label: string; key: string }> = [
|
||||
{ label: `Excel - ${cur}`, key: 'filter:xlsx' },
|
||||
{ label: `CSV - ${cur}`, key: 'filter:csv' },
|
||||
];
|
||||
if (checkedKeys.value.length) {
|
||||
const sel = t('pages.traceCode.selected', { n: checkedKeys.value.length });
|
||||
opts.push({ label: `Excel - ${sel}`, key: 'selected:xlsx' });
|
||||
opts.push({ label: `CSV - ${sel}`, key: 'selected:csv' });
|
||||
}
|
||||
return opts;
|
||||
});
|
||||
|
||||
async function onExport(key: string) {
|
||||
const [scope, format] = key.split(':');
|
||||
const params: Record<string, unknown> = { format };
|
||||
if (scope === 'selected') {
|
||||
params.ids = checkedKeys.value.map(String).join(',');
|
||||
} else {
|
||||
const aid = currentArtworkId();
|
||||
if (aid) params.artworkId = aid;
|
||||
if (query.groupId) params.groupId = query.groupId;
|
||||
if (query.batchId) params.batchId = query.batchId;
|
||||
if (query.status) params.status = query.status;
|
||||
if (query.keyword) params.keyword = query.keyword;
|
||||
}
|
||||
await downloadFile('/trace-codes/export', `trace-codes.${format}`, params);
|
||||
}
|
||||
|
||||
async function batchAssignGroup(key: string) {
|
||||
const groupId = key === '__none__' ? null : Number(key);
|
||||
await traceCodeApi.batchGroup(checkedKeys.value.map(String), groupId);
|
||||
message.success(t('common.msg.operationSuccess'));
|
||||
await load();
|
||||
}
|
||||
|
||||
async function batchStatus(disabled: boolean) {
|
||||
await traceCodeApi.batchStatus(checkedKeys.value.map(String), disabled);
|
||||
message.success(t('common.msg.operationSuccess'));
|
||||
await load();
|
||||
}
|
||||
|
||||
function resetGroupForm() {
|
||||
editingGroup.value = null;
|
||||
groupForm.name = '';
|
||||
}
|
||||
|
||||
function editGroup(g: any) {
|
||||
editingGroup.value = g.id;
|
||||
groupForm.name = g.name;
|
||||
}
|
||||
|
||||
async function saveGroup() {
|
||||
if (editingGroup.value) {
|
||||
await traceCodeApi.updateGroup(editingGroup.value, { name: groupForm.name });
|
||||
} else {
|
||||
await traceCodeApi.createGroup({ name: groupForm.name });
|
||||
}
|
||||
message.success(t('common.msg.saved'));
|
||||
resetGroupForm();
|
||||
await loadGroups();
|
||||
}
|
||||
|
||||
function removeGroup(g: 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 traceCodeApi.removeGroup(g.id);
|
||||
message.success(t('common.msg.deleted'));
|
||||
await loadGroups();
|
||||
await load();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
watch(
|
||||
() => fixedArtworkId.value,
|
||||
async (aid) => {
|
||||
query.artworkId = aid;
|
||||
genForm.artworkId = aid;
|
||||
query.batchId = null;
|
||||
query.page = 1;
|
||||
await loadBatches();
|
||||
await load();
|
||||
},
|
||||
);
|
||||
|
||||
onMounted(async () => {
|
||||
if (!fixedArtworkId.value) {
|
||||
const aid = Number(route.query.artworkId);
|
||||
if (aid) {
|
||||
query.artworkId = aid;
|
||||
genForm.artworkId = aid;
|
||||
}
|
||||
}
|
||||
await Promise.all([loadBatches(), loadGroups()]);
|
||||
await load();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.trace-code-panel {
|
||||
min-width: 0;
|
||||
}
|
||||
.panel-toolbar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
flex-wrap: wrap;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
.toolbar-control {
|
||||
width: 170px;
|
||||
}
|
||||
.toolbar-control.wide {
|
||||
width: 210px;
|
||||
}
|
||||
.toolbar-control.narrow {
|
||||
width: 130px;
|
||||
}
|
||||
.spacer {
|
||||
flex: 1 1 auto;
|
||||
}
|
||||
.batch-bar {
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
.group-toolbar {
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
.group-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
width: 100%;
|
||||
}
|
||||
.mr8 {
|
||||
margin-right: 8px;
|
||||
}
|
||||
@media (max-width: 720px) {
|
||||
.toolbar-control,
|
||||
.toolbar-control.wide,
|
||||
.toolbar-control.narrow {
|
||||
width: 100%;
|
||||
}
|
||||
.panel-toolbar :deep(.n-button) {
|
||||
flex: 1 1 auto;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user