From 1a3987afe09e8d5ce269cafead64234fe85b91c9 Mon Sep 17 00:00:00 2001 From: zdl <3489966805@qq.com> Date: Tue, 4 Nov 2025 17:53:42 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E9=87=8D=E8=A6=81=E6=80=A7=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=9A=E9=80=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Community/components/UnifiedSearchBox.js | 85 ++++++++++++------- 1 file changed, 56 insertions(+), 29 deletions(-) diff --git a/src/views/Community/components/UnifiedSearchBox.js b/src/views/Community/components/UnifiedSearchBox.js index 7159194d..5f0e1c92 100644 --- a/src/views/Community/components/UnifiedSearchBox.js +++ b/src/views/Community/components/UnifiedSearchBox.js @@ -34,7 +34,7 @@ const UnifiedSearchBox = ({ // 筛选条件状态 const [sort, setSort] = useState('new'); // 排序方式 - const [importance, setImportance] = useState('all'); // 重要性 + const [importance, setImportance] = useState([]); // 重要性(数组,支持多选) const [dateRange, setDateRange] = useState(null); // 日期范围 // ✅ 本地输入状态 - 管理用户的实时输入 @@ -129,9 +129,22 @@ const UnifiedSearchBox = ({ useEffect(() => { if (!filters) return; - // 初始化排序和重要性 + // 初始化排序 if (filters.sort) setSort(filters.sort); - if (filters.importance) setImportance(filters.importance); + + // 初始化重要性(字符串解析为数组) + if (filters.importance) { + const importanceArray = filters.importance === 'all' + ? [] // 'all' 对应空数组(不显示任何选中) + : filters.importance.split(',').map(v => v.trim()).filter(Boolean); + setImportance(importanceArray); + logger.debug('UnifiedSearchBox', '初始化重要性', { + filters_importance: filters.importance, + importanceArray + }); + } else { + setImportance([]); + } // ✅ 初始化日期范围 if (filters.date_range) { @@ -247,30 +260,28 @@ const UnifiedSearchBox = ({ } }; - // ✅ 重要性变化(使用防抖) + // ✅ 重要性变化(立即执行)- 支持多选 const handleImportanceChange = (value) => { - logger.debug('UnifiedSearchBox', '【1/5】重要性值改变', { + logger.debug('UnifiedSearchBox', '重要性值改变', { oldValue: importance, newValue: value }); + setImportance(value); - // ⚠️ 注意:setState是异步的,此时importance仍是旧值 - logger.debug('UnifiedSearchBox', '【2/5】调用buildFilterParams前的状态', { - importance: importance, // 旧值 - sort: sort, - dateRange: dateRange, - industryValue: industryValue - }); - - // 使用防抖搜索 - const params = buildFilterParams({ importance: value }); - logger.debug('UnifiedSearchBox', '【3/5】buildFilterParams返回的参数', params); - + // 取消之前的防抖搜索 if (debouncedSearchRef.current) { - logger.debug('UnifiedSearchBox', '【4/5】调用防抖函数(300ms延迟)'); - debouncedSearchRef.current(params); + debouncedSearchRef.current.cancel(); } + + // 转换为逗号分隔字符串传给后端(空数组表示"全部") + const importanceStr = value.length === 0 ? 'all' : value.join(','); + + // 立即触发搜索 + const params = buildFilterParams({ importance: importanceStr }); + logger.debug('UnifiedSearchBox', '重要性改变,立即触发搜索', params); + + triggerSearch(params); }; // ✅ 排序变化(使用防抖) @@ -401,10 +412,18 @@ const UnifiedSearchBox = ({ returnType = 'week'; } + // 处理重要性参数:数组转换为逗号分隔字符串 + let importanceValue = overrides.importance ?? importance; + if (Array.isArray(importanceValue)) { + importanceValue = importanceValue.length === 0 + ? 'all' + : importanceValue.join(','); + } + const result = { // 基础参数(overrides 优先级高于本地状态) sort: actualSort, - importance: overrides.importance ?? importance, + importance: importanceValue, date_range: dateRange ? `${dateRange[0].format('YYYY-MM-DD')} 至 ${dateRange[1].format('YYYY-MM-DD')}` : '', page: 1, @@ -445,7 +464,7 @@ const UnifiedSearchBox = ({ setStockOptions([]); setIndustryValue([]); setSort('new'); - setImportance('all'); + setImportance([]); // 改为空数组 setDateRange(null); // 输出重置后的完整参数 @@ -453,7 +472,7 @@ const UnifiedSearchBox = ({ q: '', industry_code: '', sort: 'new', - importance: 'all', + importance: 'all', // 传给后端时转为'all' date_range: '', page: 1 }; @@ -483,9 +502,10 @@ const UnifiedSearchBox = ({ tags.push({ key: 'date_range', label: `日期: ${dateLabel}` }); } - // 重要性标签(排除默认值 'all') - if (importance && importance !== 'all') { - tags.push({ key: 'importance', label: `重要性: ${importance}级` }); + // 重要性标签(多选合并显示为单个标签) + if (importance && importance.length > 0) { + const importanceLabel = importance.map(imp => `${imp}级`).join(', '); + tags.push({ key: 'importance', label: `重要性: ${importanceLabel}` }); } // 排序标签(排除默认值 'new') @@ -506,6 +526,11 @@ const UnifiedSearchBox = ({ const handleRemoveTag = (key) => { logger.debug('UnifiedSearchBox', '移除标签', { key }); + // 取消所有待执行的防抖搜索(避免旧的防抖覆盖删除操作) + if (debouncedSearchRef.current) { + debouncedSearchRef.current.cancel(); + } + if (key === 'search') { // 清除搜索关键词和输入框,立即触发搜索 setInputValue(''); // 清空输入框 @@ -523,8 +548,8 @@ const UnifiedSearchBox = ({ const params = buildFilterParams({ date_range: '' }); triggerSearch(params); } else if (key === 'importance') { - // 重置重要性为默认值 - setImportance('all'); + // 重置重要性为空数组(传给后端为'all') + setImportance([]); const params = buildFilterParams({ importance: 'all' }); triggerSearch(params); } else if (key === 'sort') { @@ -624,12 +649,14 @@ const UnifiedSearchBox = ({ 重要性: -