bundle-pricing.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * 购物车套餐offer展示辅助(纯函数)
  3. *
  4. * 权威计算在后端:POST /api/cart/combo-offer 按勾选子集检测 offer,
  5. * 前端仅消费后端下发的 comboOffer 做提示与「一键替换为套餐」,不做任何金额扣减。
  6. */
  7. /**
  8. * 安全读取套餐offer:未命中/未激活/非法值一律返回 null。
  9. * @param {Object|null} offer 后端下发的 comboOffer
  10. * @returns {Object|null}
  11. */
  12. export function comboOfferOf(offer) {
  13. if (!offer || offer.active !== true) return null
  14. return offer
  15. }
  16. /**
  17. * 套餐offer提示文案:这N件组成套餐「XX」,套餐价 ¥Y(省 ¥Z)
  18. * @param {Object|null} offer 后端下发的 comboOffer
  19. * @param {number} [checkedCount] 勾选子集件数(后端响应未含,由前端传入)
  20. * @returns {string}
  21. */
  22. export function formatComboOfferText(offer, checkedCount) {
  23. const o = comboOfferOf(offer)
  24. if (!o) return ''
  25. const n = (typeof checkedCount === 'number' && checkedCount > 0) ? checkedCount
  26. : ((typeof o.groups === 'number' && o.groups > 0) ? o.groups : 0)
  27. const name = o.comboName || ''
  28. const price = (typeof o.comboPrice === 'number' && o.comboPrice >= 0) ? o.comboPrice : 0
  29. const saving = (typeof o.saving === 'number' && o.saving > 0) ? o.saving : 0
  30. let txt = ''
  31. if (n) txt += n + ' 件'
  32. if (name) txt += '组成套餐「' + name + '」'
  33. txt += ',套餐价 ¥' + (price / 100).toFixed(2)
  34. if (saving) txt += '(省 ¥' + (saving / 100).toFixed(2) + ')'
  35. return txt
  36. }