fix: 优化后台体验与导出日志交互
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import { IsInt, IsOptional, Min } from 'class-validator';
|
||||
import { IsIn, IsInt, IsOptional, IsString, Min } from 'class-validator';
|
||||
|
||||
export class PaginationDto {
|
||||
@ApiPropertyOptional({ description: '页码,从 1 开始', default: 1 })
|
||||
@@ -17,6 +17,16 @@ export class PaginationDto {
|
||||
@Min(0)
|
||||
pageSize = 20;
|
||||
|
||||
@ApiPropertyOptional({ description: '排序字段' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
sortField?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: '排序方向', enum: ['ascend', 'descend', 'asc', 'desc'] })
|
||||
@IsOptional()
|
||||
@IsIn(['ascend', 'descend', 'asc', 'desc'])
|
||||
sortOrder?: 'ascend' | 'descend' | 'asc' | 'desc';
|
||||
|
||||
get skip(): number {
|
||||
return (this.page - 1) * this.pageSize;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
export type SortOrderInput = 'ascend' | 'descend' | 'asc' | 'desc' | undefined;
|
||||
|
||||
export function normalizeSortOrder(order: SortOrderInput): 'asc' | 'desc' | null {
|
||||
if (order === 'asc' || order === 'ascend') return 'asc';
|
||||
if (order === 'desc' || order === 'descend') return 'desc';
|
||||
return null;
|
||||
}
|
||||
|
||||
export function buildOrderBy<T extends string>(
|
||||
field: string | undefined,
|
||||
order: SortOrderInput,
|
||||
allowed: readonly T[],
|
||||
fallback: Record<string, 'asc' | 'desc'>,
|
||||
): Record<string, 'asc' | 'desc'> {
|
||||
const normalized = normalizeSortOrder(order);
|
||||
if (!field || !normalized || !allowed.includes(field as T)) return fallback;
|
||||
return { [field]: normalized };
|
||||
}
|
||||
Reference in New Issue
Block a user