/**
|
* 本地商品/品牌过滤(基于全量缓存)
|
*/
|
|
export function matchKeyword(item, keyword) {
|
if (!keyword) return true
|
const kw = String(keyword).trim()
|
if (!kw) return true
|
const kwLower = kw.toLowerCase()
|
const name = item.name != null ? String(item.name).toLowerCase() : ''
|
const pinyin = item.pinyin != null ? String(item.pinyin).toLowerCase() : ''
|
const shortPinyin = item.shortPinyin != null ? String(item.shortPinyin).toLowerCase() : ''
|
return name.includes(kwLower) ||
|
pinyin.includes(kwLower) ||
|
shortPinyin.includes(kwLower)
|
}
|
|
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)))
|
}
|