| 1234567891011121314151617181920212223242526272829303132333435 |
- import { CART_SPEC_SEPARATOR } from '@/constants/cart'
- /**
- * 详情多条规格 → 加购 specText 字符串
- * 例:「批次:2024春季§包装:袋装」
- */
- export function joinCartSpecParts(parts) {
- if (!Array.isArray(parts) || !parts.length) {
- return '默认'
- }
- const list = parts.map((p) => (p != null ? String(p).trim() : '')).filter(Boolean)
- return list.length ? list.join(CART_SPEC_SEPARATOR) : '默认'
- }
- /**
- * 购物车 specText → 展示用数组
- */
- export function parseCartSpecText(specText) {
- const text = (specText || '').trim()
- if (!text || text.includes('[object Object]')) {
- return ['默认']
- }
- if (text === '默认') {
- return ['默认']
- }
- if (text.includes(CART_SPEC_SEPARATOR)) {
- return text.split(CART_SPEC_SEPARATOR).map((s) => s.trim()).filter(Boolean)
- }
- // 兼容旧数据用「 / 」拼接的规格
- if (text.includes(' / ')) {
- return text.split(' / ').map((s) => s.trim()).filter(Boolean)
- }
- return [text]
- }
|