diff --git a/pages/buyer/index.js b/pages/buyer/index.js index 322f26e..852f4d7 100644 --- a/pages/buyer/index.js +++ b/pages/buyer/index.js @@ -1070,8 +1070,6 @@ Page({ searchKeyword: searchKeyword }) - const { goods } = this.data - // 如果搜索词为空,重置分页并重新加载所有数据 if (!searchKeyword.trim()) { console.log('搜索词为空,重置分页状态并重新加载数据'); @@ -1091,22 +1089,13 @@ Page({ return } - // ✅ 修改:实时过滤当前已加载的商品(前端搜索) - // 注意:这是在前端对已加载数据进行搜索,如果要搜索全部数据需要后端支持 - const filtered = goods.filter(item => - (item.name && item.name.toLowerCase().includes(searchKeyword.toLowerCase())) || - (item.spec && item.spec.toLowerCase().includes(searchKeyword.toLowerCase())) || - (item.yolk && item.yolk.toLowerCase().includes(searchKeyword.toLowerCase())) - ) - - this.setData({ - filteredGoods: filtered - }) + // 实时搜索时不进行前端过滤,仅在用户点击搜索按钮时才向服务器请求完整结果 + // 这样可以确保搜索结果的完整性和准确性 }, // 搜索商品 searchGoods() { - const { searchKeyword, goods, totalGoods } = this.data + const { searchKeyword } = this.data // 如果搜索词为空,重置分页并重新加载 if (!searchKeyword.trim()) { @@ -1114,7 +1103,8 @@ Page({ this.setData({ page: 1, hasMoreData: true, - selectedCategory: '全部' + selectedCategory: '全部', + searchKeyword: '' }, () => { this.loadGoods().then(() => { console.log('搜索重置后数据加载完成'); @@ -1126,25 +1116,30 @@ Page({ return } - // ✅ 添加:搜索范围提示 - if (goods.length < totalGoods) { - wx.showToast({ - title: `正在${goods.length}个已加载商品中搜索`, - icon: 'none', - duration: 1500 - }); - } - - // ✅ 修改:在当前已加载商品中搜索(前端搜索) - const filtered = goods.filter(item => - (item.name && item.name.toLowerCase().includes(searchKeyword.toLowerCase())) || - (item.spec && item.spec.toLowerCase().includes(searchKeyword.toLowerCase())) || - (item.yolk && item.yolk.toLowerCase().includes(searchKeyword.toLowerCase())) - ) + // 显示正在搜索的提示 + wx.showLoading({ + title: '正在搜索中...', + mask: true + }); + // ✅ 修改:向服务器发送搜索请求,获取完整的搜索结果 + // 重置分页状态,获取第一页的搜索结果 this.setData({ - filteredGoods: filtered - }) + page: 1, + hasMoreData: true + }, () => { + this.loadGoods().then(() => { + console.log('搜索结果加载完成'); + wx.hideLoading(); + }).catch(err => { + console.error('搜索结果加载失败:', err); + wx.hideLoading(); + wx.showToast({ + title: '搜索失败,请重试', + icon: 'none' + }); + }); + }); },