|
|
@ -20,7 +20,6 @@ Page({ |
|
|
currentY: 0, |
|
|
currentY: 0, |
|
|
sidebarBtnTop: 500, |
|
|
sidebarBtnTop: 500, |
|
|
sidebarBtnHidden: false, |
|
|
sidebarBtnHidden: false, |
|
|
userManuallyHidden: false, |
|
|
|
|
|
|
|
|
|
|
|
// 搜索区域相关
|
|
|
// 搜索区域相关
|
|
|
searchSectionVisible: true, |
|
|
searchSectionVisible: true, |
|
|
@ -42,6 +41,7 @@ Page({ |
|
|
filteredGoods: [], |
|
|
filteredGoods: [], |
|
|
leftColumnGoods: [], |
|
|
leftColumnGoods: [], |
|
|
rightColumnGoods: [], |
|
|
rightColumnGoods: [], |
|
|
|
|
|
categorizedGoods: {}, // 按品种分类存储的商品数据
|
|
|
selectedCategory: '全部', |
|
|
selectedCategory: '全部', |
|
|
categories: ['全部', '粉壳', '红壳', '绿壳', '白壳'], |
|
|
categories: ['全部', '粉壳', '红壳', '绿壳', '白壳'], |
|
|
loadingMore: false, |
|
|
loadingMore: false, |
|
|
@ -129,19 +129,14 @@ Page({ |
|
|
// 如果当前不在顶部,点击则隐藏按钮
|
|
|
// 如果当前不在顶部,点击则隐藏按钮
|
|
|
if (this.data.lastScrollTop > 50) { |
|
|
if (this.data.lastScrollTop > 50) { |
|
|
this.setData({ |
|
|
this.setData({ |
|
|
sidebarBtnHidden: true, |
|
|
sidebarBtnHidden: true |
|
|
userManuallyHidden: true |
|
|
|
|
|
}); |
|
|
}); |
|
|
wx.setStorageSync('sidebarBtnHidden', true); |
|
|
wx.setStorageSync('sidebarBtnHidden', true); |
|
|
wx.setStorageSync('userManuallyHidden', true); |
|
|
|
|
|
} else { |
|
|
} else { |
|
|
const newHiddenState = !this.data.sidebarBtnHidden; |
|
|
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
sidebarBtnHidden: newHiddenState, |
|
|
sidebarBtnHidden: !this.data.sidebarBtnHidden |
|
|
userManuallyHidden: newHiddenState |
|
|
|
|
|
}); |
|
|
}); |
|
|
wx.setStorageSync('sidebarBtnHidden', newHiddenState); |
|
|
wx.setStorageSync('sidebarBtnHidden', !this.data.sidebarBtnHidden); |
|
|
wx.setStorageSync('userManuallyHidden', newHiddenState); |
|
|
|
|
|
} |
|
|
} |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
@ -194,17 +189,13 @@ Page({ |
|
|
|
|
|
|
|
|
if (Math.abs(diffY) > screenHeight / 3) { |
|
|
if (Math.abs(diffY) > screenHeight / 3) { |
|
|
this.setData({ |
|
|
this.setData({ |
|
|
sidebarBtnTop: -100, // 隐藏按钮
|
|
|
sidebarBtnTop: -100 // 隐藏按钮
|
|
|
sidebarBtnHidden: true, |
|
|
|
|
|
userManuallyHidden: true |
|
|
|
|
|
}); |
|
|
}); |
|
|
wx.setStorageSync('sidebarBtnTop', -100); |
|
|
wx.setStorageSync('sidebarBtnTop', -100); |
|
|
wx.setStorageSync('sidebarBtnHidden', true); |
|
|
wx.setStorageSync('sidebarBtnHidden', true); |
|
|
wx.setStorageSync('userManuallyHidden', true); |
|
|
|
|
|
} else { |
|
|
} else { |
|
|
wx.setStorageSync('sidebarBtnTop', this.data.sidebarBtnTop); |
|
|
wx.setStorageSync('sidebarBtnTop', this.data.sidebarBtnTop); |
|
|
wx.setStorageSync('sidebarBtnHidden', false); |
|
|
wx.setStorageSync('sidebarBtnHidden', false); |
|
|
wx.setStorageSync('userManuallyHidden', false); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
@ -230,7 +221,6 @@ Page({ |
|
|
|
|
|
|
|
|
const savedBtnTop = wx.getStorageSync('sidebarBtnTop'); |
|
|
const savedBtnTop = wx.getStorageSync('sidebarBtnTop'); |
|
|
const savedBtnHidden = wx.getStorageSync('sidebarBtnHidden'); |
|
|
const savedBtnHidden = wx.getStorageSync('sidebarBtnHidden'); |
|
|
const savedUserManuallyHidden = wx.getStorageSync('userManuallyHidden'); |
|
|
|
|
|
|
|
|
|
|
|
let defaultTop = screenHeight * 0.5; |
|
|
let defaultTop = screenHeight * 0.5; |
|
|
if (savedBtnTop !== '' && savedBtnTop !== -100) { |
|
|
if (savedBtnTop !== '' && savedBtnTop !== -100) { |
|
|
@ -243,14 +233,30 @@ Page({ |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
sidebarBtnTop: defaultTop, |
|
|
sidebarBtnTop: defaultTop, |
|
|
sidebarBtnHidden: savedBtnHidden || false, |
|
|
sidebarBtnHidden: false, |
|
|
userManuallyHidden: savedUserManuallyHidden || false, |
|
|
|
|
|
isSearchBarFullyHidden: false, |
|
|
isSearchBarFullyHidden: false, |
|
|
lastScrollTop: 0 |
|
|
lastScrollTop: 0 |
|
|
}); |
|
|
}); |
|
|
this.checkAndRestoreLoginStatus() |
|
|
this.checkAndRestoreLoginStatus() |
|
|
this.loadCategories() |
|
|
|
|
|
this.loadGoods() |
|
|
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(); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
// 计算搜索区域高度
|
|
|
// 计算搜索区域高度
|
|
|
setTimeout(() => { |
|
|
setTimeout(() => { |
|
|
@ -521,26 +527,41 @@ Page({ |
|
|
updatedGoods = newGoods |
|
|
updatedGoods = newGoods |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const filteredGoods = this.applyFilters(updatedGoods, false) |
|
|
// 按品种分类存储商品数据
|
|
|
console.log('===== 筛选结果 =====') |
|
|
const categorizedGoods = this.data.categorizedGoods || {}; |
|
|
console.log('当前选中的分类:', this.data.selectedCategory) |
|
|
const categories = this.data.categories; |
|
|
console.log('filteredGoods 数量:', filteredGoods.length) |
|
|
|
|
|
console.log('filteredGoods[0]:', filteredGoods[0] ? JSON.stringify(filteredGoods[0], null, 2) : 'N/A') |
|
|
// 重置所有分类的商品列表
|
|
|
if (filteredGoods.length > 0) { |
|
|
categories.forEach(cat => { |
|
|
console.log('filteredGoods[0].category:', filteredGoods[0] ? filteredGoods[0].category : 'N/A') |
|
|
if (cat === '全部') { |
|
|
|
|
|
categorizedGoods[cat] = updatedGoods; |
|
|
|
|
|
} else { |
|
|
|
|
|
const categoryGoods = updatedGoods.filter(item => item.isAd || item.category === cat); |
|
|
|
|
|
categorizedGoods[cat] = categoryGoods; |
|
|
} |
|
|
} |
|
|
if (filteredGoods.length > 1) { |
|
|
}); |
|
|
console.log('filteredGoods[1].category:', filteredGoods[1] ? filteredGoods[1].category : 'N/A') |
|
|
|
|
|
|
|
|
// 应用地区筛选后的分类数据
|
|
|
|
|
|
const filteredByRegion = this.applyRegionFilter(updatedGoods); |
|
|
|
|
|
categories.forEach(cat => { |
|
|
|
|
|
if (cat !== '全部') { |
|
|
|
|
|
categorizedGoods[cat + '_filtered'] = filteredByRegion.filter(item => item.isAd || item.category === cat); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 根据当前选中的分类和地区筛选获取商品
|
|
|
|
|
|
let filteredGoods; |
|
|
|
|
|
if (this.data.selectedRegion !== '全国') { |
|
|
|
|
|
filteredGoods = categorizedGoods[this.data.selectedCategory + '_filtered'] || []; |
|
|
|
|
|
} else { |
|
|
|
|
|
filteredGoods = categorizedGoods[this.data.selectedCategory] || []; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const { leftColumnGoods, rightColumnGoods } = this.distributeToColumns(filteredGoods) |
|
|
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({ |
|
|
this.setData({ |
|
|
goods: updatedGoods, |
|
|
goods: updatedGoods, |
|
|
|
|
|
categorizedGoods: categorizedGoods, |
|
|
filteredGoods: filteredGoods, |
|
|
filteredGoods: filteredGoods, |
|
|
leftColumnGoods: leftColumnGoods, |
|
|
leftColumnGoods: leftColumnGoods, |
|
|
rightColumnGoods: rightColumnGoods, |
|
|
rightColumnGoods: rightColumnGoods, |
|
|
@ -729,6 +750,15 @@ 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() { |
|
|
refreshGoodsList: function() { |
|
|
this.setData({ |
|
|
this.setData({ |
|
|
@ -924,15 +954,37 @@ Page({ |
|
|
app.globalData.showTabBar = true; |
|
|
app.globalData.showTabBar = true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const filteredGoods = this.applyFilters(this.data.goods, false) |
|
|
// 从本地分类数据中获取基础商品列表
|
|
|
const groupedGoods = this.groupGoodsForStaggeredLayout(filteredGoods) |
|
|
const categorizedGoods = this.data.categorizedGoods || {}; |
|
|
const { leftColumnGoods, rightColumnGoods } = this.distributeToColumns(filteredGoods) |
|
|
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); |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
filteredGoods: filteredGoods, |
|
|
filteredGoods: filteredGoods, |
|
|
groupedGoods: groupedGoods, |
|
|
groupedGoods: groupedGoods, |
|
|
leftColumnGoods: leftColumnGoods, |
|
|
leftColumnGoods: leftColumnGoods, |
|
|
rightColumnGoods: rightColumnGoods |
|
|
rightColumnGoods: rightColumnGoods |
|
|
}) |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 切换地区选择器
|
|
|
// 切换地区选择器
|
|
|
@ -946,34 +998,42 @@ Page({ |
|
|
selectRegion: function(e) { |
|
|
selectRegion: function(e) { |
|
|
const region = e.currentTarget.dataset.region |
|
|
const region = e.currentTarget.dataset.region |
|
|
|
|
|
|
|
|
// 立即关闭弹窗
|
|
|
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
selectedRegion: region, |
|
|
selectedRegion: region, |
|
|
showRegionPicker: false |
|
|
showRegionPicker: false |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
// 重新显示tabBar
|
|
|
|
|
|
const app = getApp(); |
|
|
const app = getApp(); |
|
|
if (app && app.globalData) { |
|
|
if (app && app.globalData) { |
|
|
app.globalData.showTabBar = true; |
|
|
app.globalData.showTabBar = true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 如果从局部地区切换到全国地区,重新加载所有商品
|
|
|
const categorizedGoods = this.data.categorizedGoods || {}; |
|
|
if (region === '全国' && this.data.selectedCategory === '全部' && !this.data.searchKeyword) { |
|
|
let filteredGoods; |
|
|
// 重新加载商品数据
|
|
|
|
|
|
this.refreshGoodsList(); |
|
|
if (region === '全国') { |
|
|
|
|
|
filteredGoods = categorizedGoods[this.data.selectedCategory] || []; |
|
|
} else { |
|
|
} else { |
|
|
// 否则仅对本地商品进行筛选
|
|
|
filteredGoods = categorizedGoods[this.data.selectedCategory + '_filtered'] || []; |
|
|
const filteredGoods = this.applyFilters(this.data.goods, false) |
|
|
} |
|
|
const groupedGoods = this.groupGoodsForStaggeredLayout(filteredGoods) |
|
|
|
|
|
|
|
|
if (filteredGoods.length === 0 && Object.keys(categorizedGoods).length === 0) { |
|
|
|
|
|
this.setData({ |
|
|
|
|
|
page: 1, |
|
|
|
|
|
hasMoreData: true |
|
|
|
|
|
}, () => { |
|
|
|
|
|
this.loadGoods(); |
|
|
|
|
|
}); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
const { leftColumnGoods, rightColumnGoods } = this.distributeToColumns(filteredGoods) |
|
|
const { leftColumnGoods, rightColumnGoods } = this.distributeToColumns(filteredGoods) |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
filteredGoods: filteredGoods, |
|
|
filteredGoods: filteredGoods, |
|
|
groupedGoods: groupedGoods, |
|
|
|
|
|
leftColumnGoods: leftColumnGoods, |
|
|
leftColumnGoods: leftColumnGoods, |
|
|
rightColumnGoods: rightColumnGoods |
|
|
rightColumnGoods: rightColumnGoods |
|
|
}) |
|
|
}) |
|
|
} |
|
|
|
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 阻止事件冒泡
|
|
|
// 阻止事件冒泡
|
|
|
@ -983,7 +1043,6 @@ Page({ |
|
|
|
|
|
|
|
|
// 选择品种
|
|
|
// 选择品种
|
|
|
selectCategory: function(e) { |
|
|
selectCategory: function(e) { |
|
|
// 重新显示tabBar
|
|
|
|
|
|
const app = getApp(); |
|
|
const app = getApp(); |
|
|
if (app && app.globalData) { |
|
|
if (app && app.globalData) { |
|
|
app.globalData.showTabBar = true; |
|
|
app.globalData.showTabBar = true; |
|
|
@ -991,23 +1050,37 @@ Page({ |
|
|
|
|
|
|
|
|
const category = e.currentTarget.dataset.category |
|
|
const category = e.currentTarget.dataset.category |
|
|
if (category === this.data.selectedCategory) { |
|
|
if (category === this.data.selectedCategory) { |
|
|
return // 如果选择的分类和当前相同,不重复加载
|
|
|
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({ |
|
|
this.setData({ |
|
|
selectedCategory: category, |
|
|
selectedCategory: category, |
|
|
page: 1, |
|
|
page: 1, |
|
|
hasMoreData: true, |
|
|
hasMoreData: true |
|
|
goods: [], |
|
|
}, () => { |
|
|
filteredGoods: [], |
|
|
this.loadGoods(); |
|
|
leftColumnGoods: [], |
|
|
}); |
|
|
rightColumnGoods: [], |
|
|
return; |
|
|
loadingMore: false, |
|
|
} |
|
|
isLoading: true |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
// 重新从服务器加载数据
|
|
|
const { leftColumnGoods, rightColumnGoods } = this.distributeToColumns(filteredGoods) |
|
|
this.loadGoods(false) |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
selectedCategory: category, |
|
|
|
|
|
filteredGoods: filteredGoods, |
|
|
|
|
|
leftColumnGoods: leftColumnGoods, |
|
|
|
|
|
rightColumnGoods: rightColumnGoods |
|
|
|
|
|
}) |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 查看商品详情
|
|
|
// 查看商品详情
|
|
|
@ -1137,8 +1210,7 @@ Page({ |
|
|
// 搜索框始终固定显示,不做隐藏处理
|
|
|
// 搜索框始终固定显示,不做隐藏处理
|
|
|
|
|
|
|
|
|
// 侧边栏按钮显示逻辑
|
|
|
// 侧边栏按钮显示逻辑
|
|
|
// 如果用户手动隐藏过,滚动到顶部也不再自动显示
|
|
|
if (scrollTop <= threshold && this.data.sidebarBtnHidden) { |
|
|
if (scrollTop <= threshold && this.data.sidebarBtnHidden && !this.data.userManuallyHidden) { |
|
|
|
|
|
updates.sidebarBtnHidden = false; |
|
|
updates.sidebarBtnHidden = false; |
|
|
needUpdate = true; |
|
|
needUpdate = true; |
|
|
wx.setStorageSync('sidebarBtnHidden', false); |
|
|
wx.setStorageSync('sidebarBtnHidden', false); |
|
|
|