/** * 本地商品/品牌过滤(基于全量缓存) */ export function matchKeyword(item, keyword) { if (!keyword) return true const kw = String(keyword).trim() if (!kw) return true return (item.name && item.name.indexOf(kw) !== -1) || (item.pinyin && item.pinyin.indexOf(kw) !== -1) || (item.shortPinyin && item.shortPinyin.indexOf(kw) !== -1) } export function filterGoods(allGoods, options = {}) { const { categoryId, subCategoryId, brandId, keyword } = options const list = Array.isArray(allGoods) ? allGoods : [] return list.filter(item => { if (categoryId != null && categoryId !== '' && String(item.categoryId) !== String(categoryId)) { return false } if (subCategoryId != null && subCategoryId !== '' && String(item.subCategoryId) !== String(subCategoryId)) { return false } if (brandId != null && brandId !== '' && String(item.brandId) !== String(brandId)) { return false } return matchKeyword(item, keyword) }) } export function filterBrandsByCategory(allBrands, allGoods, categoryId) { if (!categoryId) { return Array.isArray(allBrands) ? allBrands : [] } const goods = Array.isArray(allGoods) ? allGoods : [] const brands = Array.isArray(allBrands) ? allBrands : [] const brandIds = new Set( goods .filter(g => String(g.categoryId) === String(categoryId) && g.brandId != null && g.brandId !== '') .map(g => String(g.brandId)) ) return brands.filter(b => brandIds.has(String(b.id))) }