|
|
@ -111,6 +111,9 @@ Page({ |
|
|
// 防抖定时器
|
|
|
// 防抖定时器
|
|
|
searchDebounceTimer: null, |
|
|
searchDebounceTimer: null, |
|
|
scrollDebounceTimer: null, |
|
|
scrollDebounceTimer: null, |
|
|
|
|
|
filterDebounceTimer: null, |
|
|
|
|
|
// 请求任务管理
|
|
|
|
|
|
currentRequestTask: null, |
|
|
isRefreshing: false, |
|
|
isRefreshing: false, |
|
|
isLoading: true, |
|
|
isLoading: true, |
|
|
|
|
|
|
|
|
@ -436,6 +439,11 @@ Page({ |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 保存当前筛选条件,用于后续验证一致性
|
|
|
|
|
|
const currentCategory = this.data.selectedCategory; |
|
|
|
|
|
const currentKeyword = this.data.searchKeyword; |
|
|
|
|
|
const currentRegion = this.data.selectedRegion; |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
isRefreshing: true, |
|
|
isRefreshing: true, |
|
|
page: 1, |
|
|
page: 1, |
|
|
@ -446,11 +454,14 @@ Page({ |
|
|
// 清除缓存以确保获取最新数据
|
|
|
// 清除缓存以确保获取最新数据
|
|
|
categoryQueryCache: {}, |
|
|
categoryQueryCache: {}, |
|
|
lastDataTimestamp: 0, |
|
|
lastDataTimestamp: 0, |
|
|
|
|
|
// 保存当前筛选条件,用于验证请求结果
|
|
|
|
|
|
refreshCategory: currentCategory, |
|
|
|
|
|
refreshKeyword: currentKeyword, |
|
|
|
|
|
refreshRegion: currentRegion |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
const timestamp = new Date().getTime(); |
|
|
const timestamp = new Date().getTime(); |
|
|
const currentCategory = this.data.selectedCategory === '全部' ? '' : this.data.selectedCategory; |
|
|
const categoryParam = currentCategory === '全部' ? '' : currentCategory; |
|
|
const currentKeyword = this.data.searchKeyword; |
|
|
|
|
|
const pageSize = this.data.pageSize; |
|
|
const pageSize = this.data.pageSize; |
|
|
|
|
|
|
|
|
// 强制刷新:清除所有缓存并重新从数据库加载
|
|
|
// 强制刷新:清除所有缓存并重新从数据库加载
|
|
|
@ -471,11 +482,20 @@ Page({ |
|
|
keyword: currentKeyword |
|
|
keyword: currentKeyword |
|
|
} |
|
|
} |
|
|
// 只有非全部分类时才传递category参数
|
|
|
// 只有非全部分类时才传递category参数
|
|
|
if (currentCategory) { |
|
|
if (categoryParam) { |
|
|
apiParams.category = currentCategory |
|
|
apiParams.category = categoryParam |
|
|
} |
|
|
} |
|
|
API.getProductList(statusList, apiParams) |
|
|
API.getProductList(statusList, apiParams) |
|
|
.then(res => { |
|
|
.then(res => { |
|
|
|
|
|
// 验证当前筛选条件是否与刷新开始时一致
|
|
|
|
|
|
if (currentCategory !== this.data.selectedCategory || |
|
|
|
|
|
currentKeyword !== this.data.searchKeyword || |
|
|
|
|
|
currentRegion !== this.data.selectedRegion) { |
|
|
|
|
|
console.log('筛选条件已变更,忽略此次刷新结果'); |
|
|
|
|
|
this.setData({ isRefreshing: false }); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
this.setData({ isRefreshing: false }) |
|
|
this.setData({ isRefreshing: false }) |
|
|
|
|
|
|
|
|
if (res.success && res.products) { |
|
|
if (res.success && res.products) { |
|
|
@ -496,11 +516,20 @@ Page({ |
|
|
keyword: currentKeyword |
|
|
keyword: currentKeyword |
|
|
} |
|
|
} |
|
|
// 只有非全部分类时才传递category参数
|
|
|
// 只有非全部分类时才传递category参数
|
|
|
if (currentCategory) { |
|
|
if (categoryParam) { |
|
|
apiParams.category = currentCategory |
|
|
apiParams.category = categoryParam |
|
|
} |
|
|
} |
|
|
API.getProductList(['sold_out'], apiParams) |
|
|
API.getProductList(['sold_out'], apiParams) |
|
|
.then(soldOutRes => { |
|
|
.then(soldOutRes => { |
|
|
|
|
|
// 验证当前筛选条件是否与刷新开始时一致
|
|
|
|
|
|
if (currentCategory !== this.data.selectedCategory || |
|
|
|
|
|
currentKeyword !== this.data.searchKeyword || |
|
|
|
|
|
currentRegion !== this.data.selectedRegion) { |
|
|
|
|
|
console.log('筛选条件已变更,忽略此次刷新结果(sold_out)'); |
|
|
|
|
|
this.setData({ isRefreshing: false }); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
this.setData({ isRefreshing: false }); |
|
|
this.setData({ isRefreshing: false }); |
|
|
|
|
|
|
|
|
if (soldOutRes.success && soldOutRes.products && soldOutRes.products.length > 0) { |
|
|
if (soldOutRes.success && soldOutRes.products && soldOutRes.products.length > 0) { |
|
|
@ -534,6 +563,15 @@ Page({ |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
.catch(err => { |
|
|
.catch(err => { |
|
|
|
|
|
// 验证当前筛选条件是否与刷新开始时一致
|
|
|
|
|
|
if (currentCategory !== this.data.selectedCategory || |
|
|
|
|
|
currentKeyword !== this.data.searchKeyword || |
|
|
|
|
|
currentRegion !== this.data.selectedRegion) { |
|
|
|
|
|
console.log('筛选条件已变更,忽略此次刷新错误'); |
|
|
|
|
|
this.setData({ isRefreshing: false }); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
this.setData({ isRefreshing: false }) |
|
|
this.setData({ isRefreshing: false }) |
|
|
console.error('刷新商品数据失败:', err) |
|
|
console.error('刷新商品数据失败:', err) |
|
|
wx.showToast({ |
|
|
wx.showToast({ |
|
|
@ -1083,6 +1121,13 @@ Page({ |
|
|
const statusList = ['published']; |
|
|
const statusList = ['published']; |
|
|
console.log('loadGoods - 查询状态列表:', statusList); |
|
|
console.log('loadGoods - 查询状态列表:', statusList); |
|
|
|
|
|
|
|
|
|
|
|
// 取消正在进行的请求
|
|
|
|
|
|
if (this.data.currentRequestTask) { |
|
|
|
|
|
this.data.currentRequestTask.abort(); |
|
|
|
|
|
console.log('已取消旧的API请求'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 直接使用API.getProductList,并在请求完成后清除请求任务标记
|
|
|
API.getProductList(statusList, apiParams) |
|
|
API.getProductList(statusList, apiParams) |
|
|
.then(res => { |
|
|
.then(res => { |
|
|
wx.hideLoading(); |
|
|
wx.hideLoading(); |
|
|
@ -1656,7 +1701,7 @@ Page({ |
|
|
}) |
|
|
}) |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 选择地区(强制刷新机制)
|
|
|
// 选择地区(强制刷新机制),添加防抖
|
|
|
selectRegion: function (e) { |
|
|
selectRegion: function (e) { |
|
|
const region = e.currentTarget.dataset.region |
|
|
const region = e.currentTarget.dataset.region |
|
|
|
|
|
|
|
|
@ -1673,8 +1718,16 @@ Page({ |
|
|
app.globalData.showTabBar = true; |
|
|
app.globalData.showTabBar = true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 防抖处理:取消之前的定时器
|
|
|
|
|
|
if (this.data.filterDebounceTimer) { |
|
|
|
|
|
clearTimeout(this.data.filterDebounceTimer); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 设置新的定时器,延迟500ms执行刷新
|
|
|
|
|
|
this.data.filterDebounceTimer = setTimeout(() => { |
|
|
// 调用下拉刷新函数实现自动刷新
|
|
|
// 调用下拉刷新函数实现自动刷新
|
|
|
this.onRefresh(); |
|
|
this.onRefresh(); |
|
|
|
|
|
}, 500); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 阻止事件冒泡
|
|
|
// 阻止事件冒泡
|
|
|
@ -1682,7 +1735,7 @@ Page({ |
|
|
// 空函数,用于阻止事件冒泡
|
|
|
// 空函数,用于阻止事件冒泡
|
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 选择品种 - 使用下拉刷新机制
|
|
|
// 选择品种 - 使用下拉刷新机制,添加防抖
|
|
|
selectCategory: function (e) { |
|
|
selectCategory: function (e) { |
|
|
// 重新显示tabBar
|
|
|
// 重新显示tabBar
|
|
|
const app = getApp(); |
|
|
const app = getApp(); |
|
|
@ -1701,8 +1754,16 @@ Page({ |
|
|
searchKeyword: '', // 清除搜索关键词,筛选框优先级更高
|
|
|
searchKeyword: '', // 清除搜索关键词,筛选框优先级更高
|
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 防抖处理:取消之前的定时器
|
|
|
|
|
|
if (this.data.filterDebounceTimer) { |
|
|
|
|
|
clearTimeout(this.data.filterDebounceTimer); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 设置新的定时器,延迟500ms执行刷新
|
|
|
|
|
|
this.data.filterDebounceTimer = setTimeout(() => { |
|
|
// 调用下拉刷新函数实现自动刷新
|
|
|
// 调用下拉刷新函数实现自动刷新
|
|
|
this.onRefresh(); |
|
|
this.onRefresh(); |
|
|
|
|
|
}, 500); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 查看商品详情
|
|
|
// 查看商品详情
|
|
|
|