diff --git a/pages/index/index.js b/pages/index/index.js index 76de126..7a3e81b 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -408,10 +408,21 @@ Page({ hasMoreData: true, goods: existingGoods, filteredGoods: [], + // 清除缓存以确保获取最新数据 + categoryQueryCache: {}, + lastDataTimestamp: 0, }) const timestamp = new Date().getTime(); + // 强制刷新:清除所有缓存并重新从数据库加载 + this.setData({ + // 清除商品数据缓存 + goodsCache: [], + // 重置缓存时间戳 + lastDataTimestamp: 0 + }); + API.getProductList('published', { timestamp: timestamp, viewMode: 'shopping', @@ -565,15 +576,17 @@ Page({ const primaryStock = quantity || minOrder || stock || inventory || availableStock || totalAvailable; const totalStock = primaryStock; - // 智能库存显示 - 有数据时显示具体数字,没有数据时显示"充足" + // 智能库存显示 - 库存>=10000显示"库存充足",库存=0显示"暂无",其他显示具体数字 let displayStock; - if (totalStock > 0) { - displayStock = totalStock; - } else if (stock > 0 || inventory > 0 || availableStock > 0 || totalAvailable > 0) { - displayStock = stock || inventory || availableStock || totalAvailable; - } else { - // 如果有商品数据但没有库存信息,显示"充足" + if (totalStock >= 1000) { + // 库存>=10000时显示"库存充足" displayStock = '充足'; + } else if (totalStock === 0) { + // 库存=0时显示"暂无" + displayStock = '暂无'; + } else { + // 其他情况显示具体数字 + displayStock = totalStock; } console.log('库存计算详情:', { @@ -693,7 +706,9 @@ Page({ filteredGoods: filteredGoods, loadingMore: false, isLoading: false, - page: this.data.page + 1 + page: this.data.page + 1, + // 更新缓存时间戳 + lastDataTimestamp: new Date().getTime() }) }, @@ -752,12 +767,25 @@ Page({ const filteredGoods = this.applyFilters(updatedGoods, false) + const currentCategory = this.data.selectedCategory === '全部' ? '' : this.data.selectedCategory; + const currentKeyword = this.data.searchKeyword; + const cacheKey = `${currentCategory}_${currentKeyword}`; + this.setData({ goods: updatedGoods, filteredGoods: filteredGoods, loadingMore: false, isLoading: false, - page: this.data.page + 1 + page: this.data.page + 1, + // 更新缓存时间戳 + lastDataTimestamp: new Date().getTime() + }) + + // 更新分类查询缓存 + const newCategoryQueryCache = { ...this.data.categoryQueryCache }; + newCategoryQueryCache[cacheKey] = updatedGoods; + this.setData({ + categoryQueryCache: newCategoryQueryCache }) }, @@ -970,15 +998,17 @@ Page({ const primaryStock = quantity || minOrder || stock || inventory || availableStock || totalAvailable; const totalStock = primaryStock; - // 智能库存显示 - 有数据时显示具体数字,没有数据时显示"充足" + // 智能库存显示 - 库存>=10000显示"库存充足",库存=0显示"暂无",其他显示具体数字 let displayStock; - if (totalStock > 0) { - displayStock = totalStock; - } else if (stock > 0 || inventory > 0 || availableStock > 0 || totalAvailable > 0) { - displayStock = stock || inventory || availableStock || totalAvailable; + if (totalStock >= 10000) { + // 库存>=10000时显示"库存充足" + displayStock = '库存充足'; + } else if (totalStock === 0) { + // 库存=0时显示"暂无" + displayStock = '暂无'; } else { - // 如果有商品数据但没有库存信息,显示"充足" - displayStock = '充足'; + // 其他情况显示具体数字 + displayStock = totalStock; } const processedProduct = { @@ -1030,16 +1060,21 @@ Page({ }); }, - // 刷新商品列表 + // 刷新商品列表 - 强制刷新机制 refreshGoodsList: function() { this.setData({ page: 1, hasMoreData: true, goods: [], filteredGoods: [], - loadingMore: false + loadingMore: false, + // 清除所有缓存以获取最新数据 + categoryQueryCache: {}, + lastDataTimestamp: 0, + goodsCache: [] }, () => { - this.loadGoods() + console.log('refreshGoodsList:清除缓存并重新加载数据'); + this.loadGoods(false, true); // 第二个参数true表示强制刷新 }) }, @@ -1321,7 +1356,7 @@ Page({ }) }, - // 选择地区(实时更新) + // 选择地区(强制刷新机制) selectRegion: function(e) { const region = e.currentTarget.dataset.region @@ -1338,19 +1373,21 @@ Page({ app.globalData.showTabBar = true; } - // 如果从局部地区切换到全国地区,重新加载所有商品 - if (region === '全国' && this.data.selectedCategory === '全部' && !this.data.searchKeyword) { - // 重新加载商品数据 - this.refreshGoodsList(); - } else { - // 否则仅对本地商品进行筛选 - const filteredGoods = this.applyFilters(this.data.goods, false) - const groupedGoods = this.groupGoodsForStaggeredLayout(filteredGoods) - this.setData({ - filteredGoods: filteredGoods, - groupedGoods: groupedGoods, - }) - } + // 清除缓存并重新加载数据 - 仿照下拉刷新机制 + this.setData({ + page: 1, + hasMoreData: true, + goods: [], + filteredGoods: [], + loadingMore: false, + // 清除所有缓存以获取最新数据 + categoryQueryCache: {}, + lastDataTimestamp: 0, + goodsCache: [] + }); + + console.log('地区选择:清除缓存并重新加载数据,地区:', region); + this.loadGoods(false, true); // 强制刷新 }, // 阻止事件冒泡 @@ -1358,7 +1395,7 @@ Page({ // 空函数,用于阻止事件冒泡 }, - // 选择品种 + // 选择品种 - 使用下拉刷新机制 selectCategory: function(e) { // 重新显示tabBar const app = getApp(); @@ -1371,55 +1408,25 @@ Page({ return // 如果选择的分类和当前相同,不重复加载 } - // 如果有现有的商品数据,先基于现有数据进行筛选,而不是清空 - const originalGoods = this.data.goods; - const hasOriginalGoods = originalGoods && originalGoods.length > 0; - + // 清除缓存并重新加载数据 - 仿照下拉刷新机制 this.setData({ selectedCategory: category, searchKeyword: '', // 清除搜索关键词,筛选框优先级更高 - loadingMore: false + loadingMore: false, + page: 1, + hasMoreData: true, + goods: [], + filteredGoods: [], + isLoading: true, + // 清除所有缓存以获取最新数据 + categoryQueryCache: {}, + lastDataTimestamp: 0, + goodsCache: [] }); - if (hasOriginalGoods) { - // 使用现有商品数据进行筛选 - console.log('使用现有商品数据进行分类筛选,原始商品数量:', originalGoods.length); - const filteredGoods = this.applyFilters(originalGoods, false); - const groupedGoods = this.groupGoodsForStaggeredLayout(filteredGoods); - - this.setData({ - filteredGoods: filteredGoods, - groupedGoods: groupedGoods, - isLoading: false - }); - } else { - // 如果没有现有数据,清空并重新加载 - this.setData({ - page: 1, - hasMoreData: true, - goods: [], - filteredGoods: [], - isLoading: true - }); - - const currentCategory = category === '全部' ? '' : category; - const currentKeyword = this.data.searchKeyword; - const cacheKey = `${currentCategory}_${currentKeyword}`; - const now = new Date().getTime(); - - // 检查缓存(首次加载时使用缓存,加载更多时不用) - if (this.data.categoryQueryCache[cacheKey] && - (now - this.data.lastDataTimestamp) < this.data.cacheValidDuration) { - console.log('selectCategory使用缓存数据,cacheKey:', cacheKey); - const cachedGoods = this.data.categoryQueryCache[cacheKey]; - // 更新timestamp以确保缓存有效 - this.setData({ lastDataTimestamp: now }); - this.processCachedGoods(cachedGoods, false); - } else { - // 重新从服务器加载数据 - this.loadGoods(false); - } - } + // 强制刷新:从数据库重新加载数据 + console.log('筛选操作:清除缓存并重新加载数据,分类:', category); + this.loadGoods(false, true); // 第二个参数true表示强制刷新 }, // 查看商品详情 diff --git a/pages/index/index.wxml b/pages/index/index.wxml index 6caf8eb..a1a5904 100644 --- a/pages/index/index.wxml +++ b/pages/index/index.wxml @@ -136,7 +136,7 @@ > - + {{item.name}} {{item.displaySpecification}} | {{item.displayYolk}} - - {{item.sourceType || ''}} + + {{item.sourceType || ''}} {{item.negotiateStatus}} - 库存:{{item.totalStock && item.totalStock !== '充足' ? item.totalStock + '件' : (item.totalStock || '充足')}} + 库存:{{item.totalStock && item.totalStock !== '充足' ? item.totalStock + '件' : (item.totalStock || '充足')}} 已有{{item.reservedCount || 0}}人收藏