From 70948ef977d27f37374c0c7a4c95aedbb775991c Mon Sep 17 00:00:00 2001 From: tikkhun Date: Mon, 5 Jan 2026 15:36:24 +0800 Subject: [PATCH] =?UTF-8?q?refactor(adapter):=20=E4=BC=98=E5=8C=96ShopYY?= =?UTF-8?q?=E8=AE=A2=E5=8D=95=E7=8A=B6=E6=80=81=E6=98=A0=E5=B0=84=E5=92=8C?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=8F=82=E6=95=B0=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构ShopYY适配器中的订单状态映射逻辑,将shopyyOrderAutoNextStatusMap重命名为shopyyOrderStatusMap 新增mapUnifiedOrderQueryToShopyyQuery方法处理查询参数转换 移除site-api.controller中多余的where参数处理 --- src/adapter/shopyy.adapter.ts | 32 ++++++++++++++++++++++----- src/controller/site-api.controller.ts | 3 +-- src/controller/webhook.controller.ts | 2 ++ src/dto/site-api.dto.ts | 4 +++- src/service/order.service.ts | 17 +++++++++----- src/service/shopyy.service.ts | 6 ++++- src/service/site.service.ts | 5 +++-- 7 files changed, 53 insertions(+), 16 deletions(-) diff --git a/src/adapter/shopyy.adapter.ts b/src/adapter/shopyy.adapter.ts index cafc345..297c4e7 100644 --- a/src/adapter/shopyy.adapter.ts +++ b/src/adapter/shopyy.adapter.ts @@ -132,7 +132,7 @@ export class ShopyyAdapter implements ISiteAdapter { }; } - shopyyOrderAutoNextStatusMap = {//订单状态 100 未完成;110 待处理;180 已完成(确认收货); 190 取消; + shopyyOrderStatusMap = {//订单状态 100 未完成;110 待处理;180 已完成(确认收货); 190 取消; [100]: OrderStatus.PENDING, // 100 未完成 转为 pending [110]: OrderStatus.PROCESSING, // 110 待处理 转为 processing [180]: OrderStatus.COMPLETED, // 180 已完成(确认收货) 转为 completed @@ -244,13 +244,13 @@ export class ShopyyAdapter implements ISiteAdapter { 'SGD': 'S$' // 可以根据需要添加更多货币代码和符号 }; - const originStatus = item.status; - item.status = this.shopyyOrderAutoNextStatusMap[originStatus]; + // 映射订单状态,如果不存在则默认 pending + const status = this.shopyyOrderStatusMap[item.status?? item.order_status] || OrderStatus.PENDING; return { id: item.id || item.order_id, number: item.order_number || item.order_sn, - status: String(item.status || item.order_status), + status, currency: item.currency_code || item.currency, total: String(item.total_price ?? item.total_amount ?? ''), customer_id: item.customer_id || item.user_id, @@ -427,15 +427,37 @@ export class ShopyyAdapter implements ISiteAdapter { ): Promise { return await this.shopyyService.batchProcessProducts(this.site, data); } + mapUnifiedOrderQueryToShopyyQuery(params:UnifiedSearchParamsDTO ){ + const {where={} as any, ...restParams} = params|| {} + const statusMap = { + 'pending': '100', // 100 未完成 + 'processing': '110', // 110 待处理 + 'completed': "180", // 180 已完成(确认收货) + 'cancelled': '190', // 190 取消 + } + const normalizedParams:any= { + ...restParams, + } + if(where){ + normalizedParams.where = { + ...where, + } + if(where.status){ + normalizedParams.where.status = statusMap[where.status]; + } + } + return normalizedParams + } async getOrders( params: UnifiedSearchParamsDTO ): Promise> { + const normalizedParams = this.mapUnifiedOrderQueryToShopyyQuery(params); const { items, total, totalPages, page, per_page } = await this.shopyyService.fetchResourcePaged( this.site, 'orders', - params + normalizedParams ); return { items: items.map(this.mapOrder.bind(this)), diff --git a/src/controller/site-api.controller.ts b/src/controller/site-api.controller.ts index a98c958..f0c96c3 100644 --- a/src/controller/site-api.controller.ts +++ b/src/controller/site-api.controller.ts @@ -663,8 +663,7 @@ export class SiteApiController { this.logger.info(`[Site API] 获取订单列表开始, siteId: ${siteId}, query: ${JSON.stringify(query)}`); try { const adapter = await this.siteApiService.getAdapter(siteId); - const where = { ...(query.where || {}) }; - const data = await adapter.getOrders({ ...query, where }); + const data = await adapter.getOrders(query); this.logger.info(`[Site API] 获取订单列表成功, siteId: ${siteId}, 共获取到 ${data.total} 个订单`); return successResponse(data); } catch (error) { diff --git a/src/controller/webhook.controller.ts b/src/controller/webhook.controller.ts index c4ad821..69a070d 100644 --- a/src/controller/webhook.controller.ts +++ b/src/controller/webhook.controller.ts @@ -38,12 +38,14 @@ export class WebhookController { return 'webhook'; } + // TODO 这里得检查一下是否对 SHOPYY有效,否则得另外书写 @Post('/woocommerce') async handleWooWebhook( @Body() body: any, @Query('siteId') siteIdStr: string, @Headers() header: any ) { + console.log(`webhook woocommerce`, siteIdStr, body,header) const signature = header['x-wc-webhook-signature']; const topic = header['x-wc-webhook-topic']; const source = header['x-wc-webhook-source']; diff --git a/src/dto/site-api.dto.ts b/src/dto/site-api.dto.ts index 448bb39..a34751c 100644 --- a/src/dto/site-api.dto.ts +++ b/src/dto/site-api.dto.ts @@ -2,7 +2,9 @@ import { ApiProperty } from '@midwayjs/swagger'; import { UnifiedPaginationDTO, } from './api.dto'; - +// export class UnifiedOrderWhere{ +// [] +// } export class UnifiedTagDTO { // 标签DTO用于承载统一标签数据 @ApiProperty({ description: '标签ID' }) diff --git a/src/service/order.service.ts b/src/service/order.service.ts index 775b341..ba75691 100644 --- a/src/service/order.service.ts +++ b/src/service/order.service.ts @@ -1,4 +1,4 @@ -import { Inject, Provide } from '@midwayjs/core'; +import { Inject, Logger, Provide } from '@midwayjs/core'; import { WPService } from './wp.service'; import { Order } from '../entity/order.entity'; import { In, Like, Repository } from 'typeorm'; @@ -100,6 +100,9 @@ export class OrderService { @Inject() siteApiService: SiteApiService; + @Logger() + logger; // 注入 Logger 实例 + async syncOrders(siteId: number, params: Record = {}): Promise { // 调用 WooCommerce API 获取订单 const result = await (await this.siteApiService.getAdapter(siteId)).getAllOrders(params); @@ -144,7 +147,7 @@ export class OrderService { syncResult.processed++; } } - + this.logger.debug('syncOrders result', syncResult) return syncResult; } @@ -202,7 +205,7 @@ export class OrderService { // 由于 wordpress 订单状态和 我们的订单状态 不一致,需要做转换 async autoUpdateOrderStatus(siteId: number, order: any) { - console.log('更新订单状态', order.status, '=>', this.orderAutoNextStatusMap[order.status]) + // console.log('更新订单状态', order.status, '=>', this.orderAutoNextStatusMap[order.status]) // 其他状态保持不变 const originStatus = order.status; // 如果有值就赋值 @@ -286,7 +289,7 @@ export class OrderService { externalOrderId, coupon_lines, }); - console.log('同步进单个订单2') + // console.log('同步进单个订单2') await this.saveOrderShipping({ siteId, orderId, @@ -344,10 +347,14 @@ export class OrderService { where: { email: order.customer_email }, }); if (!customer) { + // 这里用 customer create await this.customerModel.save({ email: order.customer_email, site_id: siteId, origin_id: String(order.customer_id), + billing: order.billing, + shipping: order.shipping, + phone: order?.billing?.phone || order?.shipping?.phone, }); } return await this.orderModel.save(entity); @@ -394,7 +401,7 @@ export class OrderService { externalOrderId: string; orderItems: Record[]; }) { - console.log('saveOrderItems params',params) + // console.log('saveOrderItems params',params) const { siteId, orderId, externalOrderId, orderItems } = params; const currentOrderItems = await this.orderItemModel.find({ where: { siteId, externalOrderId: externalOrderId }, diff --git a/src/service/shopyy.service.ts b/src/service/shopyy.service.ts index f4bb39e..e437f46 100644 --- a/src/service/shopyy.service.ts +++ b/src/service/shopyy.service.ts @@ -1,4 +1,4 @@ -import { Inject, Provide } from '@midwayjs/core'; +import { ILogger, Inject, Provide } from '@midwayjs/core'; import axios, { AxiosRequestConfig } from 'axios'; import * as fs from 'fs'; import * as FormData from 'form-data'; @@ -13,6 +13,8 @@ import { UnifiedSearchParamsDTO } from '../dto/api.dto'; */ @Provide() export class ShopyyService { + @Inject() + logger:ILogger; /** * 获取ShopYY评论列表 * @param site 站点配置 @@ -201,10 +203,12 @@ export class ShopyyService { page, limit }; + this.logger.debug('ShopYY API请求分页参数:'+ JSON.stringify(requestParams)); const response = await this.request(site, endpoint, 'GET', null, requestParams); if (response?.code !== 0) { throw new Error(response?.msg) } + return { items: (response.data.list || []) as T[], total: response.data?.paginate?.total || 0, diff --git a/src/service/site.service.ts b/src/service/site.service.ts index 58664d7..165fb5c 100644 --- a/src/service/site.service.ts +++ b/src/service/site.service.ts @@ -47,7 +47,8 @@ export class SiteService { } // 使用 save 方法保存实体及其关联关系 - await this.siteModel.save(newSite); + const result = await this.siteModel.save(newSite); + console.log('result create siteId',result) return true; } @@ -97,7 +98,7 @@ export class SiteService { } // 使用 save 方法保存实体及其更新后的关联关系 - await this.siteModel.save(siteToUpdate); + await this.siteModel.save(siteToUpdate); return true; }