zksu
/
API
forked from yoone/API
1
0
Fork 0

refactor(api): 统一接口参数为对象形式并支持多条件查询

重构所有接口方法,将直接传递id参数改为接受where条件对象
支持通过id、sku、email等多条件查询实体
优化产品服务逻辑,支持通过sku直接查询产品
统一各适配器实现,确保接口一致性
This commit is contained in:
tikkhun 2026-01-07 20:33:23 +08:00
parent 6311451d61
commit 9e90d5f9cf
7 changed files with 248 additions and 217 deletions

View File

@ -122,8 +122,11 @@ export class ShopyyAdapter implements ISiteAdapter {
return data return data
} }
async getCustomer(id: string | number): Promise<UnifiedCustomerDTO> { async getCustomer(where: {id?: string | number,email?: string,phone?: string}): Promise<UnifiedCustomerDTO> {
const customer = await this.shopyyService.getCustomer(this.site, id); if(!where.id && !where.email && !where.phone){
throw new Error('必须传入 id 或 email 或 phone')
}
const customer = await this.shopyyService.getCustomer(this.site, where.id);
return this.mapPlatformToUnifiedCustomer(customer); return this.mapPlatformToUnifiedCustomer(customer);
} }
@ -149,13 +152,13 @@ export class ShopyyAdapter implements ISiteAdapter {
return this.mapPlatformToUnifiedCustomer(createdCustomer); return this.mapPlatformToUnifiedCustomer(createdCustomer);
} }
async updateCustomer(id: string | number, data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO> { async updateCustomer(where: {id: string | number}, data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO> {
const updatedCustomer = await this.shopyyService.updateCustomer(this.site, id, data); const updatedCustomer = await this.shopyyService.updateCustomer(this.site, where.id, data);
return this.mapPlatformToUnifiedCustomer(updatedCustomer); return this.mapPlatformToUnifiedCustomer(updatedCustomer);
} }
async deleteCustomer(id: string | number): Promise<boolean> { async deleteCustomer(where: {id: string | number}): Promise<boolean> {
return await this.shopyyService.deleteCustomer(this.site, id); return await this.shopyyService.deleteCustomer(this.site, where.id);
} }
batchProcessCustomers?(data: BatchOperationDTO): Promise<BatchOperationResultDTO> { batchProcessCustomers?(data: BatchOperationDTO): Promise<BatchOperationResultDTO> {
@ -208,13 +211,13 @@ export class ShopyyAdapter implements ISiteAdapter {
return this.mapPlatformToUnifiedMedia(createdMedia); return this.mapPlatformToUnifiedMedia(createdMedia);
} }
async updateMedia(id: string | number, data: any): Promise<UnifiedMediaDTO> { async updateMedia(where: {id: string | number}, data: any): Promise<UnifiedMediaDTO> {
const updatedMedia = await this.shopyyService.updateMedia(this.site, id, data); const updatedMedia = await this.shopyyService.updateMedia(this.site, where.id, data);
return this.mapPlatformToUnifiedMedia(updatedMedia); return this.mapPlatformToUnifiedMedia(updatedMedia);
} }
async deleteMedia(id: string | number): Promise<boolean> { async deleteMedia(where: {id: string | number}): Promise<boolean> {
return await this.shopyyService.deleteMedia(this.site, id); return await this.shopyyService.deleteMedia(this.site, where.id);
} }
mapMediaSearchParams(params: UnifiedSearchParamsDTO): any { mapMediaSearchParams(params: UnifiedSearchParamsDTO): any {
@ -530,8 +533,8 @@ export class ShopyyAdapter implements ISiteAdapter {
return params; return params;
} }
async getOrder(id: string | number): Promise<UnifiedOrderDTO> { async getOrder(where: {id: string | number}): Promise<UnifiedOrderDTO> {
const data = await this.shopyyService.getOrder(this.site.id, String(id)); const data = await this.shopyyService.getOrder(this.site.id, String(where.id));
return this.mapPlatformToUnifiedOrder(data); return this.mapPlatformToUnifiedOrder(data);
} }
@ -577,14 +580,14 @@ export class ShopyyAdapter implements ISiteAdapter {
return this.mapPlatformToUnifiedOrder(createdOrder); return this.mapPlatformToUnifiedOrder(createdOrder);
} }
async updateOrder(id: string | number, data: Partial<UnifiedOrderDTO>): Promise<boolean> { async updateOrder(where: {id: string | number}, data: Partial<UnifiedOrderDTO>): Promise<boolean> {
// 使用映射方法转换参数 // 使用映射方法转换参数
const requestParams = this.mapUpdateOrderParams(data); const requestParams = this.mapUpdateOrderParams(data);
return await this.shopyyService.updateOrder(this.site, String(id), requestParams); return await this.shopyyService.updateOrder(this.site, String(where.id), requestParams);
} }
async deleteOrder(id: string | number): Promise<boolean> { async deleteOrder(where: {id: string | number}): Promise<boolean> {
return await this.shopyyService.deleteOrder(this.site, id); return await this.shopyyService.deleteOrder(this.site, where.id);
} }
async getOrderNotes(orderId: string | number): Promise<any[]> { async getOrderNotes(orderId: string | number): Promise<any[]> {
@ -858,10 +861,18 @@ export class ShopyyAdapter implements ISiteAdapter {
return params; return params;
} }
async getProduct(id: string | number): Promise<UnifiedProductDTO> { async getProduct(where: {id?: string | number, sku?: string}): Promise<UnifiedProductDTO> {
// 使用ShopyyService获取单个产品 if(!where.id && !where.sku){
const product = await this.shopyyService.getProduct(this.site, id); throw new Error('必须传入 id 或 sku')
return this.mapPlatformToUnifiedProduct(product); }
if (where.id) {
// 使用ShopyyService获取单个产品
const product = await this.shopyyService.getProduct(this.site, where.id);
return this.mapPlatformToUnifiedProduct(product);
} else if (where.sku) {
// 通过sku获取产品
return this.getProductBySku(where.sku);
}
} }
async getProducts( async getProducts(
@ -900,24 +911,50 @@ export class ShopyyAdapter implements ISiteAdapter {
return this.mapPlatformToUnifiedProduct(res); return this.mapPlatformToUnifiedProduct(res);
} }
async updateProduct(id: string | number, data: Partial<UnifiedProductDTO>): Promise<boolean> { async updateProduct(where: {id?: string | number, sku?: string}, data: Partial<UnifiedProductDTO>): Promise<boolean> {
// Shopyy update returns boolean? let productId: string;
// shopyyService.updateProduct returns boolean. if (where.id) {
// So I can't return the updated product. productId = String(where.id);
// I have to fetch it again or return empty/input. } else if (where.sku) {
// Since getProduct is missing, I'll return input data as UnifiedProductDTO (mock). // 通过sku获取产品ID
const product = await this.getProductBySku(where.sku);
productId = String(product.id);
} else {
throw new Error('必须提供id或sku参数');
}
// 使用映射方法转换参数 // 使用映射方法转换参数
const requestParams = this.mapUpdateProductParams(data); const requestParams = this.mapUpdateProductParams(data);
await this.shopyyService.updateProduct(this.site, String(id), requestParams); await this.shopyyService.updateProduct(this.site, productId, requestParams);
return true; return true;
} }
async deleteProduct(id: string | number): Promise<boolean> { async deleteProduct(where: {id?: string | number, sku?: string}): Promise<boolean> {
let productId: string | number;
if (where.id) {
productId = where.id;
} else if (where.sku) {
// 通过sku获取产品ID
const product = await this.getProductBySku(where.sku);
productId = product.id;
} else {
throw new Error('必须提供id或sku参数');
}
// Use batch delete // Use batch delete
await this.shopyyService.batchProcessProducts(this.site, { delete: [id] }); await this.shopyyService.batchProcessProducts(this.site, { delete: [productId] });
return true; return true;
} }
// 通过sku获取产品详情的私有方法
private async getProductBySku(sku: string): Promise<UnifiedProductDTO> {
// 使用Shopyy API的搜索功能通过sku查询产品
const response = await this.shopyyService.getProducts(this.site, 1, 100);
const product = response.items.find((item: any) => item.sku === sku);
if (!product) {
throw new Error(`未找到sku为${sku}的产品`);
}
return this.mapPlatformToUnifiedProduct(product);
}
async batchProcessProducts( async batchProcessProducts(
data: { create?: any[]; update?: any[]; delete?: Array<string | number> } data: { create?: any[]; update?: any[]; delete?: Array<string | number> }
): Promise<any> { ): Promise<any> {
@ -929,9 +966,6 @@ export class ShopyyAdapter implements ISiteAdapter {
} }
// ========== 评论映射方法 ========== // ========== 评论映射方法 ==========
mapPlatformToUnifiedReview(data: any): UnifiedReviewDTO {
return data
}
mapUnifiedToPlatformReview(data: Partial<UnifiedReviewDTO>) { mapUnifiedToPlatformReview(data: Partial<UnifiedReviewDTO>) {
return data return data
@ -954,7 +988,7 @@ export class ShopyyAdapter implements ISiteAdapter {
requestParams requestParams
); );
return { return {
items: items.map(this.mapReview), items: items.map(this.mapPlatformToUnifiedReview),
total, total,
totalPages, totalPages,
page, page,
@ -969,19 +1003,19 @@ export class ShopyyAdapter implements ISiteAdapter {
async createReview(data: any): Promise<UnifiedReviewDTO> { async createReview(data: any): Promise<UnifiedReviewDTO> {
const createdReview = await this.shopyyService.createReview(this.site, data); const createdReview = await this.shopyyService.createReview(this.site, data);
return this.mapReview(createdReview); return this.mapPlatformToUnifiedReview(createdReview);
} }
async updateReview(id: string | number, data: any): Promise<UnifiedReviewDTO> { async updateReview(where: {id: string | number}, data: any): Promise<UnifiedReviewDTO> {
const updatedReview = await this.shopyyService.updateReview(this.site, id, data); const updatedReview = await this.shopyyService.updateReview(this.site, where.id, data);
return this.mapReview(updatedReview); return this.mapPlatformToUnifiedReview(updatedReview);
} }
async deleteReview(id: string | number): Promise<boolean> { async deleteReview(where: {id: string | number}): Promise<boolean> {
return await this.shopyyService.deleteReview(this.site, id); return await this.shopyyService.deleteReview(this.site, where.id);
} }
mapReview(review: any): UnifiedReviewDTO { mapPlatformToUnifiedReview(review: any): UnifiedReviewDTO {
// 将ShopYY评论数据映射到统一评论DTO格式 // 将ShopYY评论数据映射到统一评论DTO格式
return { return {
id: review.id || review.review_id, id: review.id || review.review_id,
@ -1119,9 +1153,7 @@ export class ShopyyAdapter implements ISiteAdapter {
} }
// ========== Webhook映射方法 ========== // ========== Webhook映射方法 ==========
mapPlatformToUnifiedWebhook(data: any): UnifiedWebhookDTO {
return data
}
mapUnifiedToPlatformWebhook(data: Partial<UnifiedWebhookDTO>) { mapUnifiedToPlatformWebhook(data: Partial<UnifiedWebhookDTO>) {
return data return data
@ -1135,15 +1167,15 @@ export class ShopyyAdapter implements ISiteAdapter {
return data return data
} }
async getWebhook(id: string | number): Promise<UnifiedWebhookDTO> { async getWebhook(where: {id: string | number}): Promise<UnifiedWebhookDTO> {
const webhook = await this.shopyyService.getWebhook(this.site, id); const webhook = await this.shopyyService.getWebhook(this.site, where.id);
return this.mapWebhook(webhook); return this.mapPlatformToUnifiedWebhook(webhook);
} }
async getWebhooks(params: UnifiedSearchParamsDTO): Promise<UnifiedWebhookPaginationDTO> { async getWebhooks(params: UnifiedSearchParamsDTO): Promise<UnifiedWebhookPaginationDTO> {
const { items, total, totalPages, page, per_page } = await this.shopyyService.getWebhooks(this.site, params); const { items, total, totalPages, page, per_page } = await this.shopyyService.getWebhooks(this.site, params);
return { return {
items: items.map(this.mapWebhook), items: items.map(this.mapPlatformToUnifiedWebhook),
total, total,
totalPages, totalPages,
page, page,
@ -1158,19 +1190,19 @@ export class ShopyyAdapter implements ISiteAdapter {
async createWebhook(data: CreateWebhookDTO): Promise<UnifiedWebhookDTO> { async createWebhook(data: CreateWebhookDTO): Promise<UnifiedWebhookDTO> {
const createdWebhook = await this.shopyyService.createWebhook(this.site, data); const createdWebhook = await this.shopyyService.createWebhook(this.site, data);
return this.mapWebhook(createdWebhook); return this.mapPlatformToUnifiedWebhook(createdWebhook);
} }
async updateWebhook(id: string | number, data: UpdateWebhookDTO): Promise<UnifiedWebhookDTO> { async updateWebhook(where: {id: string | number}, data: UpdateWebhookDTO): Promise<UnifiedWebhookDTO> {
const updatedWebhook = await this.shopyyService.updateWebhook(this.site, id, data); const updatedWebhook = await this.shopyyService.updateWebhook(this.site, where.id, data);
return this.mapWebhook(updatedWebhook); return this.mapPlatformToUnifiedWebhook(updatedWebhook);
} }
async deleteWebhook(id: string | number): Promise<boolean> { async deleteWebhook(where: {id: string | number}): Promise<boolean> {
return await this.shopyyService.deleteWebhook(this.site, id); return await this.shopyyService.deleteWebhook(this.site, where.id);
} }
mapWebhook(item: ShopyyWebhook): UnifiedWebhookDTO { mapPlatformToUnifiedWebhook(item: ShopyyWebhook): UnifiedWebhookDTO {
return { return {
id: item.id, id: item.id,
name: item.webhook_name || `Webhook-${item.id}`, name: item.webhook_name || `Webhook-${item.id}`,

View File

@ -36,12 +36,15 @@ import { BatchOperationDTO, BatchOperationResultDTO } from '../dto/batch.dto';
export class WooCommerceAdapter implements ISiteAdapter { export class WooCommerceAdapter implements ISiteAdapter {
// 构造函数接收站点配置与服务实例 // 构造函数接收站点配置与服务实例
constructor(private site: Site, private wpService: WPService) { constructor(private site: Site, private wpService: WPService) {
this.mapProduct = this.mapProduct.bind(this); this.mapPlatformToUnifiedProduct = this.mapPlatformToUnifiedProduct.bind(this);
this.mapReview = this.mapReview.bind(this); this.mapPlatformToUnifiedReview = this.mapPlatformToUnifiedReview.bind(this);
this.mapCustomer = this.mapCustomer.bind(this); this.mapPlatformToUnifiedCustomer = this.mapPlatformToUnifiedCustomer.bind(this);
this.mapMedia = this.mapMedia.bind(this); this.mapPlatformToUnifiedMedia = this.mapPlatformToUnifiedMedia.bind(this);
this.mapOrder = this.mapOrder.bind(this); this.mapPlatformToUnifiedOrder = this.mapPlatformToUnifiedOrder.bind(this);
this.mapWebhook = this.mapWebhook.bind(this); this.mapPlatformToUnifiedWebhook = this.mapPlatformToUnifiedWebhook.bind(this);
}
mapUnifiedToPlatformCustomer(data: Partial<UnifiedCustomerDTO>) {
return data
} }
batchProcessProducts?(data: BatchOperationDTO): Promise<BatchOperationResultDTO> { batchProcessProducts?(data: BatchOperationDTO): Promise<BatchOperationResultDTO> {
throw new Error('Method not implemented.'); throw new Error('Method not implemented.');
@ -54,14 +57,8 @@ export class WooCommerceAdapter implements ISiteAdapter {
} }
// ========== 客户映射方法 ========== // ========== 客户映射方法 ==========
mapPlatformToUnifiedCustomer(data: any): UnifiedCustomerDTO {
return data;
}
mapUnifiedToPlatformCustomer(data: Partial<UnifiedCustomerDTO>) {
return data;
}
mapCustomer(item: WooCustomer): UnifiedCustomerDTO { mapPlatformToUnifiedCustomer(item: WooCustomer): UnifiedCustomerDTO {
// 将 WooCommerce 客户数据映射为统一客户DTO // 将 WooCommerce 客户数据映射为统一客户DTO
// 包含基础信息地址信息与时间信息 // 包含基础信息地址信息与时间信息
return { return {
@ -136,10 +133,10 @@ export class WooCommerceAdapter implements ISiteAdapter {
} }
// 客户操作方法 // 客户操作方法
async getCustomer(id: string | number): Promise<UnifiedCustomerDTO> { async getCustomer(where: {id: string | number}): Promise<UnifiedCustomerDTO> {
const api = (this.wpService as any).createApi(this.site, 'wc/v3'); const api = (this.wpService as any).createApi(this.site, 'wc/v3');
const res = await api.get(`customers/${id}`); const res = await api.get(`customers/${where.id}`);
return this.mapCustomer(res.data); return this.mapPlatformToUnifiedCustomer(res.data);
} }
async getCustomers(params: UnifiedSearchParamsDTO): Promise<UnifiedPaginationDTO<UnifiedCustomerDTO>> { async getCustomers(params: UnifiedSearchParamsDTO): Promise<UnifiedPaginationDTO<UnifiedCustomerDTO>> {
@ -150,7 +147,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
requestParams requestParams
); );
return { return {
items: items.map((i: any) => this.mapCustomer(i)), items: items.map((i: any) => this.mapPlatformToUnifiedCustomer(i)),
total, total,
totalPages, totalPages,
page, page,
@ -167,36 +164,33 @@ export class WooCommerceAdapter implements ISiteAdapter {
const requestParams = this.mapCustomerSearchParams(params || {}); const requestParams = this.mapCustomerSearchParams(params || {});
const customers = await this.wpService.sdkGetAll(api, 'customers', requestParams); const customers = await this.wpService.sdkGetAll(api, 'customers', requestParams);
return customers.map((customer: any) => this.mapCustomer(customer)); return customers.map((customer: any) => this.mapPlatformToUnifiedCustomer(customer));
} }
async createCustomer(data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO> { async createCustomer(data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO> {
const api = (this.wpService as any).createApi(this.site, 'wc/v3'); const api = (this.wpService as any).createApi(this.site, 'wc/v3');
const res = await api.post('customers', data); const res = await api.post('customers', data);
return this.mapCustomer(res.data); return this.mapPlatformToUnifiedCustomer(res.data);
} }
async updateCustomer(id: string | number, data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO> { async updateCustomer(where: {id: string | number}, data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO> {
const api = (this.wpService as any).createApi(this.site, 'wc/v3'); const api = (this.wpService as any).createApi(this.site, 'wc/v3');
const res = await api.put(`customers/${id}`, data); const res = await api.put(`customers/${where.id}`, data);
return this.mapCustomer(res.data); return this.mapPlatformToUnifiedCustomer(res.data);
} }
async deleteCustomer(id: string | number): Promise<boolean> { async deleteCustomer(where: {id: string | number}): Promise<boolean> {
const api = (this.wpService as any).createApi(this.site, 'wc/v3'); const api = (this.wpService as any).createApi(this.site, 'wc/v3');
await api.delete(`customers/${id}`, { force: true }); await api.delete(`customers/${where.id}`, { force: true });
return true; return true;
} }
// ========== 媒体映射方法 ========== // ========== 媒体映射方法 ==========
mapPlatformToUnifiedMedia(data: any): UnifiedMediaDTO {
return data;
}
mapUnifiedToPlatformMedia(data: Partial<UnifiedMediaDTO>) { mapUnifiedToPlatformMedia(data: Partial<UnifiedMediaDTO>) {
return data; return data;
} }
mapMedia(item: WpMedia): UnifiedMediaDTO { mapPlatformToUnifiedMedia(item: WpMedia): UnifiedMediaDTO {
// 将 WordPress 媒体数据映射为统一媒体DTO // 将 WordPress 媒体数据映射为统一媒体DTO
// 兼容不同字段命名的时间信息 // 兼容不同字段命名的时间信息
return { return {
@ -221,7 +215,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
params params
); );
return { return {
items: items.map(this.mapMedia.bind(this)), items: items.map(this.mapPlatformToUnifiedMedia.bind(this)),
total, total,
totalPages, totalPages,
page, page,
@ -233,21 +227,21 @@ export class WooCommerceAdapter implements ISiteAdapter {
// 使用sdkGetAll获取所有媒体数据不受分页限制 // 使用sdkGetAll获取所有媒体数据不受分页限制
const api = (this.wpService as any).createApi(this.site, 'wc/v3'); const api = (this.wpService as any).createApi(this.site, 'wc/v3');
const media = await this.wpService.sdkGetAll(api, 'media', params); const media = await this.wpService.sdkGetAll(api, 'media', params);
return media.map((mediaItem: any) => this.mapMedia(mediaItem)); return media.map((mediaItem: any) => this.mapPlatformToUnifiedMedia(mediaItem));
} }
createMedia(file: any): Promise<UnifiedMediaDTO> { createMedia(file: any): Promise<UnifiedMediaDTO> {
throw new Error('Method not implemented.'); throw new Error('Method not implemented.');
} }
async updateMedia(id: string | number, data: any): Promise<any> { async updateMedia(where: {id: string | number}, data: any): Promise<any> {
// 更新媒体信息 // 更新媒体信息
return await this.wpService.updateMedia(Number(this.site.id), Number(id), data); return await this.wpService.updateMedia(Number(this.site.id), Number(where.id), data);
} }
async deleteMedia(id: string | number): Promise<boolean> { async deleteMedia(where: {id: string | number}): Promise<boolean> {
// 删除媒体资源 // 删除媒体资源
await this.wpService.deleteMedia(Number(this.site.id), Number(id), true); await this.wpService.deleteMedia(Number(this.site.id), Number(where.id), true);
return true; return true;
} }
@ -258,9 +252,6 @@ export class WooCommerceAdapter implements ISiteAdapter {
} }
// ========== 订单映射方法 ========== // ========== 订单映射方法 ==========
mapPlatformToUnifiedOrder(data: any): UnifiedOrderDTO {
return data;
}
mapUnifiedToPlatformOrder(data: Partial<UnifiedOrderDTO>) { mapUnifiedToPlatformOrder(data: Partial<UnifiedOrderDTO>) {
return data; return data;
} }
@ -356,7 +347,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
].filter(Boolean).join(', '); ].filter(Boolean).join(', ');
} }
mapOrder(item: WooOrder): UnifiedOrderDTO { mapPlatformToUnifiedOrder(item: WooOrder): UnifiedOrderDTO {
// 将 WooCommerce 订单数据映射为统一订单DTO // 将 WooCommerce 订单数据映射为统一订单DTO
// 包含账单地址与收货地址以及创建与更新时间 // 包含账单地址与收货地址以及创建与更新时间
@ -410,11 +401,11 @@ export class WooCommerceAdapter implements ISiteAdapter {
} }
// 订单操作方法 // 订单操作方法
async getOrder(id: string | number): Promise<UnifiedOrderDTO> { async getOrder(where: {id: string | number}): Promise<UnifiedOrderDTO> {
// 获取单个订单详情 // 获取单个订单详情
const api = (this.wpService as any).createApi(this.site, 'wc/v3'); const api = (this.wpService as any).createApi(this.site, 'wc/v3');
const res = await api.get(`orders/${id}`); const res = await api.get(`orders/${where.id}`);
return this.mapOrder(res.data); return this.mapPlatformToUnifiedOrder(res.data);
} }
async getOrders(params: UnifiedSearchParamsDTO): Promise<UnifiedPaginationDTO<UnifiedOrderDTO>> { async getOrders(params: UnifiedSearchParamsDTO): Promise<UnifiedPaginationDTO<UnifiedOrderDTO>> {
@ -444,7 +435,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
); );
return { return {
items: ordersWithFulfillments.map(this.mapOrder), items: ordersWithFulfillments.map(this.mapPlatformToUnifiedOrder),
total, total,
totalPages, totalPages,
page, page,
@ -456,7 +447,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
// 使用sdkGetAll获取所有订单数据不受分页限制 // 使用sdkGetAll获取所有订单数据不受分页限制
const api = (this.wpService as any).createApi(this.site, 'wc/v3'); const api = (this.wpService as any).createApi(this.site, 'wc/v3');
const orders = await this.wpService.sdkGetAll(api, 'orders', params); const orders = await this.wpService.sdkGetAll(api, 'orders', params);
return orders.map((order: any) => this.mapOrder(order)); return orders.map((order: any) => this.mapPlatformToUnifiedOrder(order));
} }
async countOrders(where: Record<string, any>): Promise<number> { async countOrders(where: Record<string, any>): Promise<number> {
@ -475,18 +466,18 @@ export class WooCommerceAdapter implements ISiteAdapter {
// 创建订单并返回统一订单DTO // 创建订单并返回统一订单DTO
const api = (this.wpService as any).createApi(this.site, 'wc/v3'); const api = (this.wpService as any).createApi(this.site, 'wc/v3');
const res = await api.post('orders', data); const res = await api.post('orders', data);
return this.mapOrder(res.data); return this.mapPlatformToUnifiedOrder(res.data);
} }
async updateOrder(id: string | number, data: Partial<UnifiedOrderDTO>): Promise<boolean> { async updateOrder(where: {id: string | number}, data: Partial<UnifiedOrderDTO>): Promise<boolean> {
// 更新订单并返回布尔结果 // 更新订单并返回布尔结果
return await this.wpService.updateOrder(this.site, String(id), data as any); return await this.wpService.updateOrder(this.site, String(where.id), data as any);
} }
async deleteOrder(id: string | number): Promise<boolean> { async deleteOrder(where: {id: string | number}): Promise<boolean> {
// 删除订单 // 删除订单
const api = (this.wpService as any).createApi(this.site, 'wc/v3'); const api = (this.wpService as any).createApi(this.site, 'wc/v3');
await api.delete(`orders/${id}`, { force: true }); await api.delete(`orders/${where.id}`, { force: true });
return true; return true;
} }
@ -591,9 +582,6 @@ export class WooCommerceAdapter implements ISiteAdapter {
} }
// ========== 产品映射方法 ========== // ========== 产品映射方法 ==========
mapPlatformToUnifiedProduct(data: any): UnifiedProductDTO {
return data;
}
mapUnifiedToPlatformProduct(data: Partial<UnifiedProductDTO>) { mapUnifiedToPlatformProduct(data: Partial<UnifiedProductDTO>) {
return data; return data;
} }
@ -661,7 +649,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
return mapped; return mapped;
} }
mapProduct(item: WooProduct): UnifiedProductDTO { mapPlatformToUnifiedProduct(item: WooProduct): UnifiedProductDTO {
// 将 WooCommerce 产品数据映射为统一产品DTO // 将 WooCommerce 产品数据映射为统一产品DTO
// 保留常用字段与时间信息以便前端统一展示 // 保留常用字段与时间信息以便前端统一展示
// https://woocommerce.github.io/woocommerce-rest-api-docs/?javascript#product-properties // https://woocommerce.github.io/woocommerce-rest-api-docs/?javascript#product-properties
@ -749,11 +737,22 @@ export class WooCommerceAdapter implements ISiteAdapter {
raw: item, raw: item,
}; };
} }
async getProduct({id, sku}: {id?: string, sku?:string}){
if(id) return this.getProductById(id);
if(sku) return this.getProductBySku(sku)
return this.getProductById(id || sku || '');
}
async getProductBySku(sku: string){
const api = (this.wpService as any).createApi(this.site, 'wc/v3');
const res = await api.get(`products?sku=${sku}`);
const product = res.data[0];
return this.mapPlatformToUnifiedProduct(product);
}
// 产品操作方法 // 产品操作方法
async getProduct(id: string | number): Promise<UnifiedProductDTO> { async getProductById(id: string | number): Promise<UnifiedProductDTO> {
// 获取单个产品详情并映射为统一产品DTO // 获取单个产品详情并映射为统一产品DTO
const api = (this.wpService as any).createApi(this.site, 'wc/v3'); const api = (this.wpService as any).createApi(this.site, 'wc/v3');
const res = await api.get(`products/${id}`); const res = await api.get(`products/${id}`);
const product = res.data; const product = res.data;
@ -773,7 +772,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
} }
} }
return this.mapProduct(product); return this.mapPlatformToUnifiedProduct(product);
} }
async getProducts(params: UnifiedSearchParamsDTO): Promise<UnifiedPaginationDTO<UnifiedProductDTO>> { async getProducts(params: UnifiedSearchParamsDTO): Promise<UnifiedPaginationDTO<UnifiedProductDTO>> {
@ -808,7 +807,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
); );
return { return {
items: productsWithVariations.map(this.mapProduct), items: productsWithVariations.map(this.mapPlatformToUnifiedProduct),
total, total,
totalPages, totalPages,
page, page,
@ -844,26 +843,46 @@ export class WooCommerceAdapter implements ISiteAdapter {
}) })
); );
return productsWithVariations.map((product: any) => this.mapProduct(product)); return productsWithVariations.map((product: any) => this.mapPlatformToUnifiedProduct(product));
} }
async createProduct(data: Partial<UnifiedProductDTO>): Promise<UnifiedProductDTO> { async createProduct(data: Partial<UnifiedProductDTO>): Promise<UnifiedProductDTO> {
// 创建产品并返回统一产品DTO // 创建产品并返回统一产品DTO
const res = await this.wpService.createProduct(this.site, data); const res = await this.wpService.createProduct(this.site, data);
return this.mapProduct(res); return this.mapPlatformToUnifiedProduct(res);
} }
async updateProduct(id: string | number, data: Partial<UnifiedProductDTO>): Promise<boolean> { async updateProduct(where: {id?: string | number, sku?: string}, data: Partial<UnifiedProductDTO>): Promise<boolean> {
// 更新产品并返回统一产品DTO // 更新产品并返回统一产品DTO
const res = await this.wpService.updateProduct(this.site, String(id), data as any); let productId: string;
if (where.id) {
productId = String(where.id);
} else if (where.sku) {
// 通过sku获取产品ID
const product = await this.getProductBySku(where.sku);
productId = String(product.id);
} else {
throw new Error('必须提供id或sku参数');
}
const res = await this.wpService.updateProduct(this.site, productId, data as any);
return res; return res;
} }
async deleteProduct(id: string | number): Promise<boolean> { async deleteProduct(where: {id?: string | number, sku?: string}): Promise<boolean> {
// 删除产品 // 删除产品
let productId: string;
if (where.id) {
productId = String(where.id);
} else if (where.sku) {
// 通过sku获取产品ID
const product = await this.getProductBySku(where.sku);
productId = String(product.id);
} else {
throw new Error('必须提供id或sku参数');
}
const api = (this.wpService as any).createApi(this.site, 'wc/v3'); const api = (this.wpService as any).createApi(this.site, 'wc/v3');
try { try {
await api.delete(`products/${id}`, { force: true }); await api.delete(`products/${productId}`, { force: true });
return true; return true;
} catch (e) { } catch (e) {
return false; return false;
@ -871,9 +890,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
} }
// ========== 评论映射方法 ========== // ========== 评论映射方法 ==========
mapPlatformToUnifiedReview(data: any): UnifiedReviewDTO {
return data;
}
mapUnifiedToPlatformReview(data: Partial<UnifiedReviewDTO>) { mapUnifiedToPlatformReview(data: Partial<UnifiedReviewDTO>) {
return data; return data;
} }
@ -885,7 +902,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
return data; return data;
} }
mapReview(item: any): UnifiedReviewDTO & { raw: any } { mapPlatformToUnifiedReview(item: any): UnifiedReviewDTO & { raw: any } {
// 将 WooCommerce 评论数据映射为统一评论DTO // 将 WooCommerce 评论数据映射为统一评论DTO
return { return {
id: item.id, id: item.id,
@ -910,7 +927,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
requestParams requestParams
); );
return { return {
items: items.map(this.mapReview.bind(this)), items: items.map(this.mapPlatformToUnifiedReview.bind(this)),
total, total,
totalPages, totalPages,
page, page,
@ -922,32 +939,29 @@ export class WooCommerceAdapter implements ISiteAdapter {
// 使用sdkGetAll获取所有评论数据不受分页限制 // 使用sdkGetAll获取所有评论数据不受分页限制
const api = (this.wpService as any).createApi(this.site, 'wc/v3'); const api = (this.wpService as any).createApi(this.site, 'wc/v3');
const reviews = await this.wpService.sdkGetAll(api, 'products/reviews', params); const reviews = await this.wpService.sdkGetAll(api, 'products/reviews', params);
return reviews.map((review: any) => this.mapReview(review)); return reviews.map((review: any) => this.mapPlatformToUnifiedReview(review));
} }
async createReview(data: any): Promise<UnifiedReviewDTO> { async createReview(data: any): Promise<UnifiedReviewDTO> {
const res = await this.wpService.createReview(this.site, data); const res = await this.wpService.createReview(this.site, data);
return this.mapReview(res); return this.mapPlatformToUnifiedReview(res);
} }
async updateReview(id: number, data: any): Promise<UnifiedReviewDTO> { async updateReview(where: {id: number}, data: any): Promise<UnifiedReviewDTO> {
const res = await this.wpService.updateReview(this.site, id, data); const res = await this.wpService.updateReview(this.site, where.id, data);
return this.mapReview(res); return this.mapPlatformToUnifiedReview(res);
} }
async deleteReview(id: number): Promise<boolean> { async deleteReview(where: {id: number}): Promise<boolean> {
return await this.wpService.deleteReview(this.site, id); return await this.wpService.deleteReview(this.site, where.id);
} }
// ========== 订阅映射方法 ========== // ========== 订阅映射方法 ==========
mapPlatformToUnifiedSubscription(data: any): UnifiedSubscriptionDTO {
return data;
}
mapUnifiedToPlatformSubscription(data: Partial<UnifiedSubscriptionDTO>) { mapUnifiedToPlatformSubscription(data: Partial<UnifiedSubscriptionDTO>) {
return data; return data;
} }
mapSubscription(item: WooSubscription): UnifiedSubscriptionDTO { mapPlatformToUnifiedSubscription(item: WooSubscription): UnifiedSubscriptionDTO {
// 将 WooCommerce 订阅数据映射为统一订阅DTO // 将 WooCommerce 订阅数据映射为统一订阅DTO
// 若缺少创建时间则回退为开始时间 // 若缺少创建时间则回退为开始时间
return { return {
@ -974,7 +988,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
params params
); );
return { return {
items: items.map(this.mapSubscription), items: items.map(this.mapPlatformToUnifiedSubscription),
total, total,
totalPages, totalPages,
page, page,
@ -987,7 +1001,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
// 使用sdkGetAll获取所有订阅数据不受分页限制 // 使用sdkGetAll获取所有订阅数据不受分页限制
const api = (this.wpService as any).createApi(this.site, 'wc/v3'); const api = (this.wpService as any).createApi(this.site, 'wc/v3');
const subscriptions = await this.wpService.sdkGetAll(api, 'subscriptions', params); const subscriptions = await this.wpService.sdkGetAll(api, 'subscriptions', params);
return subscriptions.map((subscription: any) => this.mapSubscription(subscription)); return subscriptions.map((subscription: any) => this.mapPlatformToUnifiedSubscription(subscription));
} }
// ========== 变体映射方法 ========== // ========== 变体映射方法 ==========
@ -1225,9 +1239,6 @@ export class WooCommerceAdapter implements ISiteAdapter {
} }
// ========== 网络钩子映射方法 ========== // ========== 网络钩子映射方法 ==========
mapPlatformToUnifiedWebhook(data: any): UnifiedWebhookDTO {
return data;
}
mapUnifiedToPlatformWebhook(data: Partial<UnifiedWebhookDTO>) { mapUnifiedToPlatformWebhook(data: Partial<UnifiedWebhookDTO>) {
return data; return data;
} }
@ -1240,7 +1251,7 @@ export class WooCommerceAdapter implements ISiteAdapter {
} }
// 映射 WooCommerce webhook 到统一格式 // 映射 WooCommerce webhook 到统一格式
mapWebhook(webhook: WooWebhook): UnifiedWebhookDTO { mapPlatformToUnifiedWebhook(webhook: WooWebhook): UnifiedWebhookDTO {
return { return {
id: webhook.id.toString(), id: webhook.id.toString(),
name: webhook.name, name: webhook.name,
@ -1262,12 +1273,12 @@ export class WooCommerceAdapter implements ISiteAdapter {
const result = await this.wpService.getWebhooks(this.site, params); const result = await this.wpService.getWebhooks(this.site, params);
return { return {
items: (result.items as WooWebhook[]).map(this.mapWebhook), items: (result.items as WooWebhook[]).map(this.mapPlatformToUnifiedWebhook),
total: result.total, total: result.total,
page: Number(params.page || 1), page: Number(params.page || 1),
per_page: Number(params.per_page || 20), per_page: Number(params.per_page || 20),
totalPages: result.totalPages, totalPages: result.totalPages,
}; };
} catch (error) { } catch (error) {
throw new Error(`Failed to get webhooks: ${error instanceof Error ? error.message : String(error)}`); throw new Error(`Failed to get webhooks: ${error instanceof Error ? error.message : String(error)}`);
} }
@ -1278,17 +1289,17 @@ export class WooCommerceAdapter implements ISiteAdapter {
try { try {
const api = (this.wpService as any).createApi(this.site, 'wc/v3'); const api = (this.wpService as any).createApi(this.site, 'wc/v3');
const webhooks = await this.wpService.sdkGetAll(api, 'webhooks', params); const webhooks = await this.wpService.sdkGetAll(api, 'webhooks', params);
return webhooks.map((webhook: any) => this.mapWebhook(webhook)); return webhooks.map((webhook: any) => this.mapPlatformToUnifiedWebhook(webhook));
} catch (error) { } catch (error) {
throw new Error(`Failed to get all webhooks: ${error instanceof Error ? error.message : String(error)}`); throw new Error(`Failed to get all webhooks: ${error instanceof Error ? error.message : String(error)}`);
} }
} }
// 获取单个 webhook 详情 // 获取单个 webhook 详情
async getWebhook(id: string | number): Promise<UnifiedWebhookDTO> { async getWebhook(where: {id: string | number}): Promise<UnifiedWebhookDTO> {
try { try {
const result = await this.wpService.getWebhook(this.site, id); const result = await this.wpService.getWebhook(this.site, where.id);
return this.mapWebhook(result as WooWebhook); return this.mapPlatformToUnifiedWebhook(result as WooWebhook);
} catch (error) { } catch (error) {
throw new Error(`Failed to get webhook: ${error instanceof Error ? error.message : String(error)}`); throw new Error(`Failed to get webhook: ${error instanceof Error ? error.message : String(error)}`);
} }
@ -1306,14 +1317,14 @@ export class WooCommerceAdapter implements ISiteAdapter {
api_version: data.api_version || 'wp/v2', api_version: data.api_version || 'wp/v2',
}; };
const result = await this.wpService.createWebhook(this.site, params); const result = await this.wpService.createWebhook(this.site, params);
return this.mapWebhook(result as WooWebhook); return this.mapPlatformToUnifiedWebhook(result as WooWebhook);
} catch (error) { } catch (error) {
throw new Error(`Failed to create webhook: ${error instanceof Error ? error.message : String(error)}`); throw new Error(`Failed to create webhook: ${error instanceof Error ? error.message : String(error)}`);
} }
} }
// 更新现有的 webhook // 更新现有的 webhook
async updateWebhook(id: string | number, data: UpdateWebhookDTO): Promise<UnifiedWebhookDTO> { async updateWebhook(where: Partial<Pick<UnifiedWebhookDTO, 'id'>>, data: UpdateWebhookDTO): Promise<UnifiedWebhookDTO> {
try { try {
const params = { const params = {
...(data.name ? { name: data.name } : {}), ...(data.name ? { name: data.name } : {}),
@ -1323,17 +1334,17 @@ export class WooCommerceAdapter implements ISiteAdapter {
...(data.secret ? { secret: data.secret } : {}), ...(data.secret ? { secret: data.secret } : {}),
...(data.api_version ? { api_version: data.api_version } : {}), ...(data.api_version ? { api_version: data.api_version } : {}),
}; };
const result = await this.wpService.updateWebhook(this.site, id, params); const result = await this.wpService.updateWebhook(this.site, where.id, params);
return this.mapWebhook(result as WooWebhook); return this.mapPlatformToUnifiedWebhook(result as WooWebhook);
} catch (error) { } catch (error) {
throw new Error(`Failed to update webhook: ${error instanceof Error ? error.message : String(error)}`); throw new Error(`Failed to update webhook: ${error instanceof Error ? error.message : String(error)}`);
} }
} }
// 删除指定的 webhook // 删除指定的 webhook
async deleteWebhook(id: string | number): Promise<boolean> { async deleteWebhook(where: Partial<Pick<UnifiedWebhookDTO, 'id'>>): Promise<boolean> {
try { try {
await this.wpService.deleteWebhook(this.site, id); await this.wpService.deleteWebhook(this.site, where.id);
return true; return true;
} catch (error) { } catch (error) {
throw new Error(`Failed to delete webhook: ${error instanceof Error ? error.message : String(error)}`); throw new Error(`Failed to delete webhook: ${error instanceof Error ? error.message : String(error)}`);

View File

@ -105,7 +105,7 @@ export class SiteApiController {
this.logger.debug(`[Site API] 更新评论开始, siteId: ${siteId}, id: ${id}, body: ${JSON.stringify(body)}`); this.logger.debug(`[Site API] 更新评论开始, siteId: ${siteId}, id: ${id}, body: ${JSON.stringify(body)}`);
try { try {
const adapter = await this.siteApiService.getAdapter(siteId); const adapter = await this.siteApiService.getAdapter(siteId);
const data = await adapter.updateReview(id, body); const data = await adapter.updateReview({ id }, body);
this.logger.debug(`[Site API] 更新评论成功, siteId: ${siteId}, id: ${id}`); this.logger.debug(`[Site API] 更新评论成功, siteId: ${siteId}, id: ${id}`);
return successResponse(data); return successResponse(data);
} catch (error) { } catch (error) {
@ -123,7 +123,7 @@ export class SiteApiController {
this.logger.debug(`[Site API] 删除评论开始, siteId: ${siteId}, id: ${id}`); this.logger.debug(`[Site API] 删除评论开始, siteId: ${siteId}, id: ${id}`);
try { try {
const adapter = await this.siteApiService.getAdapter(siteId); const adapter = await this.siteApiService.getAdapter(siteId);
const data = await adapter.deleteReview(id); const data = await adapter.deleteReview({ id });
this.logger.debug(`[Site API] 删除评论成功, siteId: ${siteId}, id: ${id}`); this.logger.debug(`[Site API] 删除评论成功, siteId: ${siteId}, id: ${id}`);
return successResponse(data); return successResponse(data);
} catch (error) { } catch (error) {
@ -159,7 +159,7 @@ export class SiteApiController {
this.logger.debug(`[Site API] 获取单个webhook开始, siteId: ${siteId}, id: ${id}`); this.logger.debug(`[Site API] 获取单个webhook开始, siteId: ${siteId}, id: ${id}`);
try { try {
const adapter = await this.siteApiService.getAdapter(siteId); const adapter = await this.siteApiService.getAdapter(siteId);
const data = await adapter.getWebhook(id); const data = await adapter.getWebhook({ id });
this.logger.debug(`[Site API] 获取单个webhook成功, siteId: ${siteId}, id: ${id}`); this.logger.debug(`[Site API] 获取单个webhook成功, siteId: ${siteId}, id: ${id}`);
return successResponse(data); return successResponse(data);
} catch (error) { } catch (error) {
@ -198,7 +198,7 @@ export class SiteApiController {
this.logger.debug(`[Site API] 更新webhook开始, siteId: ${siteId}, id: ${id}, body: ${JSON.stringify(body)}`); this.logger.debug(`[Site API] 更新webhook开始, siteId: ${siteId}, id: ${id}, body: ${JSON.stringify(body)}`);
try { try {
const adapter = await this.siteApiService.getAdapter(siteId); const adapter = await this.siteApiService.getAdapter(siteId);
const data = await adapter.updateWebhook(id, body); const data = await adapter.updateWebhook({ id }, body);
this.logger.debug(`[Site API] 更新webhook成功, siteId: ${siteId}, id: ${id}`); this.logger.debug(`[Site API] 更新webhook成功, siteId: ${siteId}, id: ${id}`);
return successResponse(data); return successResponse(data);
} catch (error) { } catch (error) {
@ -216,7 +216,7 @@ export class SiteApiController {
this.logger.debug(`[Site API] 删除webhook开始, siteId: ${siteId}, id: ${id}`); this.logger.debug(`[Site API] 删除webhook开始, siteId: ${siteId}, id: ${id}`);
try { try {
const adapter = await this.siteApiService.getAdapter(siteId); const adapter = await this.siteApiService.getAdapter(siteId);
const data = await adapter.deleteWebhook(id); const data = await adapter.deleteWebhook({ id });
this.logger.debug(`[Site API] 删除webhook成功, siteId: ${siteId}, id: ${id}`); this.logger.debug(`[Site API] 删除webhook成功, siteId: ${siteId}, id: ${id}`);
return successResponse(data); return successResponse(data);
} catch (error) { } catch (error) {
@ -326,7 +326,7 @@ export class SiteApiController {
if (site.type === 'woocommerce') { if (site.type === 'woocommerce') {
const page = query.page || 1; const page = query.page || 1;
const perPage = (query.per_page) || 100; const perPage = (query.per_page) || 100;
const res = await this.siteApiService.wpService.getProducts(site, page, perPage); const res = await this.siteApiService.wpService.getProducts(site, { page, per_page: perPage });
const header = ['id', 'name', 'type', 'status', 'sku', 'regular_price', 'sale_price', 'stock_status', 'stock_quantity']; const header = ['id', 'name', 'type', 'status', 'sku', 'regular_price', 'sale_price', 'stock_status', 'stock_quantity'];
const rows = (res.items || []).map((p: any) => [p.id, p.name, p.type, p.status, p.sku, p.regular_price, p.sale_price, p.stock_status, p.stock_quantity]); const rows = (res.items || []).map((p: any) => [p.id, p.name, p.type, p.status, p.sku, p.regular_price, p.sale_price, p.stock_status, p.stock_quantity]);
const toCsvValue = (val: any) => { const toCsvValue = (val: any) => {
@ -359,7 +359,7 @@ export class SiteApiController {
this.logger.info(`[Site API] 获取单个产品开始, siteId: ${siteId}, productId: ${id}`); this.logger.info(`[Site API] 获取单个产品开始, siteId: ${siteId}, productId: ${id}`);
try { try {
const adapter = await this.siteApiService.getAdapter(siteId); const adapter = await this.siteApiService.getAdapter(siteId);
const data = await adapter.getProduct(id); const data = await adapter.getProduct({ id });
// 如果获取到商品数据则增强ERP产品信息 // 如果获取到商品数据则增强ERP产品信息
if (data) { if (data) {
@ -484,7 +484,7 @@ export class SiteApiController {
this.logger.info(`[Site API] 更新产品开始, siteId: ${siteId}, productId: ${id}`); this.logger.info(`[Site API] 更新产品开始, siteId: ${siteId}, productId: ${id}`);
try { try {
const adapter = await this.siteApiService.getAdapter(siteId); const adapter = await this.siteApiService.getAdapter(siteId);
const data = await adapter.updateProduct(id, body); const data = await adapter.updateProduct({ id }, body);
this.logger.info(`[Site API] 更新产品成功, siteId: ${siteId}, productId: ${id}`); this.logger.info(`[Site API] 更新产品成功, siteId: ${siteId}, productId: ${id}`);
return successResponse(data); return successResponse(data);
} catch (error) { } catch (error) {
@ -539,7 +539,7 @@ export class SiteApiController {
this.logger.info(`[Site API] 删除产品开始, siteId: ${siteId}, productId: ${id}`); this.logger.info(`[Site API] 删除产品开始, siteId: ${siteId}, productId: ${id}`);
try { try {
const adapter = await this.siteApiService.getAdapter(siteId); const adapter = await this.siteApiService.getAdapter(siteId);
const success = await adapter.deleteProduct(id); const success = await adapter.deleteProduct({ id });
this.logger.info(`[Site API] 删除产品成功, siteId: ${siteId}, productId: ${id}`); this.logger.info(`[Site API] 删除产品成功, siteId: ${siteId}, productId: ${id}`);
return successResponse(success); return successResponse(success);
} catch (error) { } catch (error) {
@ -584,7 +584,7 @@ export class SiteApiController {
for (const item of body.update) { for (const item of body.update) {
try { try {
const id = item.id; const id = item.id;
const data = await adapter.updateProduct(id, item); const data = await adapter.updateProduct({ id }, item);
updated.push(data); updated.push(data);
} catch (e) { } catch (e) {
errors.push({ errors.push({
@ -597,7 +597,7 @@ export class SiteApiController {
if (body.delete?.length) { if (body.delete?.length) {
for (const id of body.delete) { for (const id of body.delete) {
try { try {
const ok = await adapter.deleteProduct(id); const ok = await adapter.deleteProduct({ id });
if (ok) deleted.push(id); if (ok) deleted.push(id);
else errors.push({ else errors.push({
identifier: String(id), identifier: String(id),
@ -771,7 +771,7 @@ export class SiteApiController {
this.logger.info(`[Site API] 获取单个订单开始, siteId: ${siteId}, orderId: ${id}`); this.logger.info(`[Site API] 获取单个订单开始, siteId: ${siteId}, orderId: ${id}`);
try { try {
const adapter = await this.siteApiService.getAdapter(siteId); const adapter = await this.siteApiService.getAdapter(siteId);
const data = await adapter.getOrder(id); const data = await adapter.getOrder({ id });
this.logger.info(`[Site API] 获取单个订单成功, siteId: ${siteId}, orderId: ${id}`); this.logger.info(`[Site API] 获取单个订单成功, siteId: ${siteId}, orderId: ${id}`);
return successResponse(data); return successResponse(data);
} catch (error) { } catch (error) {
@ -843,7 +843,7 @@ export class SiteApiController {
this.logger.info(`[Site API] 更新订单开始, siteId: ${siteId}, orderId: ${id}`); this.logger.info(`[Site API] 更新订单开始, siteId: ${siteId}, orderId: ${id}`);
try { try {
const adapter = await this.siteApiService.getAdapter(siteId); const adapter = await this.siteApiService.getAdapter(siteId);
const ok = await adapter.updateOrder(id, body); const ok = await adapter.updateOrder({ id }, body);
this.logger.info(`[Site API] 更新订单成功, siteId: ${siteId}, orderId: ${id}`); this.logger.info(`[Site API] 更新订单成功, siteId: ${siteId}, orderId: ${id}`);
return successResponse(ok); return successResponse(ok);
} catch (error) { } catch (error) {
@ -861,7 +861,7 @@ export class SiteApiController {
this.logger.info(`[Site API] 删除订单开始, siteId: ${siteId}, orderId: ${id}`); this.logger.info(`[Site API] 删除订单开始, siteId: ${siteId}, orderId: ${id}`);
try { try {
const adapter = await this.siteApiService.getAdapter(siteId); const adapter = await this.siteApiService.getAdapter(siteId);
const ok = await adapter.deleteOrder(id); const ok = await adapter.deleteOrder({ id });
this.logger.info(`[Site API] 删除订单成功, siteId: ${siteId}, orderId: ${id}`); this.logger.info(`[Site API] 删除订单成功, siteId: ${siteId}, orderId: ${id}`);
return successResponse(ok); return successResponse(ok);
} catch (error) { } catch (error) {
@ -901,7 +901,7 @@ export class SiteApiController {
for (const item of body.update) { for (const item of body.update) {
try { try {
const id = item.id; const id = item.id;
const ok = await adapter.updateOrder(id, item); const ok = await adapter.updateOrder({ id }, item);
if (ok) updated.push(item); if (ok) updated.push(item);
else errors.push({ else errors.push({
identifier: String(item.id || 'unknown'), identifier: String(item.id || 'unknown'),
@ -918,7 +918,7 @@ export class SiteApiController {
if (body.delete?.length) { if (body.delete?.length) {
for (const id of body.delete) { for (const id of body.delete) {
try { try {
const ok = await adapter.deleteOrder(id); const ok = await adapter.deleteOrder({ id });
if (ok) deleted.push(id); if (ok) deleted.push(id);
else errors.push({ else errors.push({
identifier: String(id), identifier: String(id),
@ -1435,7 +1435,7 @@ export class SiteApiController {
this.logger.info(`[Site API] 获取单个客户开始, siteId: ${siteId}, customerId: ${id}`); this.logger.info(`[Site API] 获取单个客户开始, siteId: ${siteId}, customerId: ${id}`);
try { try {
const adapter = await this.siteApiService.getAdapter(siteId); const adapter = await this.siteApiService.getAdapter(siteId);
const data = await adapter.getCustomer(id); const data = await adapter.getCustomer({ id });
this.logger.info(`[Site API] 获取单个客户成功, siteId: ${siteId}, customerId: ${id}`); this.logger.info(`[Site API] 获取单个客户成功, siteId: ${siteId}, customerId: ${id}`);
return successResponse(data); return successResponse(data);
} catch (error) { } catch (error) {
@ -1507,7 +1507,7 @@ export class SiteApiController {
this.logger.info(`[Site API] 更新客户开始, siteId: ${siteId}, customerId: ${id}`); this.logger.info(`[Site API] 更新客户开始, siteId: ${siteId}, customerId: ${id}`);
try { try {
const adapter = await this.siteApiService.getAdapter(siteId); const adapter = await this.siteApiService.getAdapter(siteId);
const data = await adapter.updateCustomer(id, body); const data = await adapter.updateCustomer({ id }, body);
this.logger.info(`[Site API] 更新客户成功, siteId: ${siteId}, customerId: ${id}`); this.logger.info(`[Site API] 更新客户成功, siteId: ${siteId}, customerId: ${id}`);
return successResponse(data); return successResponse(data);
} catch (error) { } catch (error) {
@ -1525,7 +1525,7 @@ export class SiteApiController {
this.logger.info(`[Site API] 删除客户开始, siteId: ${siteId}, customerId: ${id}`); this.logger.info(`[Site API] 删除客户开始, siteId: ${siteId}, customerId: ${id}`);
try { try {
const adapter = await this.siteApiService.getAdapter(siteId); const adapter = await this.siteApiService.getAdapter(siteId);
const success = await adapter.deleteCustomer(id); const success = await adapter.deleteCustomer({ id });
this.logger.info(`[Site API] 删除客户成功, siteId: ${siteId}, customerId: ${id}`); this.logger.info(`[Site API] 删除客户成功, siteId: ${siteId}, customerId: ${id}`);
return successResponse(success); return successResponse(success);
} catch (error) { } catch (error) {
@ -1561,7 +1561,7 @@ export class SiteApiController {
for (const item of body.update) { for (const item of body.update) {
try { try {
const id = item.id; const id = item.id;
const data = await adapter.updateCustomer(id, item); const data = await adapter.updateCustomer({ id }, item);
updated.push(data); updated.push(data);
} catch (e) { } catch (e) {
failed.push({ action: 'update', item, error: (e as any).message }); failed.push({ action: 'update', item, error: (e as any).message });
@ -1571,7 +1571,7 @@ export class SiteApiController {
if (body.delete?.length) { if (body.delete?.length) {
for (const id of body.delete) { for (const id of body.delete) {
try { try {
const ok = await adapter.deleteCustomer(id); const ok = await adapter.deleteCustomer({ id });
if (ok) deleted.push(id); if (ok) deleted.push(id);
else failed.push({ action: 'delete', id, error: 'delete failed' }); else failed.push({ action: 'delete', id, error: 'delete failed' });
} catch (e) { } catch (e) {

View File

@ -19,7 +19,7 @@ import {
import { UnifiedPaginationDTO, UnifiedSearchParamsDTO } from '../dto/api.dto'; import { UnifiedPaginationDTO, UnifiedSearchParamsDTO } from '../dto/api.dto';
import { BatchOperationDTO, BatchOperationResultDTO } from '../dto/batch.dto'; import { BatchOperationDTO, BatchOperationResultDTO } from '../dto/batch.dto';
export interface ISiteAdapter { export interface ISiteAdapter {
// ========== 客户映射方法 ========== // ========== 客户映射方法 ==========
/** /**
* *
@ -38,7 +38,7 @@ export interface ISiteAdapter {
/** /**
* *
*/ */
getCustomer(id: string | number): Promise<UnifiedCustomerDTO>; getCustomer(where: Partial<Pick<UnifiedCustomerDTO, 'id' | 'email' | 'phone'>>): Promise<UnifiedCustomerDTO>;
/** /**
* *
@ -58,12 +58,12 @@ export interface ISiteAdapter {
/** /**
* *
*/ */
updateCustomer(id: string | number, data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO>; updateCustomer(where: Partial<Pick<UnifiedCustomerDTO, 'id' | 'email' | 'phone'>>, data: Partial<UnifiedCustomerDTO>): Promise<UnifiedCustomerDTO>;
/** /**
* *
*/ */
deleteCustomer(id: string | number): Promise<boolean>; deleteCustomer(where: Partial<Pick<UnifiedCustomerDTO, 'id' | 'email' | 'phone'>>): Promise<boolean>;
/** /**
* *
@ -132,7 +132,7 @@ export interface ISiteAdapter {
/** /**
* *
*/ */
getOrder(id: string | number): Promise<UnifiedOrderDTO>; getOrder(where: Partial<Pick<UnifiedOrderDTO, 'id'>>): Promise<UnifiedOrderDTO>;
/** /**
* *
@ -147,7 +147,7 @@ export interface ISiteAdapter {
/** /**
* *
*/ */
countOrders(params: Record<string,any>): Promise<number>; countOrders(params: Record<string, any>): Promise<number>;
/** /**
* *
@ -157,12 +157,12 @@ export interface ISiteAdapter {
/** /**
* *
*/ */
updateOrder(id: string | number, data: Partial<UnifiedOrderDTO>): Promise<boolean>; updateOrder(where: Partial<Pick<UnifiedOrderDTO, 'id'>>, data: Partial<UnifiedOrderDTO>): Promise<boolean>;
/** /**
* *
*/ */
deleteOrder(id: string | number): Promise<boolean>; deleteOrder(where: Partial<Pick<UnifiedOrderDTO, 'id'>>): Promise<boolean>;
/** /**
* *
@ -259,7 +259,7 @@ export interface ISiteAdapter {
/** /**
* *
*/ */
getProduct(id: string | number): Promise<UnifiedProductDTO>; getProduct(where: Partial<Pick<UnifiedProductDTO, 'id' | 'sku'>>): Promise<UnifiedProductDTO>;
/** /**
* *
@ -279,12 +279,12 @@ export interface ISiteAdapter {
/** /**
* *
*/ */
updateProduct(id: string | number, data: Partial<UnifiedProductDTO>): Promise<boolean>; updateProduct(where: Partial<Pick<UnifiedProductDTO, 'id' | 'sku'>>, data: Partial<UnifiedProductDTO>): Promise<boolean>;
/** /**
* *
*/ */
deleteProduct(id: string | number): Promise<boolean>; deleteProduct(where: Partial<Pick<UnifiedProductDTO, 'id' | 'sku'>>): Promise<boolean>;
/** /**
* *
@ -338,12 +338,12 @@ export interface ISiteAdapter {
/** /**
* *
*/ */
updateReview(id: number, data: UpdateReviewDTO): Promise<UnifiedReviewDTO>; updateReview(where: Partial<Pick<UnifiedReviewDTO, 'id'>>, data: UpdateReviewDTO): Promise<UnifiedReviewDTO>;
/** /**
* *
*/ */
deleteReview(id: number): Promise<boolean>; deleteReview(where: Partial<Pick<UnifiedReviewDTO, 'id'>>): Promise<boolean>;
// ========== 订阅映射方法 ========== // ========== 订阅映射方法 ==========
/** /**
@ -461,7 +461,7 @@ export interface ISiteAdapter {
/** /**
* webhook * webhook
*/ */
getWebhook(id: string | number): Promise<UnifiedWebhookDTO>; getWebhook(where: Partial<Pick<UnifiedWebhookDTO, 'id'>>): Promise<UnifiedWebhookDTO>;
/** /**
* webhooks列表 * webhooks列表
@ -481,16 +481,16 @@ export interface ISiteAdapter {
/** /**
* webhook * webhook
*/ */
updateWebhook(id: string | number, data: UpdateWebhookDTO): Promise<UnifiedWebhookDTO>; updateWebhook(where: Partial<Pick<UnifiedWebhookDTO, 'id'>>, data: UpdateWebhookDTO): Promise<UnifiedWebhookDTO>;
/** /**
* webhook * webhook
*/ */
deleteWebhook(id: string | number): Promise<boolean>; deleteWebhook(where: Partial<Pick<UnifiedWebhookDTO, 'id'>>): Promise<boolean>;
// ========== 站点/其他方法 ========== // ========== 站点/其他方法 ==========
/** /**
* *
*/ */
getLinks(): Promise<Array<{title: string, url: string}>>; getLinks(): Promise<Array<{ title: string, url: string }>>;
} }

View File

@ -202,7 +202,7 @@ export class OrderService {
try { try {
// 调用 WooCommerce API 获取订单 // 调用 WooCommerce API 获取订单
const adapter = await this.siteApiService.getAdapter(siteId); const adapter = await this.siteApiService.getAdapter(siteId);
const order = await adapter.getOrder(orderId); const order = await adapter.getOrder({ id: orderId });
// 检查订单是否已存在,以区分创建和更新 // 检查订单是否已存在,以区分创建和更新
const existingOrder = await this.orderModel.findOne({ const existingOrder = await this.orderModel.findOne({

View File

@ -110,36 +110,22 @@ export class SiteApiService {
const adapter = await this.getAdapter(siteId); const adapter = await this.getAdapter(siteId);
// 首先尝试查找产品 // 首先尝试查找产品
if (product.id) { if (product.sku) {
try {
// 尝试获取产品以确认它是否存在
const existingProduct = await adapter.getProduct(product.id);
if (existingProduct) {
// 产品存在,执行更新
return await adapter.updateProduct(product.id, product);
}
} catch (error) {
// 如果获取产品失败,可能是因为产品不存在,继续执行创建逻辑
console.log(`产品 ${product.id} 不存在,将创建新产品:`, error.message);
}
} else if (product.sku) {
// 如果没有提供ID但提供了SKU尝试通过SKU查找产品 // 如果没有提供ID但提供了SKU尝试通过SKU查找产品
try { try {
// 尝试搜索具有相同SKU的产品 // 尝试搜索具有相同SKU的产品
const searchResult = await adapter.getProducts({ where: { sku: product.sku } }); const existingProduct = await adapter.getProduct( { sku: product.sku });
if (searchResult.items && searchResult.items.length > 0) { if (existingProduct) {
const existingProduct = searchResult.items[0];
// 找到现有产品,更新它 // 找到现有产品,更新它
return await adapter.updateProduct(existingProduct.id, product); return await adapter.updateProduct({ id: existingProduct.id }, product);
} }
// 产品不存在,执行创建
return await adapter.createProduct(product);
} catch (error) { } catch (error) {
// 搜索失败,继续执行创建逻辑 // 搜索失败,继续执行创建逻辑
console.log(`通过SKU搜索产品失败:`, error.message); console.log(`通过SKU搜索产品失败:`, error.message);
} }
} }
// 产品不存在,执行创建
return await adapter.createProduct(product);
} }
/** /**
@ -197,7 +183,7 @@ export class SiteApiService {
*/ */
async getProductFromSite(siteId: number, productId: string | number): Promise<any> { async getProductFromSite(siteId: number, productId: string | number): Promise<any> {
const adapter = await this.getAdapter(siteId); const adapter = await this.getAdapter(siteId);
return await adapter.getProduct(productId); return await adapter.getProduct({ id: productId });
} }
/** /**

View File

@ -240,9 +240,11 @@ export class WPService implements IPlatformService {
return allData; return allData;
} }
async getProducts(site: any, page: number = 1, pageSize: number = 100): Promise<any> { async getProducts(site: any, params: Record<string, any> = {}): Promise<any> {
const api = this.createApi(site, 'wc/v3'); const api = this.createApi(site, 'wc/v3');
return await this.sdkGetPage<WooProduct>(api, 'products', { page, per_page: pageSize }); const page = params.page ?? 1;
const per_page = params.per_page ?? params.pageSize ?? 100;
return await this.sdkGetPage<WooProduct>(api, 'products', { ...params, page, per_page });
} }
async getProduct(site: any, id: number): Promise<any> { async getProduct(site: any, id: number): Promise<any> {
@ -254,7 +256,7 @@ export class WPService implements IPlatformService {
// 导出 WooCommerce 产品为特殊CSV平台特性 // 导出 WooCommerce 产品为特殊CSV平台特性
async exportProductsCsvSpecial(site: any, page: number = 1, pageSize: number = 100): Promise<string> { async exportProductsCsvSpecial(site: any, page: number = 1, pageSize: number = 100): Promise<string> {
const list = await this.getProducts(site, page, pageSize); const list = await this.getProducts(site, { page, per_page: pageSize });
const header = ['id','name','type','status','sku','regular_price','sale_price','stock_status','stock_quantity']; const header = ['id','name','type','status','sku','regular_price','sale_price','stock_status','stock_quantity'];
const rows = (list.items || []).map((p: any) => [p.id,p.name,p.type,p.status,p.sku,p.regular_price,p.sale_price,p.stock_status,p.stock_quantity]); const rows = (list.items || []).map((p: any) => [p.id,p.name,p.type,p.status,p.sku,p.regular_price,p.sale_price,p.stock_status,p.stock_quantity]);
const csv = [header.join(','), ...rows.map(r => r.map(v => String(v ?? '')).join(','))].join('\n'); const csv = [header.join(','), ...rows.map(r => r.map(v => String(v ?? '')).join(','))].join('\n');