skuCombo.js 1014 B

1234567891011121314151617181920212223242526272829
  1. /**
  2. * SKU 组合工具函数(管理端商品选择器 / SKU 编辑器共用)
  3. */
  4. /**
  5. * 组合摘要:'商品A×2;商品B×1',超出 max 项追加 ' 等N项'。
  6. * items 为空/缺失时返回空串。
  7. */
  8. export function formatComboSummary(items, max = 3) {
  9. if (!items || !items.length) return ''
  10. const shown = items.slice(0, max).map(it => {
  11. const name = it.childProductName || it.name || ('商品#' + (it.childProductId || it.productId))
  12. const qty = it.quantity == null ? 1 : it.quantity
  13. return name + '×' + qty
  14. })
  15. return shown.join(';') + (items.length > max ? ' 等' + items.length + '项' : '')
  16. }
  17. /**
  18. * 组合保存载荷:归一化为 [{ childProductId, childSkuId, quantity }]。
  19. * childSkuId 为空时为 null(按互品扣减)。
  20. */
  21. export function normalizeComboPayload(items) {
  22. return (items || []).map(it => ({
  23. childProductId: it.childProductId || it.id,
  24. childSkuId: it.childSkuId || null,
  25. quantity: it.quantity == null ? 1 : it.quantity
  26. }))
  27. }