Compare commits

..

2 Commits

Author SHA1 Message Date
zhuotianyuan a956c963c0 Merge pull request '【Fix】紧急修复site数据优化产生的连接wp数据异常的bug' (#32) from zhuotianyuan/API:main into main
Reviewed-on: #32
2025-12-19 09:29:38 +00:00
zhuotianyuan ae34d1fab0 fix: 修复订单同步时未限制时间范围的问题
修改订单同步逻辑,默认只同步最近7天的订单数据
移除站点服务中不必要的字段解构
扩展WP服务getOrders方法支持参数传递
2025-12-19 17:28:51 +08:00
4 changed files with 20 additions and 6 deletions

View File

@ -16,8 +16,8 @@ export class OrderStatisticsParams {
keyword?: string;
@ApiProperty()
@Rule(RuleType.string().allow(null))
siteId?: string;
@Rule(RuleType.number().allow(null))
siteId?: number;
@ApiProperty({
enum: ['all', 'first_purchase', 'repeat_purchase'],

View File

@ -104,7 +104,21 @@ export class OrderService {
siteService: SiteService;
async syncOrders(siteId: string) {
const orders = await this.wpService.getOrders(siteId); // 调用 WooCommerce API 获取订单
//TODO: 临时方案,后续记得调整成前端可控制
const daysRange = 7;
// 获取当前时间和7天前时间
const now = new Date();
const sevenDaysAgo = new Date();
sevenDaysAgo.setDate(now.getDate() - daysRange);
// 格式化时间为ISO 8601
const after = sevenDaysAgo.toISOString();
const before = now.toISOString();
const orders = await this.wpService.getOrders(siteId,{
after: after,
before: before,
}); // 调用 WooCommerce API 获取订单
for (const order of orders) {
await this.syncSingleOrder(siteId, order);
}

View File

@ -57,7 +57,7 @@ export class SiteService {
if (!site) return null;
if (includeSecret) return site;
// 默认不返回密钥,进行字段脱敏
const { consumerKey, consumerSecret, ...rest } = site;
const { ...rest } = site;
return rest;
}

View File

@ -186,10 +186,10 @@ export class WPService {
const res = await api.get(`orders/${orderId}`);
return res.data as Record<string, any>;
}
async getOrders(siteId: string): Promise<Record<string, any>[]> {
async getOrders(siteId: string,params: Record<string, any> = {}): Promise<Record<string, any>[]> {
const site = await this.siteService.get(siteId);
const api = this.createApi(site, 'wc/v3');
return await this.sdkGetAll<Record<string, any>>(api, 'orders');
return await this.sdkGetAll<Record<string, any>>(api, 'orders', params);
}
/**