From a64e61129434111ea4b6605af137ec6626e15940 Mon Sep 17 00:00:00 2001 From: tikkhun Date: Mon, 24 Nov 2025 09:24:55 +0800 Subject: [PATCH] =?UTF-8?q?refactor(service):=20=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E4=BD=BF=E7=94=A8site.apiUrl=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BA=A7=E5=93=81=E5=90=8C=E6=AD=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除对wpApiUrl的兼容处理,统一使用apiUrl 修复产品同步查询的分页问题,使用Infinity获取全部数据 优化代码格式和缩进 --- src/service/site.service.ts | 2 +- src/service/wp.service.ts | 11 +++-- src/service/wp_product.service.ts | 74 +++++++++++++++---------------- 3 files changed, 43 insertions(+), 44 deletions(-) diff --git a/src/service/site.service.ts b/src/service/site.service.ts index 8510865..157ddf1 100644 --- a/src/service/site.service.ts +++ b/src/service/site.service.ts @@ -52,7 +52,7 @@ export class SiteService { const { consumerKey, consumerSecret, emailPswd, ...rest } = s as any; return rest; } - + async list(param: { current?: number; pageSize?: number; keyword?: string; isDisabled?: boolean; ids?: string }, includeSecret = false) { const { current = 1, pageSize = 10, keyword, isDisabled, ids } = (param || {}) as any; const where: any = {}; diff --git a/src/service/wp.service.ts b/src/service/wp.service.ts index 31bd356..110ff84 100644 --- a/src/service/wp.service.ts +++ b/src/service/wp.service.ts @@ -36,9 +36,8 @@ export class WPService { * @param namespace API 命名空间,默认 wc/v3;订阅推荐 wcs/v1 */ private createApi(site: any, namespace: WooCommerceRestApiVersion = 'wc/v3') { - const url = site.wpApiUrl ?? site.apiUrl; // 中文注释:兼容数据库 Site 与 WpSite 结构 return new WooCommerceRestApi({ - url, + url: site.apiUrl, consumerKey: site.consumerKey, consumerSecret: site.consumerSecret, version: namespace, @@ -86,7 +85,7 @@ export class WPService { param: Record = {} ): Promise { try { - const apiUrl = site.wpApiUrl ?? site.apiUrl; + const apiUrl = site.apiUrl; const { consumerKey, consumerSecret } = site; // 构建 URL,规避多/或少/问题 const url = this.buildURL(apiUrl, '/wp-json', endpoint); @@ -247,7 +246,7 @@ export class WPService { site: any, data: Record ): Promise { - const apiUrl = site.wpApiUrl ?? site.apiUrl; + const apiUrl = site.apiUrl; const { consumerKey, consumerSecret } = site; const auth = Buffer.from(`${consumerKey}:${consumerSecret}`).toString( 'base64' @@ -347,7 +346,7 @@ export class WPService { orderId: string, data: Record ) { - const apiUrl = site.wpApiUrl ?? site.apiUrl; + const apiUrl = site.apiUrl; const { consumerKey, consumerSecret } = site; const auth = Buffer.from(`${consumerKey}:${consumerSecret}`).toString( 'base64' @@ -375,7 +374,7 @@ export class WPService { orderId: string, trackingId: string, ): Promise { - const apiUrl = site.wpApiUrl ?? site.apiUrl; + const apiUrl = site.apiUrl; const { consumerKey, consumerSecret } = site; const auth = Buffer.from(`${consumerKey}:${consumerSecret}`).toString( 'base64' diff --git a/src/service/wp_product.service.ts b/src/service/wp_product.service.ts index 7da4afd..ddc214a 100644 --- a/src/service/wp_product.service.ts +++ b/src/service/wp_product.service.ts @@ -33,7 +33,7 @@ export class WpProductService { async syncAllSites() { // 中文注释:从数据库获取所有启用的站点,并逐站点同步产品与变体 - const { items } = await this.siteService.list({ current: 1, pageSize: 1000, isDisabled: false }, true); + const { items } = await this.siteService.list({ current: 1, pageSize: Infinity, isDisabled: false }, true); for (const s of items as any[]) { const site: WpSite = { id: String(s.id), @@ -54,49 +54,49 @@ export class WpProductService { } } } - + // 同步一个网站 async syncSite(siteId: string) { // 中文注释:通过数据库获取站点并转换为 WpSite,用于后续 WooCommerce 同步 const site = await this.siteService.get(Number(siteId), true); const externalProductIds = this.wpProductModel.createQueryBuilder('wp_product') - .select([ - 'wp_product.id ', + .select([ + 'wp_product.id ', 'wp_product.externalProductId ', ]) - .where('wp_product.siteId = :siteIds ', { + .where('wp_product.siteId = :siteIds ', { siteIds: siteId, }) - const rawResult = await externalProductIds.getRawMany(); + const rawResult = await externalProductIds.getRawMany(); - const externalIds = rawResult.map(item => item.externalProductId); + const externalIds = rawResult.map(item => item.externalProductId); -const excludeValues = []; + const excludeValues = []; const products = await this.wpApiService.getProducts(site); for (const product of products) { - excludeValues.push(String(product.id)); - const variations = + excludeValues.push(String(product.id)); + const variations = product.type === 'variable' ? await this.wpApiService.getVariations(site, product.id) : []; - + await this.syncProductAndVariations(site.id, product, variations); } const filteredIds = externalIds.filter(id => !excludeValues.includes(id)); - if(filteredIds.length!=0){ - await this.variationModel.createQueryBuilder('variation') - .update() - .set({ on_delete: true }) - .where(" variation.externalProductId in (:...filteredId) ",{filteredId:filteredIds}) - .execute(); + if (filteredIds.length != 0) { + await this.variationModel.createQueryBuilder('variation') + .update() + .set({ on_delete: true }) + .where(" variation.externalProductId in (:...filteredId) ", { filteredId: filteredIds }) + .execute(); - this.wpProductModel.createQueryBuilder('wp_product') - .update() - .set({ on_delete: true }) - .where(" wp_product.externalProductId in (:...filteredId) ",{filteredId:filteredIds}) - .execute(); -} + this.wpProductModel.createQueryBuilder('wp_product') + .update() + .set({ on_delete: true }) + .where(" wp_product.externalProductId in (:...filteredId) ", { filteredId: filteredIds }) + .execute(); + } } // 控制产品上下架 @@ -105,7 +105,7 @@ const excludeValues = []; const site = await this.siteService.get(Number(wpProduct.siteId), true); wpProduct.status = status; wpProduct.stockStatus = stock_status; - const res = await this.wpApiService.updateProductStatus(site, wpProduct.externalProductId, status, stock_status); + const res = await this.wpApiService.updateProductStatus(site, wpProduct.externalProductId, status, stock_status); if (res === true) { this.wpProductModel.save(wpProduct); return true; @@ -321,8 +321,8 @@ const excludeValues = []; if (status) { where.status = status; } - where.on_delete = false; - + where.on_delete = false; + const products = await this.wpProductModel.find({ where, skip: (current - 1) * pageSize, @@ -503,16 +503,16 @@ const excludeValues = []; if (!product) throw new Error('未找到该商品'); await this.variationModel.createQueryBuilder('variation') - .update() - .set({ on_delete: true }) - .where(" variation.externalProductId = :externalProductId ",{externalProductId:productId}) - .execute(); + .update() + .set({ on_delete: true }) + .where(" variation.externalProductId = :externalProductId ", { externalProductId: productId }) + .execute(); - const sums= await this.wpProductModel.createQueryBuilder('wp_product') - .update() - .set({ on_delete: true }) - .where(" wp_product.externalProductId = :externalProductId ",{externalProductId:productId}) - .execute(); + const sums = await this.wpProductModel.createQueryBuilder('wp_product') + .update() + .set({ on_delete: true }) + .where(" wp_product.externalProductId = :externalProductId ", { externalProductId: productId }) + .execute(); console.log(sums); //await this.variationModel.delete({ siteId, externalProductId: productId }); @@ -520,7 +520,7 @@ const excludeValues = []; } - + async findProductsByName(name: string): Promise { const nameFilter = name ? name.split(' ').filter(Boolean) : []; const query = this.wpProductModel.createQueryBuilder('product'); @@ -553,7 +553,7 @@ const excludeValues = []; } query.take(50); - + return await query.getMany(); } }