|
|
|
@ -20,6 +20,7 @@ Page({ |
|
|
|
currentY: 0, |
|
|
|
sidebarBtnTop: 500, |
|
|
|
sidebarBtnHidden: false, |
|
|
|
isPageHidden: false, |
|
|
|
|
|
|
|
// 搜索区域相关
|
|
|
|
searchSectionVisible: true, |
|
|
|
@ -41,7 +42,6 @@ Page({ |
|
|
|
filteredGoods: [], |
|
|
|
leftColumnGoods: [], |
|
|
|
rightColumnGoods: [], |
|
|
|
categorizedGoods: {}, // 按品种分类存储的商品数据
|
|
|
|
selectedCategory: '全部', |
|
|
|
categories: ['全部', '粉壳', '红壳', '绿壳', '白壳'], |
|
|
|
loadingMore: false, |
|
|
|
@ -238,25 +238,8 @@ Page({ |
|
|
|
lastScrollTop: 0 |
|
|
|
}); |
|
|
|
this.checkAndRestoreLoginStatus() |
|
|
|
|
|
|
|
API.getProductCategories().then(categories => { |
|
|
|
console.log('加载分类成功, categories:', JSON.stringify(categories)); |
|
|
|
if (categories && categories.length > 0) { |
|
|
|
this.setData({ |
|
|
|
categories: categories |
|
|
|
}); |
|
|
|
console.log('分类列表已设置到data, categories:', this.data.categories); |
|
|
|
} else { |
|
|
|
console.log('分类列表为空,使用默认分类'); |
|
|
|
} |
|
|
|
|
|
|
|
// 确保有分类数据后再加载商品
|
|
|
|
this.loadGoods(); |
|
|
|
}).catch(err => { |
|
|
|
console.error('加载分类失败:', err); |
|
|
|
// 即使分类加载失败也加载商品,使用默认分类
|
|
|
|
this.loadGoods(); |
|
|
|
}); |
|
|
|
this.loadCategories() |
|
|
|
this.loadGoods() |
|
|
|
|
|
|
|
// 计算搜索区域高度
|
|
|
|
setTimeout(() => { |
|
|
|
@ -327,9 +310,17 @@ Page({ |
|
|
|
|
|
|
|
const savedBtnHidden = wx.getStorageSync('sidebarBtnHidden'); |
|
|
|
console.log('onShow - savedBtnHidden:', savedBtnHidden); |
|
|
|
console.log('onShow - isPageHidden:', this.data.isPageHidden); |
|
|
|
|
|
|
|
// 只有在返回时才恢复按钮状态(刷新页面不恢复)
|
|
|
|
if (this.data.isPageHidden) { |
|
|
|
this.setData({ |
|
|
|
sidebarBtnHidden: savedBtnHidden || false, |
|
|
|
isPageHidden: false |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
sidebarBtnHidden: savedBtnHidden || false, |
|
|
|
isSearchBarFullyHidden: false, |
|
|
|
lastScrollTop: 0 |
|
|
|
}); |
|
|
|
@ -341,7 +332,8 @@ Page({ |
|
|
|
|
|
|
|
onHide: function () { |
|
|
|
this.setData({ |
|
|
|
sidebarBtnHidden: true |
|
|
|
sidebarBtnHidden: true, |
|
|
|
isPageHidden: true |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
@ -527,65 +519,26 @@ Page({ |
|
|
|
updatedGoods = newGoods |
|
|
|
} |
|
|
|
|
|
|
|
// 按品种分类存储商品数据
|
|
|
|
const categorizedGoods = this.data.categorizedGoods || {}; |
|
|
|
const categories = this.data.categories; |
|
|
|
const selectedCategory = this.data.selectedCategory; |
|
|
|
|
|
|
|
// 如果是首次加载全部数据,需要把返回的所有数据按实际分类存储
|
|
|
|
if (selectedCategory === '全部' && !isLoadMore) { |
|
|
|
categories.forEach(cat => { |
|
|
|
if (cat === '全部') { |
|
|
|
categorizedGoods[cat] = updatedGoods; |
|
|
|
} else { |
|
|
|
categorizedGoods[cat] = updatedGoods.filter(item => item.isAd || item.category === cat); |
|
|
|
} |
|
|
|
}); |
|
|
|
} else { |
|
|
|
// 只更新当前选中的分类
|
|
|
|
categories.forEach(cat => { |
|
|
|
if (cat === '全部') { |
|
|
|
categorizedGoods[cat] = updatedGoods; |
|
|
|
} else if (cat === selectedCategory) { |
|
|
|
categorizedGoods[cat] = updatedGoods.filter(item => item.isAd || !item.category || item.category === cat); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
// 应用地区筛选后的分类数据
|
|
|
|
const filteredByRegion = this.applyRegionFilter(updatedGoods); |
|
|
|
|
|
|
|
if (selectedCategory === '全部' && !isLoadMore) { |
|
|
|
categories.forEach(cat => { |
|
|
|
if (cat !== '全部') { |
|
|
|
categorizedGoods[cat + '_filtered'] = filteredByRegion.filter(item => item.isAd || item.category === cat); |
|
|
|
} |
|
|
|
}); |
|
|
|
} else { |
|
|
|
categories.forEach(cat => { |
|
|
|
if (cat !== '全部') { |
|
|
|
if (cat === selectedCategory) { |
|
|
|
categorizedGoods[cat + '_filtered'] = filteredByRegion.filter(item => item.isAd || !item.category || item.category === cat); |
|
|
|
} else { |
|
|
|
categorizedGoods[cat + '_filtered'] = []; |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
const filteredGoods = this.applyFilters(updatedGoods, false) |
|
|
|
console.log('===== 筛选结果 =====') |
|
|
|
console.log('当前选中的分类:', this.data.selectedCategory) |
|
|
|
console.log('filteredGoods 数量:', filteredGoods.length) |
|
|
|
console.log('filteredGoods[0]:', filteredGoods[0] ? JSON.stringify(filteredGoods[0], null, 2) : 'N/A') |
|
|
|
if (filteredGoods.length > 0) { |
|
|
|
console.log('filteredGoods[0].category:', filteredGoods[0] ? filteredGoods[0].category : 'N/A') |
|
|
|
} |
|
|
|
|
|
|
|
// 根据当前选中的分类和地区筛选获取商品
|
|
|
|
let filteredGoods; |
|
|
|
if (this.data.selectedRegion !== '全国') { |
|
|
|
filteredGoods = categorizedGoods[this.data.selectedCategory + '_filtered'] || []; |
|
|
|
} else { |
|
|
|
filteredGoods = categorizedGoods[this.data.selectedCategory] || []; |
|
|
|
if (filteredGoods.length > 1) { |
|
|
|
console.log('filteredGoods[1].category:', filteredGoods[1] ? filteredGoods[1].category : 'N/A') |
|
|
|
} |
|
|
|
|
|
|
|
const { leftColumnGoods, rightColumnGoods } = this.distributeToColumns(filteredGoods) |
|
|
|
console.log('leftColumnGoods 数量:', leftColumnGoods.length) |
|
|
|
console.log('rightColumnGoods 数量:', rightColumnGoods.length) |
|
|
|
console.log('leftColumnGoods[0]:', leftColumnGoods[0]) |
|
|
|
console.log('rightColumnGoods[0]:', rightColumnGoods[0]) |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
goods: updatedGoods, |
|
|
|
categorizedGoods: categorizedGoods, |
|
|
|
filteredGoods: filteredGoods, |
|
|
|
leftColumnGoods: leftColumnGoods, |
|
|
|
rightColumnGoods: rightColumnGoods, |
|
|
|
@ -738,16 +691,13 @@ Page({ |
|
|
|
const timestamp = new Date().getTime(); |
|
|
|
const currentPage = isLoadMore ? this.data.page : 1 |
|
|
|
|
|
|
|
// 首次加载时获取全部数据,在本地进行分类
|
|
|
|
const shouldLoadAll = !isLoadMore && this.data.selectedCategory === '全部'; |
|
|
|
|
|
|
|
API.getProductList('published', { |
|
|
|
timestamp: timestamp, |
|
|
|
viewMode: 'shopping', |
|
|
|
page: currentPage, |
|
|
|
pageSize: this.data.pageSize, |
|
|
|
keyword: this.data.searchKeyword, |
|
|
|
category: shouldLoadAll ? '' : this.data.selectedCategory |
|
|
|
category: this.data.selectedCategory === '全部' ? '' : this.data.selectedCategory |
|
|
|
}) |
|
|
|
.then(res => { |
|
|
|
wx.hideLoading(); |
|
|
|
@ -777,15 +727,6 @@ Page({ |
|
|
|
}) |
|
|
|
}, |
|
|
|
|
|
|
|
// 应用地区筛选
|
|
|
|
applyRegionFilter: function(goods) { |
|
|
|
if (this.data.selectedRegion === '全国') { |
|
|
|
return goods; |
|
|
|
} |
|
|
|
const selectedRegion = this.data.selectedRegion; |
|
|
|
return goods.filter(item => item.isAd || (item.region && item.region.includes(selectedRegion))); |
|
|
|
}, |
|
|
|
|
|
|
|
// 刷新商品列表
|
|
|
|
refreshGoodsList: function() { |
|
|
|
this.setData({ |
|
|
|
@ -981,37 +922,15 @@ Page({ |
|
|
|
app.globalData.showTabBar = true; |
|
|
|
} |
|
|
|
|
|
|
|
// 从本地分类数据中获取基础商品列表
|
|
|
|
const categorizedGoods = this.data.categorizedGoods || {}; |
|
|
|
let baseGoods; |
|
|
|
|
|
|
|
if (this.data.selectedRegion !== '全国') { |
|
|
|
baseGoods = categorizedGoods[this.data.selectedCategory + '_filtered'] || []; |
|
|
|
} else { |
|
|
|
baseGoods = categorizedGoods[this.data.selectedCategory] || []; |
|
|
|
} |
|
|
|
|
|
|
|
// 在本地数据上进行关键词搜索
|
|
|
|
let filteredGoods = [...baseGoods]; |
|
|
|
|
|
|
|
if (this.data.searchKeyword) { |
|
|
|
const keyword = this.data.searchKeyword.toLowerCase(); |
|
|
|
filteredGoods = filteredGoods.filter(item => |
|
|
|
item.isAd || |
|
|
|
(item.name || '').toLowerCase().includes(keyword) || |
|
|
|
(item.region || '').toLowerCase().includes(keyword) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
const groupedGoods = this.groupGoodsForStaggeredLayout(filteredGoods); |
|
|
|
const { leftColumnGoods, rightColumnGoods } = this.distributeToColumns(filteredGoods); |
|
|
|
|
|
|
|
const filteredGoods = this.applyFilters(this.data.goods, false) |
|
|
|
const groupedGoods = this.groupGoodsForStaggeredLayout(filteredGoods) |
|
|
|
const { leftColumnGoods, rightColumnGoods } = this.distributeToColumns(filteredGoods) |
|
|
|
this.setData({ |
|
|
|
filteredGoods: filteredGoods, |
|
|
|
groupedGoods: groupedGoods, |
|
|
|
leftColumnGoods: leftColumnGoods, |
|
|
|
rightColumnGoods: rightColumnGoods |
|
|
|
}); |
|
|
|
}) |
|
|
|
}, |
|
|
|
|
|
|
|
// 切换地区选择器
|
|
|
|
@ -1025,42 +944,34 @@ Page({ |
|
|
|
selectRegion: function(e) { |
|
|
|
const region = e.currentTarget.dataset.region |
|
|
|
|
|
|
|
// 立即关闭弹窗
|
|
|
|
this.setData({ |
|
|
|
selectedRegion: region, |
|
|
|
showRegionPicker: false |
|
|
|
}) |
|
|
|
|
|
|
|
// 重新显示tabBar
|
|
|
|
const app = getApp(); |
|
|
|
if (app && app.globalData) { |
|
|
|
app.globalData.showTabBar = true; |
|
|
|
} |
|
|
|
|
|
|
|
const categorizedGoods = this.data.categorizedGoods || {}; |
|
|
|
let filteredGoods; |
|
|
|
|
|
|
|
if (region === '全国') { |
|
|
|
filteredGoods = categorizedGoods[this.data.selectedCategory] || []; |
|
|
|
// 如果从局部地区切换到全国地区,重新加载所有商品
|
|
|
|
if (region === '全国' && this.data.selectedCategory === '全部' && !this.data.searchKeyword) { |
|
|
|
// 重新加载商品数据
|
|
|
|
this.refreshGoodsList(); |
|
|
|
} else { |
|
|
|
filteredGoods = categorizedGoods[this.data.selectedCategory + '_filtered'] || []; |
|
|
|
} |
|
|
|
|
|
|
|
if (filteredGoods.length === 0 && Object.keys(categorizedGoods).length === 0) { |
|
|
|
// 否则仅对本地商品进行筛选
|
|
|
|
const filteredGoods = this.applyFilters(this.data.goods, false) |
|
|
|
const groupedGoods = this.groupGoodsForStaggeredLayout(filteredGoods) |
|
|
|
const { leftColumnGoods, rightColumnGoods } = this.distributeToColumns(filteredGoods) |
|
|
|
this.setData({ |
|
|
|
page: 1, |
|
|
|
hasMoreData: true |
|
|
|
}, () => { |
|
|
|
this.loadGoods(); |
|
|
|
}); |
|
|
|
return; |
|
|
|
filteredGoods: filteredGoods, |
|
|
|
groupedGoods: groupedGoods, |
|
|
|
leftColumnGoods: leftColumnGoods, |
|
|
|
rightColumnGoods: rightColumnGoods |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
const { leftColumnGoods, rightColumnGoods } = this.distributeToColumns(filteredGoods) |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
filteredGoods: filteredGoods, |
|
|
|
leftColumnGoods: leftColumnGoods, |
|
|
|
rightColumnGoods: rightColumnGoods |
|
|
|
}) |
|
|
|
}, |
|
|
|
|
|
|
|
// 阻止事件冒泡
|
|
|
|
@ -1070,6 +981,7 @@ Page({ |
|
|
|
|
|
|
|
// 选择品种
|
|
|
|
selectCategory: function(e) { |
|
|
|
// 重新显示tabBar
|
|
|
|
const app = getApp(); |
|
|
|
if (app && app.globalData) { |
|
|
|
app.globalData.showTabBar = true; |
|
|
|
@ -1077,37 +989,23 @@ Page({ |
|
|
|
|
|
|
|
const category = e.currentTarget.dataset.category |
|
|
|
if (category === this.data.selectedCategory) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
const categorizedGoods = this.data.categorizedGoods || {}; |
|
|
|
let filteredGoods; |
|
|
|
|
|
|
|
if (this.data.selectedRegion !== '全国') { |
|
|
|
filteredGoods = categorizedGoods[category + '_filtered'] || []; |
|
|
|
} else { |
|
|
|
filteredGoods = categorizedGoods[category] || []; |
|
|
|
} |
|
|
|
|
|
|
|
if (filteredGoods.length === 0 && Object.keys(categorizedGoods).length === 0) { |
|
|
|
this.setData({ |
|
|
|
selectedCategory: category, |
|
|
|
page: 1, |
|
|
|
hasMoreData: true |
|
|
|
}, () => { |
|
|
|
this.loadGoods(); |
|
|
|
}); |
|
|
|
return; |
|
|
|
return // 如果选择的分类和当前相同,不重复加载
|
|
|
|
} |
|
|
|
|
|
|
|
const { leftColumnGoods, rightColumnGoods } = this.distributeToColumns(filteredGoods) |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
selectedCategory: category, |
|
|
|
filteredGoods: filteredGoods, |
|
|
|
leftColumnGoods: leftColumnGoods, |
|
|
|
rightColumnGoods: rightColumnGoods |
|
|
|
page: 1, |
|
|
|
hasMoreData: true, |
|
|
|
goods: [], |
|
|
|
filteredGoods: [], |
|
|
|
leftColumnGoods: [], |
|
|
|
rightColumnGoods: [], |
|
|
|
loadingMore: false, |
|
|
|
isLoading: true |
|
|
|
}) |
|
|
|
|
|
|
|
// 重新从服务器加载数据
|
|
|
|
this.loadGoods(false) |
|
|
|
}, |
|
|
|
|
|
|
|
// 查看商品详情
|
|
|
|
|