From bc575840b2a50e0eef1aadb73a836dbe374fff1c Mon Sep 17 00:00:00 2001 From: tikkhun Date: Fri, 28 Nov 2025 16:58:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E4=BA=A7=E5=93=81=E6=9C=8D=E5=8A=A1):?= =?UTF-8?q?=20=E9=87=8D=E6=9E=84=E4=BA=A7=E5=93=81=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E9=80=BB=E8=BE=91=EF=BC=8C=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E8=BF=94=E5=9B=9E=E5=AD=97=E5=85=B8=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将品牌/口味/规格等属性从返回标题改为直接返回完整的 DictItem 对象 保留原 attributes 列表以便前端灵活使用 --- src/service/product.service.ts | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/service/product.service.ts b/src/service/product.service.ts index 2ecc4e0..b5d20b6 100644 --- a/src/service/product.service.ts +++ b/src/service/product.service.ts @@ -174,28 +174,36 @@ export class ProductService { return map; }, {}); - // 格式化返回的数据 + // 格式化返回的数据(中文注释:将品牌/口味/规格/尺寸均以 DictItem 对象形式返回,并保留 attributes 列表) const formattedItems = items.map(product => { - const getAttributeTitle = (dictName: string) => - product.attributes.find(a => a.dict.name === dictName)?.title || null; + // 函数(中文注释:按字典名称获取对应的属性对象) + const getAttributeByDict = (dictName: string) => + product.attributes.find(a => a.dict?.name === dictName) || null; + // 条件判断(中文注释:从属性中取出各类维度对象) + const brand = getAttributeByDict('brand'); + const flavors = getAttributeByDict('flavor'); + const strength = getAttributeByDict('strength'); + const size = getAttributeByDict('size'); return { id: product.id, name: product.name, nameCn: product.nameCn, description: product.description, - humidity: getAttributeTitle('humidity'), sku: product.sku, - stock: stockMap[product.sku] || 0, // 使用映射的库存或默认为 0 + stock: stockMap[product.sku] || 0, // 中文注释:库存使用聚合库存值 price: product.price, promotionPrice: product.promotionPrice, - source: product.source, // 中文注释:补充返回产品来源字段 + source: product.source, // 中文注释:返回产品来源字段 createdAt: product.createdAt, updatedAt: product.updatedAt, + // 单列属性(中文注释:直接返回 DictItem 对象,方便前端展示与使用) + brand, + flavors, + strength, + size, + // 全量属性列表(中文注释:保留原 attributes,包含字典关系) attributes: product.attributes, - brandName: getAttributeTitle('brand'), - flavorsName: getAttributeTitle('flavor'), - strengthName: getAttributeTitle('strength'), }; });