巴青农资商城

cartSpec.js 995B

1234567891011121314151617181920212223242526272829303132333435
  1. import { CART_SPEC_SEPARATOR } from '@/constants/cart'
  2. /**
  3. * 详情多条规格 → 加购 specText 字符串
  4. * 例:「批次:2024春季§包装:袋装」
  5. */
  6. export function joinCartSpecParts(parts) {
  7. if (!Array.isArray(parts) || !parts.length) {
  8. return '默认'
  9. }
  10. const list = parts.map((p) => (p != null ? String(p).trim() : '')).filter(Boolean)
  11. return list.length ? list.join(CART_SPEC_SEPARATOR) : '默认'
  12. }
  13. /**
  14. * 购物车 specText → 展示用数组
  15. */
  16. export function parseCartSpecText(specText) {
  17. const text = (specText || '').trim()
  18. if (!text || text.includes('[object Object]')) {
  19. return ['默认']
  20. }
  21. if (text === '默认') {
  22. return ['默认']
  23. }
  24. if (text.includes(CART_SPEC_SEPARATOR)) {
  25. return text.split(CART_SPEC_SEPARATOR).map((s) => s.trim()).filter(Boolean)
  26. }
  27. // 兼容旧数据用「 / 」拼接的规格
  28. if (text.includes(' / ')) {
  29. return text.split(' / ').map((s) => s.trim()).filter(Boolean)
  30. }
  31. return [text]
  32. }