From a06807ad7a3c772cc17293f3e00c7c97d32fbd42 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Sat, 7 Nov 2026 23:24:35 +0800 Subject: [PATCH] =?UTF-8?q?fix(shop-product):=20=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E5=8F=AA=E5=9B=9E=203=20=E4=B8=AA=E8=A7=84=E6=A0=BC=E6=97=B6?= =?UTF-8?q?=E7=94=A8=20/Product/LoadSkuList=20=E8=A1=A5=E5=85=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 平台 `/Product/LoadList` 返回的商品 `Skus` 固定只给 3 条(与 `SkuCount` 无关), 分销商品 3773823319627923739 声明 6 个规格、实际只下发 3 个,导致库里缺规格、 订单明细也匹配不到。商品页打开规格列表走的是另一个接口,实测可用: POST /Product/LoadSkuList ProductCode=<商品行的 ProductCode>&ProductType=all&IsHideDelSku=false… → Data 为「数字键对象」,含全部 6 个规格(与列表的 Skus[] 同结构) - 新增 dgj.fetchProductSkuList:按 ProductCode 拉全量规格 - 新增 dgj.repairProductSkus:只对 SkuCount > Skus.length 的商品补一次, 成功就地覆盖 product.Skus,失败只 warn(保留列表下发的部分) - fetchAreaSnapshot 在快照确定后调用补全(分区 dbname 与查商品时一致) - 单测 dgjRepairSkus.test.js 3 例(补全 / 不需要补 / 失败保留原样) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- backend/app/lib/printPlatform/dgj.js | 105 ++++++++++++++++++++- backend/test/app/lib/dgjRepairSkus.test.js | 58 ++++++++++++ 2 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 backend/test/app/lib/dgjRepairSkus.test.js diff --git a/backend/app/lib/printPlatform/dgj.js b/backend/app/lib/printPlatform/dgj.js index a662188..a014226 100644 --- a/backend/app/lib/printPlatform/dgj.js +++ b/backend/app/lib/printPlatform/dgj.js @@ -26,7 +26,8 @@ const AGENT_LIST_URL = `https://${WORKBENCH_HOST}/Partner/LoadMyAgentList`; /** 商品列表接口(按分区域名请求,路径固定) */ const PRODUCT_LIST_PATH = '/Product/LoadList'; - +/** 商品规格列表接口:列表接口的 `Skus` 可能不全(分销/多来源商品),这里按 ProductCode 取全量规格 */ +const PRODUCT_SKU_LIST_PATH = '/Product/LoadSkuList'; /** 订单列表接口(按分区域名请求,路径固定) */ const ORDER_LIST_PATH = '/NewOrder/List'; @@ -1596,6 +1597,49 @@ class DgjService extends PrintPlatformService { }; } + /** + * 按 `ProductCode` 拉取商品的全量规格列表 + * + * `/Product/LoadList` 的 `Skus` 会不全(实测分销/多来源商品:`SkuCount=6` 只下发 3 个), + * 商品页打开规格列表走的是本接口(入参 `ProductCode`,对应商品行的 `ProductCode` 字段)。 + * @param {string} host 商品接口域名 + * @param {Object} options 查询参数 + * @param {string} [options.traceId] 登录上下文 traceId + * @param {string} [options.dbname] 分区库名,为空表示默认区(与查商品时一致) + * @param {string} options.productCode 商品行里的 `ProductCode` + * @return {Promise>} 平台规格数组(与商品行 `Skus[]` 同结构) + */ + async fetchProductSkuList(host, { traceId = '', dbname = '', productCode = '' }) { + const code = String(productCode || '').trim(); + if (!code) return []; + const form = buildPhpForm({ + ProductCode: code, + ProductType: 'all', + // 含已删除规格:拉全是为了不漏规格,是否下架由商品状态字段判断 + IsHideDelSku: 'false', + IsRelationBaseProduct: '', + ProductSkuName: '', + ProductSkuShortTitle: '', + IsSetSkuShortTitle: '', + IsSkuSetWeight: '', + SkuId: '', + SkuCargoNumber: '', + IsExistStockCode: '', + StockCode: '', + IsCustomerOrder: 'false', + FxPageType: '1', + }); + const res = await this.postPlatformForm(host, PRODUCT_SKU_LIST_PATH, form, traceId, dbname); + if (!res.json || res.json.Success !== true) { + const reason = (res.json && (res.json.Message || res.json.ErrorCode)) || `HTTP ${res.status}`; + throw new Error(`拉取店管家商品规格失败(${host} ProductCode=${code}):${reason}`); + } + // 平台回的是「数字键对象」而不是数组:Data = {0:{…},1:{…}} + const data = res.json.Data; + const rows = Array.isArray(data) ? data : Object.values(data || {}).filter(item => item && typeof item === 'object'); + return rows.filter(item => item && (item.SkuId != null || item.Id != null)); + } + /** * 拉取某个域名下单个分区的全量商品快照。 * 逐页拉取并校验「去重后的商品数 = Total」,不一致时重试一次, @@ -1655,9 +1699,68 @@ class DgjService extends PrintPlatformService { if (snapshot.verified) break; } + // 规格补齐:列表接口下发的 `Skus` 可能不全(实测 SkuCount=6 只回 3 个), + // 商品页打开规格走 /Product/LoadSkuList(入参 ProductCode),这里按需补全 + if (snapshot && Array.isArray(snapshot.rows)) { + await this.repairProductSkus(host, { traceId, dbname, products: snapshot.rows }); + } + return snapshot; } + /** + * 把 `Skus` 不全的商品按 `/Product/LoadSkuList` 补全(就地覆盖 `product.Skus`) + * + * 只对 `SkuCount > Skus.length` 的商品多发一次请求;失败只留痕,不阻断整体同步 + * (上层会用 `incompleteProducts` 跳过这些商品的下架判定)。 + * @param {string} host 商品接口域名 + * @param {Object} options 参数 + * @param {string} options.traceId 登录上下文 traceId + * @param {string} options.dbname 分区库名 + * @param {Array} options.products 平台商品行(会被就地修改) + * @return {Promise<{repaired: number, failed: number}>} 补全统计 + */ + async repairProductSkus(host, { traceId, dbname, products }) { + let repaired = 0; + let failed = 0; + for (const product of Array.isArray(products) ? products : []) { + const expected = Number(product && product.SkuCount); + const got = Array.isArray(product && product.Skus) ? product.Skus.length : 0; + if (!(expected > got)) continue; + try { + const skus = await this.fetchProductSkuList(host, { + traceId, + dbname, + productCode: product.ProductCode, + }); + if (skus.length > got) { + product.Skus = skus; + repaired++; + if (this.logger && typeof this.logger.info === 'function') { + this.logger.info( + '[dgj] 商品 %s 的规格已补全:%d → %d(ProductCode=%s)', + product.PlatformId || product.Id, + got, + skus.length, + product.ProductCode || '-' + ); + } + } + } catch (err) { + failed++; + if (this.logger && typeof this.logger.warn === 'function') { + this.logger.warn( + '[dgj] 商品 %s 的规格补全失败(保持列表下发的 %d 个):%s', + product.PlatformId || product.Id, + got, + err.message + ); + } + } + } + return { repaired, failed }; + } + /** * 拉取某个域名下所有分区的商品快照并合并。 * 同一域名下可能有多个物理库(默认区 / 新系统区等),只查默认区会漏掉其它区的商品。 diff --git a/backend/test/app/lib/dgjRepairSkus.test.js b/backend/test/app/lib/dgjRepairSkus.test.js new file mode 100644 index 0000000..dce19b5 --- /dev/null +++ b/backend/test/app/lib/dgjRepairSkus.test.js @@ -0,0 +1,58 @@ +'use strict'; + +const { assert } = require('egg-mock/bootstrap'); +const DgjService = require('../../../app/lib/printPlatform/dgj'); + +/** + * 造一个不打网络的服务实例 + * @return {Object} dgj 服务实例 + */ +function buildService() { + const service = new DgjService({ platform: { code: 'dgj', name: '店管家' }, account: { token: 't', cookie: 'c' } }); + service.logger = { info() {}, warn() {}, error() {} }; + return service; +} + +describe('dgj.repairProductSkus 规格补全', () => { + it('SkuCount 大于下发数量时按 ProductCode 补全', async () => { + const service = buildService(); + const calls = []; + service.fetchProductSkuList = async (host, options) => { + calls.push(`${host}|${options.dbname}|${options.productCode}`); + return [ { SkuId: '1' }, { SkuId: '2' }, { SkuId: '3' }, { SkuId: '4' }, { SkuId: '5' }, { SkuId: '6' } ]; + }; + const product = { PlatformId: '3773823319627923739', ProductCode: '4bd42a4c869d1929', SkuCount: 6, Skus: [ { SkuId: '1' }, { SkuId: '2' }, { SkuId: '3' } ] }; + const result = await service.repairProductSkus('fxdd.dgjapp.com', { traceId: 'tr', dbname: '', products: [ product ] }); + console.log('repair:', JSON.stringify(result), '| skus:', product.Skus.length, '| calls:', calls.join(',')); + assert.deepStrictEqual(result, { repaired: 1, failed: 0 }); + assert.strictEqual(product.Skus.length, 6); + assert.strictEqual(calls[0], 'fxdd.dgjapp.com||4bd42a4c869d1929'); + }); + + it('规格已齐 / 没有 SkuCount 的商品不发请求', async () => { + const service = buildService(); + let called = 0; + service.fetchProductSkuList = async () => { + called++; + return []; + }; + const products = [ + { PlatformId: 'a', ProductCode: 'pa', SkuCount: 2, Skus: [ { SkuId: '1' }, { SkuId: '2' } ] }, + { PlatformId: 'b', ProductCode: 'pb', Skus: [ { SkuId: '3' } ] }, + ]; + const result = await service.repairProductSkus('fxdd.dgjapp.com', { traceId: 'tr', dbname: '', products }); + assert.deepStrictEqual(result, { repaired: 0, failed: 0 }); + assert.strictEqual(called, 0); + }); + + it('补全失败时保留列表下发的规格并计入 failed', async () => { + const service = buildService(); + service.fetchProductSkuList = async () => { + throw new Error('平台超时'); + }; + const product = { PlatformId: 'a', ProductCode: 'pa', SkuCount: 6, Skus: [ { SkuId: '1' } ] }; + const result = await service.repairProductSkus('fxdd.dgjapp.com', { traceId: 'tr', dbname: '', products: [ product ] }); + assert.deepStrictEqual(result, { repaired: 0, failed: 1 }); + assert.strictEqual(product.Skus.length, 1, '失败时不能把原规格清空'); + }); +});