From eb201ae95aeb551937ce10250007bab8c51c51c2 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: Fri, 2 Jan 2026 15:09:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=A7=84=E6=A0=BC=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E5=8A=9F=E8=83=BD=EF=BC=9A=E5=A2=9E=E5=BC=BA=E6=A8=A1?= =?UTF-8?q?=E7=B3=8A=E6=90=9C=E7=B4=A2=E7=AE=97=E6=B3=95=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=90=9C=E7=B4=A2=E5=BB=BA=E8=AE=AE=E5=92=8C=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E4=BD=93=E9=AA=8C=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/seller/index.js | 116 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 112 insertions(+), 4 deletions(-) diff --git a/pages/seller/index.js b/pages/seller/index.js index 9b7ab41..5d1472c 100644 --- a/pages/seller/index.js +++ b/pages/seller/index.js @@ -4391,16 +4391,98 @@ Page({ }); }, - // 弹窗中规格搜索输入 + // 弹窗中规格搜索输入 - 增强版 onModalSpecSearchInput: function (e) { - const keyword = e.detail.value; + const keyword = e.detail.value.trim(); // 添加trim()处理空格 + console.log('规格搜索输入:', keyword); + const specOptions = this.data.specOptions; + console.log('可用规格选项:', specOptions.length, '个'); + let filteredOptions = specOptions; if (keyword) { + // 增强的模糊搜索算法 filteredOptions = specOptions.filter(option => { - return option.toLowerCase().includes(keyword.toLowerCase()); + const optionLower = option.toLowerCase(); + const keywordLower = keyword.toLowerCase(); + + // 多种匹配策略: + // 1. 精确匹配 + if (optionLower === keywordLower) return true; + + // 2. 包含匹配 + if (optionLower.includes(keywordLower)) return true; + + // 3. 分词匹配(支持空格分隔的关键词) + const keywordWords = keywordLower.split(' ').filter(word => word.length > 0); + if (keywordWords.length > 1) { + return keywordWords.every(word => optionLower.includes(word)); + } + + // 4. 数字范围匹配(例如搜索"30-35"能找到"净重30-35") + if (keywordLower.includes('-') || keywordLower.includes('+')) { + const keywordParts = keywordLower.split(/[-+]/); + if (keywordParts.length >= 2) { + return keywordParts.every(part => { + return part.trim() && optionLower.includes(part.trim()); + }); + } + } + + return false; }); + + // 搜索结果排序:优先显示包含完整关键词的结果 + filteredOptions.sort((a, b) => { + const aLower = a.toLowerCase(); + const bLower = b.toLowerCase(); + + // 完整包含关键词的排在前面 + const aFullMatch = aLower.includes(keywordLower); + const bFullMatch = bLower.includes(keywordLower); + + if (aFullMatch && !bFullMatch) return -1; + if (!aFullMatch && bFullMatch) return 1; + + // 如果都包含或都不包含,按字母顺序排序 + return a.localeCompare(b); + }); + + console.log('搜索结果:', filteredOptions.length, '个匹配项'); + console.log('搜索结果详情:', filteredOptions); + + // 如果没有匹配结果,显示友好提示 + if (filteredOptions.length === 0) { + wx.showToast({ + title: `未找到包含"${keyword}"的规格`, + icon: 'none', + duration: 2000 + }); + + // 提供搜索建议 + const suggestions = this.getSearchSuggestions(keyword); + if (suggestions.length > 0) { + setTimeout(() => { + wx.showModal({ + title: '搜索建议', + content: `是否要搜索这些相关规格?\n${suggestions.join('\n')}`, + success: (res) => { + if (res.confirm) { + const firstSuggestion = suggestions[0]; + this.setData({ + modalSpecSearchKeyword: firstSuggestion + }); + // 递归调用搜索建议的关键词 + this.onModalSpecSearchInput({ detail: { value: firstSuggestion } }); + } + } + }); + }, 1000); + } + } + } else { + console.log('搜索词为空,显示所有规格'); } this.setData({ @@ -4410,13 +4492,39 @@ Page({ }); }, - // 清除弹窗中的规格搜索关键词 + // 获取搜索建议 + getSearchSuggestions(keyword) { + const specOptions = this.data.specOptions; + const keywordLower = keyword.toLowerCase(); + + // 查找包含关键词中部分内容的规格 + const partialMatches = specOptions.filter(option => { + const optionLower = option.toLowerCase(); + return optionLower.includes(keywordLower) && optionLower !== keywordLower; + }); + + // 返回前3个建议 + return partialMatches.slice(0, 3); + }, + + // 清除弹窗中的规格搜索关键词 - 增强版 clearModalSpecSearch: function () { + console.log('清除规格搜索关键词'); + this.setData({ modalSpecSearchKeyword: '', filteredModalSpecOptions: this.data.specOptions, selectedModalSpecIndex: -1 }); + + console.log('搜索已清除,显示所有规格选项:', this.data.specOptions.length, '个'); + + // 提供友好的反馈 + wx.showToast({ + title: '已清除搜索条件', + icon: 'success', + duration: 1000 + }); }, // 双击检测变量