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