forked from yoone/API
1
0
Fork 0

refactor(woocommerce): 优化参数处理逻辑并提取工具函数

将参数转换逻辑提取到独立的工具模块中
合并重复的include参数处理逻辑
添加对parent_exclude参数的支持
This commit is contained in:
tikkhun 2026-01-22 15:05:41 +08:00
parent d5384944a4
commit 75056db42c
3 changed files with 15 additions and 15 deletions

View File

@ -35,6 +35,7 @@ import {
import { Site } from '../entity/site.entity'; import { Site } from '../entity/site.entity';
import { WPService } from '../service/wp.service'; import { WPService } from '../service/wp.service';
import { BatchOperationDTO, BatchOperationResultDTO } from '../dto/batch.dto'; import { BatchOperationDTO, BatchOperationResultDTO } from '../dto/batch.dto';
import { toArray, toNumber } from '../utils/trans.util';
export class WooCommerceAdapter implements ISiteAdapter { export class WooCommerceAdapter implements ISiteAdapter {
// 构造函数接收站点配置与服务实例 // 构造函数接收站点配置与服务实例
@ -332,22 +333,11 @@ export class WooCommerceAdapter implements ISiteAdapter {
// } // }
const mapped: any = { const mapped: any = {
...(params.search ? { search: params.search } : {}), ...(params.search ? { search: params.search } : {}),
// ...(orderBy ? { orderBy } : {}),
page, page,
per_page, per_page,
}; };
const toArray = (value: any): any[] => {
if (Array.isArray(value)) return value;
if (value === undefined || value === null) return [];
return String(value).split(',').map(v => v.trim()).filter(Boolean);
};
const toNumber = (value: any): number | undefined => {
if (value === undefined || value === null || value === '') return undefined;
const n = Number(value);
return Number.isFinite(n) ? n : undefined;
};
// 时间过滤参数 // 时间过滤参数
if (where.after ?? where.date_created_after ?? where.created_after) mapped.after = String(where.after ?? where.date_created_after ?? where.created_after); if (where.after ?? where.date_created_after ?? where.created_after) mapped.after = String(where.after ?? where.date_created_after ?? where.created_after);
@ -358,9 +348,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
// 集合过滤参数 // 集合过滤参数
if (where.exclude) mapped.exclude = toArray(where.exclude); if (where.exclude) mapped.exclude = toArray(where.exclude);
if (where.include) mapped.include = toArray(where.include); if (where.ids || where.number || where.id || where.include) mapped.include = [...new Set([where.number,where.id,...toArray(where.ids),...toArray(where.include)])].filter(Boolean);
if (where.ids) mapped.include = toArray(where.ids);
if (toNumber(where.offset) !== undefined) mapped.offset = Number(where.offset); if (toNumber(where.offset) !== undefined) mapped.offset = Number(where.offset);
if (where.parent ?? where.parentId) mapped.parent = toArray(where.parent ?? where.parentId); if (where.parent ?? where.parentId) mapped.parent = toArray(where.parent ?? where.parentId);
if (where.parent_exclude ?? where.parentExclude) mapped.parent_exclude = toArray(where.parent_exclude ?? where.parentExclude); if (where.parent_exclude ?? where.parentExclude) mapped.parent_exclude = toArray(where.parent_exclude ?? where.parentExclude);

View File

@ -559,7 +559,8 @@ export interface WooOrderSearchParams {
order: string; order: string;
orderby: string; orderby: string;
parant: string[]; parant: string[];
status: (WooOrderStatusSearchParams)[]; parent_exclude: string[];
status: WooOrderStatusSearchParams[];
customer: number; customer: number;
product: number; product: number;
dp: number; dp: number;

11
src/utils/trans.util.ts Normal file
View File

@ -0,0 +1,11 @@
export const toArray = (value: any): any[] => {
if (Array.isArray(value)) return value;
if (value === undefined || value === null) return [];
return String(value).split(',').map(v => v.trim()).filter(Boolean);
};
export const toNumber = (value: any): number | undefined => {
if (value === undefined || value === null || value === '') return undefined;
const n = Number(value);
return Number.isFinite(n) ? n : undefined;
};