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'), }; });