diff --git a/apps/admin/src/components/TraceCodePanel.vue b/apps/admin/src/components/TraceCodePanel.vue index 958ff07..396a63a 100644 --- a/apps/admin/src/components/TraceCodePanel.vue +++ b/apps/admin/src/components/TraceCodePanel.vue @@ -258,6 +258,8 @@ const query = reactive<{ batchId: number | null; status: string | null; outboundStatus: string | null; + // 是否激活过:YES 有激活时间,NO 从未激活。与看板「已激活追溯码」同一口径 + activated: string | null; keyword: string; }>({ artworkId: fixedArtworkId.value, @@ -265,6 +267,7 @@ const query = reactive<{ batchId: null, status: null, outboundStatus: null, + activated: null, keyword: '', }); const { pagination, page, pageSize, resetPage, setPageResult } = useTablePage(load, embedded.value ? 20 : 20); @@ -274,6 +277,11 @@ const outboundStatusOptions = computed(() => [ { label: t('pages.traceCode.outRegistered'), value: 'OUTBOUND' }, { label: t('pages.traceCode.notOutbound'), value: 'NOT_OUTBOUND' }, ]); +const activatedOptions = computed(() => [ + { label: t('pages.traceCode.everActivated'), value: 'YES' }, + { label: t('pages.traceCode.neverActivated'), value: 'NO' }, +]); +const activatedParam = () => (query.activated === 'YES' ? true : query.activated === 'NO' ? false : undefined); const batchOptions = ref<{ label: string; value: number }[]>([]); const batchMoveOptions = computed(() => batchOptions.value.map((b) => ({ label: b.label, value: b.value }))); @@ -471,7 +479,14 @@ const columns = computed>(() => { // 查询次数 { title: () => t('pages.traceCode.queryCount'), key: 'queryCount', width: 100, align: 'center' }, // 激活时间 - { title: () => t('pages.traceCode.firstActivatedAt'), key: 'firstActivatedAt', width: 150, align: 'center', render: (r) => fmt(r.firstActivatedAt), ...sortProps('firstActivatedAt') }, + { + title: () => renderFilterTitle(t('pages.traceCode.firstActivatedAt'), query.activated, activatedOptions.value, (value) => setHeaderFilter('activated', value)), + key: 'firstActivatedAt', + width: 170, + align: 'center', + render: (r) => fmt(r.firstActivatedAt), + ...sortProps('firstActivatedAt'), + }, // 链接:拦截直跳,先告知「直接访问会激活/计数」,再让用户选预览或实际访问 { title: () => t('pages.traceCode.url'), @@ -592,7 +607,7 @@ function confirmVisitReal(row: any) { }); } -type HeaderFilterField = 'batchId' | 'groupId' | 'status' | 'outboundStatus'; +type HeaderFilterField = 'batchId' | 'groupId' | 'status' | 'outboundStatus' | 'activated'; type HeaderFilterValue = string | number | null; type HeaderFilterOption = { label: string; value: string | number }; @@ -626,6 +641,8 @@ function renderFilterTitle( size: 'tiny', class: ['filter-trigger', { active }], title: t('pages.traceCode.tableFilter'), + // 可排序的列点表头会切换排序,点筛选按钮时不要连带触发 + onClick: (event: MouseEvent) => event.stopPropagation(), }, { icon: () => h(NIcon, null, { default: () => h(FilterOutline) }) }, ), @@ -673,6 +690,7 @@ async function load() { batchId: query.batchId || undefined, status: query.status || undefined, outboundStatus: query.outboundStatus || undefined, + activated: activatedParam(), keyword: query.keyword || undefined, page: page.value, pageSize: pageSize.value, @@ -759,6 +777,7 @@ const hasActiveExportFilter = computed(() => Boolean( query.batchId || query.status || query.outboundStatus || + query.activated || (!fixedArtworkId.value && query.artworkId), )); @@ -781,6 +800,7 @@ async function onExport() { if (query.batchId) params.batchId = query.batchId; if (query.status) params.status = query.status; if (query.outboundStatus) params.outboundStatus = query.outboundStatus; + if (query.activated) params.activated = activatedParam(); if (query.keyword) params.keyword = query.keyword; } exporting.value = true; @@ -1142,6 +1162,7 @@ onMounted(async () => { genForm.artworkId = aid; } if (typeof route.query.status === 'string') query.status = route.query.status; + if (route.query.activated === '1') query.activated = 'YES'; } await Promise.all([loadBatches(), loadGroups()]); await load(); diff --git a/apps/admin/src/locales/en-US.json b/apps/admin/src/locales/en-US.json index 07498a3..b993a9c 100644 --- a/apps/admin/src/locales/en-US.json +++ b/apps/admin/src/locales/en-US.json @@ -478,6 +478,8 @@ "reconfirmOnchain": "Reconfirm", "confirmSubmitted": "Reconfirm task submitted", "tableFilter": "Filter", + "everActivated": "Ever activated", + "neverActivated": "Never activated", "onchainInfo": "On-chain Info", "enableOne": "Enable", "disableOne": "Disable", diff --git a/apps/admin/src/locales/zh-CN.json b/apps/admin/src/locales/zh-CN.json index 31c5f14..867fdbc 100644 --- a/apps/admin/src/locales/zh-CN.json +++ b/apps/admin/src/locales/zh-CN.json @@ -478,6 +478,8 @@ "reconfirmOnchain": "重新确认", "confirmSubmitted": "已提交重新确认任务", "tableFilter": "筛选", + "everActivated": "激活过", + "neverActivated": "从未激活", "onchainInfo": "上链信息", "enableOne": "启用", "disableOne": "禁用", diff --git a/apps/admin/src/views/Dashboard.vue b/apps/admin/src/views/Dashboard.vue index aa843c0..2d4f9c7 100644 --- a/apps/admin/src/views/Dashboard.vue +++ b/apps/admin/src/views/Dashboard.vue @@ -90,7 +90,7 @@ const range = ref<[number, number] | null>(null); const stats = computed(() => [ { key: 'totalArtworks', label: t('pages.dashboard.totalArtworks'), icon: ImagesOutline, color: '#1f3864', open: () => router.push('/artworks') }, { key: 'totalCodes', label: t('pages.dashboard.totalCodes'), icon: QrCodeOutline, color: '#2e5496', open: () => router.push('/trace-codes') }, - { key: 'activatedCodes', label: t('pages.dashboard.activatedCodes'), icon: CheckmarkCircleOutline, color: '#138a72', open: () => router.push({ path: '/trace-codes', query: { status: 'ACTIVE' } }) }, + { key: 'activatedCodes', label: t('pages.dashboard.activatedCodes'), icon: CheckmarkCircleOutline, color: '#138a72', open: () => router.push({ path: '/trace-codes', query: { activated: '1' } }) }, { key: 'totalQueries', label: t('pages.dashboard.totalQueries'), icon: SearchOutline, color: '#3a7bd5', open: () => router.push('/query-logs') }, { key: 'todayQueries', label: t('pages.dashboard.todayQueries'), icon: TodayOutline, color: '#b9851f', open: () => router.push({ path: '/query-logs', query: { quickRange: 'today' } }) }, { key: 'abnormalQueries', label: t('pages.dashboard.abnormalQueries'), icon: WarningOutline, color: '#c0392b', open: () => router.push({ path: '/query-logs', query: { abnormal: '1' } }) }, diff --git a/apps/api/src/common/dto/trace-code-selection.dto.spec.ts b/apps/api/src/common/dto/trace-code-selection.dto.spec.ts index f3b380e..cdc39bd 100644 --- a/apps/api/src/common/dto/trace-code-selection.dto.spec.ts +++ b/apps/api/src/common/dto/trace-code-selection.dto.spec.ts @@ -1,12 +1,13 @@ import { plainToInstance } from 'class-transformer'; import { TraceCodeFilterDto } from './trace-code-selection.dto'; -import { QueryTraceCodeDto } from '../../trace-code/dto/trace-code.dto'; +import { ExportTraceCodeDto, QueryTraceCodeDto } from '../../trace-code/dto/trace-code.dto'; import { buildTraceCodeFilterWhere, buildTraceCodeSelectionWhere } from '../utils/trace-code-selection.util'; describe('activated query transformation', () => { it.each([ [TraceCodeFilterDto, {}], [QueryTraceCodeDto, {}], + [ExportTraceCodeDto, {}], ])('does not turn an omitted activated filter into false', (Dto, input) => { const dto = plainToInstance(Dto, input); expect(dto.activated).toBeUndefined(); @@ -21,6 +22,28 @@ describe('activated query transformation', () => { ])('parses %p as %p', (input, expected) => { expect(plainToInstance(TraceCodeFilterDto, { activated: input }).activated).toBe(expected); expect(plainToInstance(QueryTraceCodeDto, { activated: input }).activated).toBe(expected); + expect(plainToInstance(ExportTraceCodeDto, { activated: input }).activated).toBe(expected); + }); + + // 与 main.ts 的全局校验管道一致:查询字符串会先做隐式类型转换,'false' 不能因此变成 true + it.each([ + ['true', true], + ['false', false], + ])('parses the query string %p as %p under the global pipe options', (input, expected) => { + for (const Dto of [TraceCodeFilterDto, QueryTraceCodeDto, ExportTraceCodeDto]) { + const dto = plainToInstance(Dto, { activated: input }, { enableImplicitConversion: true }); + expect(dto.activated).toBe(expected); + } + }); + + it('lists only never-activated codes for activated=false', () => { + const dto = plainToInstance(QueryTraceCodeDto, { activated: 'false' }, { enableImplicitConversion: true }); + expect(buildTraceCodeFilterWhere(dto)).toMatchObject({ firstActivatedAt: null }); + }); + + it('exports only the codes that were ever activated when that filter is on', () => { + const dto = plainToInstance(ExportTraceCodeDto, { activated: 'true' }); + expect(buildTraceCodeFilterWhere(dto)).toMatchObject({ firstActivatedAt: { not: null } }); }); }); diff --git a/apps/api/src/common/dto/trace-code-selection.dto.ts b/apps/api/src/common/dto/trace-code-selection.dto.ts index 8034c87..59e7af3 100644 --- a/apps/api/src/common/dto/trace-code-selection.dto.ts +++ b/apps/api/src/common/dto/trace-code-selection.dto.ts @@ -15,6 +15,7 @@ import { ValidateNested, } from 'class-validator'; import { MAX_BATCH_SELECTION, ProofStatus, TraceCodeStatus } from '@artveris/shared'; +import { queryBoolean } from '../utils/query-boolean.util'; export const ONCHAIN_FILTER_STATUSES = ['ELIGIBLE', 'NONE', ...Object.values(ProofStatus)] as const; export type OnchainFilterStatus = (typeof ONCHAIN_FILTER_STATUSES)[number]; @@ -55,9 +56,7 @@ export class TraceCodeFilterDto { @ApiPropertyOptional({ description: '是否已经首次激活' }) @IsOptional() - @Transform(({ value }) => - value === undefined || value === null || value === '' ? undefined : value === true || value === 'true', - ) + @Transform(queryBoolean) @IsBoolean() activated?: boolean; diff --git a/apps/api/src/common/utils/query-boolean.util.ts b/apps/api/src/common/utils/query-boolean.util.ts new file mode 100644 index 0000000..754b9cf --- /dev/null +++ b/apps/api/src/common/utils/query-boolean.util.ts @@ -0,0 +1,12 @@ +import type { TransformFnParams } from 'class-transformer'; + +/** + * 解析查询参数里的布尔值,配合 @Transform 使用。 + * 全局校验管道开启了隐式类型转换,查询字符串 'false' 会先被转成 true 再交给 @Transform, + * 所以这里读取转换前的原始值;未传或传空时保持未设置,不当作 false。 + */ +export function queryBoolean({ obj, key }: TransformFnParams): boolean | undefined { + const raw: unknown = obj?.[key]; + if (raw === undefined || raw === null || raw === '') return undefined; + return raw === true || raw === 'true'; +} diff --git a/apps/api/src/query-log/query-log.controller.ts b/apps/api/src/query-log/query-log.controller.ts index 6b2ad27..bd49c87 100644 --- a/apps/api/src/query-log/query-log.controller.ts +++ b/apps/api/src/query-log/query-log.controller.ts @@ -8,6 +8,7 @@ import { PrismaService } from '../prisma/prisma.service'; import { buildPage } from '../common/utils/pagination.util'; import { PaginationDto } from '../common/dto/pagination.dto'; import { buildOrderBy } from '../common/utils/sort.util'; +import { queryBoolean } from '../common/utils/query-boolean.util'; class QueryLogQueryDto extends PaginationDto { @ApiPropertyOptional({ description: '关键词(追溯码 / 档案编号)' }) @@ -32,9 +33,7 @@ class QueryLogQueryDto extends PaginationDto { @ApiPropertyOptional({ description: '只看异常查询(无效、停用、异常),与看板「异常查询」同一口径' }) @IsOptional() - @Transform(({ value }) => - value === undefined || value === null || value === '' ? undefined : value === true || value === 'true', - ) + @Transform(queryBoolean) @IsBoolean() abnormal?: boolean; diff --git a/apps/api/src/trace-code/dto/trace-code.dto.ts b/apps/api/src/trace-code/dto/trace-code.dto.ts index 77cf313..9c57ee0 100644 --- a/apps/api/src/trace-code/dto/trace-code.dto.ts +++ b/apps/api/src/trace-code/dto/trace-code.dto.ts @@ -16,6 +16,7 @@ import { import { TraceCodeStatus } from '@artveris/shared'; import { PaginationDto } from '../../common/dto/pagination.dto'; import { ONCHAIN_FILTER_STATUSES, OnchainFilterStatus } from '../../common/dto/trace-code-selection.dto'; +import { queryBoolean } from '../../common/utils/query-boolean.util'; export class GenerateBatchDto { @ApiProperty({ description: '所属作品(款)ID' }) @@ -73,9 +74,7 @@ export class QueryTraceCodeDto extends PaginationDto { @ApiPropertyOptional({ description: '是否已经首次激活' }) @IsOptional() - @Transform(({ value }) => - value === undefined || value === null || value === '' ? undefined : value === true || value === 'true', - ) + @Transform(queryBoolean) @IsBoolean() activated?: boolean; @@ -230,6 +229,12 @@ export class ExportTraceCodeDto { @IsIn(['OUTBOUND', 'NOT_OUTBOUND']) outboundStatus?: 'OUTBOUND' | 'NOT_OUTBOUND'; + @ApiPropertyOptional({ description: '是否已经首次激活' }) + @IsOptional() + @Transform(queryBoolean) + @IsBoolean() + activated?: boolean; + @ApiPropertyOptional({ description: '关键词' }) @IsOptional() @IsString()