From dc2de7f7eab06efbd9585cbc954429e5009fa937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Thu, 4 Dec 2025 09:41:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=90=9C=E7=B4=A2=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E5=8F=AA=E6=A3=80=E6=B5=8B=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93product=E8=A1=A8=E7=9A=84productName=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/buyer/index.js | 54 ++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 27 deletions(-) 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 + }) },