fix: 修复出库追溯码默认加载

This commit is contained in:
Nixevol
2026-07-03 04:35:05 +08:00
parent 9eda1b722a
commit bc6a4e9cc2
5 changed files with 109 additions and 30 deletions
+79 -21
View File
@@ -93,10 +93,11 @@
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue';
import { onBeforeUnmount, onMounted, reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import type { UploadCustomRequestOptions } from 'naive-ui';
import { DownloadOutline, CloudUploadOutline } from '@vicons/ionicons5';
import dayjs from 'dayjs';
import { jobApi, outboundApi, traceCodeApi, post } from '@/api';
import { useAuthStore } from '@/stores/auth';
import MediaUpload from '@/components/MediaUpload.vue';
@@ -110,7 +111,7 @@ const auth = useAuthStore();
/* 单条录入 */
const saving = ref(false);
const searching = ref(false);
const codeOptions = ref<{ label: string; value: string }[]>([]);
const codeOptions = ref<{ label: string; value: string; raw: any }[]>([]);
const outImages = ref<any[]>([]);
const form = reactive<any>({
traceCodeId: null,
@@ -122,39 +123,88 @@ const form = reactive<any>({
});
let searchTimer: ReturnType<typeof setTimeout> | null = null;
let searchSeq = 0;
function formatDateTime(value?: string | Date | null) {
return value ? dayjs(value).format('YYYY-MM-DD HH:mm') : null;
}
function toNumber(value: unknown) {
if (value === null || value === undefined || value === '') return null;
const n = Number(value);
return Number.isFinite(n) ? n : null;
}
function fileName(key: string) {
return key.split('/').pop() || key;
}
function codeLabel(c: any) {
return [
c.shortCode,
c.artwork?.archiveNo,
c.artwork?.title,
c.batch?.batchNo,
].filter(Boolean).join(' · ');
}
function resetCodeDetail() {
form.outTime = null;
form.remark = '';
form.txHash = '';
form.onchainAt = null;
form.blockHeight = null;
outImages.value = [];
}
async function loadCodeOptions(keyword = '') {
const seq = ++searchSeq;
searching.value = true;
try {
const res: any = await traceCodeApi.list({ keyword: keyword || undefined, pageSize: 20 });
if (seq !== searchSeq) return;
codeOptions.value = res.list.map((c: any) => ({
label: codeLabel(c),
value: String(c.id),
raw: c,
}));
} finally {
if (seq === searchSeq) searching.value = false;
}
}
function searchCodes(keyword: string) {
if (searchTimer) clearTimeout(searchTimer);
searchTimer = setTimeout(async () => {
searching.value = true;
try {
const res: any = await traceCodeApi.list({ keyword: keyword || undefined, pageSize: 20 });
codeOptions.value = res.list.map((c: any) => ({
label: `${c.shortCode}${c.artwork ? ' · ' + (c.artwork.archiveNo || c.artwork.title) : ''}`,
value: String(c.id),
raw: c,
}));
} finally {
searching.value = false;
}
}, 300);
searchTimer = setTimeout(() => loadCodeOptions(keyword), 300);
}
function onPickCode(_id: string, opt: any) {
function onPickCode(id: string | null, opt: any) {
if (!id) {
resetCodeDetail();
return;
}
const c = opt?.raw;
if (!c) return;
const latestProof = c.latestProof;
form.outTime = formatDateTime(c.outTime);
form.remark = c.remark || '';
outImages.value = c.outImage ? [{ key: c.outImage, url: c.outImage, name: c.outImage }] : [];
form.txHash = latestProof?.txHash || '';
form.onchainAt = formatDateTime(latestProof?.onchainAt);
form.blockHeight = toNumber(latestProof?.blockHeight);
outImages.value = c.outImage ? [{ key: c.outImage, url: c.outImageUrl || c.outImage, name: fileName(c.outImage) }] : [];
}
async function saveSingle() {
if (!form.traceCodeId) return;
saving.value = true;
try {
await outboundApi.updateSingle(form.traceCodeId, {
outTime: form.outTime ? new Date(form.outTime).toISOString() : undefined,
outImageKey: outImages.value[0]?.key || undefined,
outTime: form.outTime ? dayjs(form.outTime).toISOString() : null,
outImageKey: outImages.value[0]?.key ?? null,
txHash: form.txHash || undefined,
onchainAt: form.onchainAt ? new Date(form.onchainAt).toISOString() : undefined,
onchainAt: form.onchainAt ? dayjs(form.onchainAt).toISOString() : undefined,
blockHeight: form.blockHeight ?? undefined,
remark: form.remark || undefined,
remark: form.remark || null,
});
message.success(t('common.msg.saved'));
} finally {
@@ -193,4 +243,12 @@ async function doImport({ file, onFinish, onError }: UploadCustomRequestOptions)
importing.value = false;
}
}
onMounted(() => {
loadCodeOptions();
});
onBeforeUnmount(() => {
if (searchTimer) clearTimeout(searchTimer);
});
</script>