import React from 'react' import { View, Text, ScrollView } from '@tarojs/components' import Taro from '@tarojs/taro' import { useState, useEffect } from 'react' import { Input } from '@nutui/nutui-react-taro' import ProductCard from '@/components/common/ProductCard' import EmptyState from '@/components/common/EmptyState' import type { ShopGoods, ShopGoodsParam } from '@/api/shop/shopGoods/model' import { pageShopGoods } from '@/api/shop/shopGoods' import { useScrollHeight } from '@/hooks/useScrollHeight' definePageConfig({ navigationBarTitleText: '搜索', }) const SearchPage: React.FC = () => { const [keyword, setKeyword] = useState('') const [list, setList] = useState([]) const [loading, setLoading] = useState(false) const scrollHeight = useScrollHeight(44) const [history, setHistory] = useState([]) useEffect(() => { const saved = Taro.getStorageSync('search_history') if (saved) setHistory(JSON.parse(saved)) }, []) const doSearch = async (kw?: string) => { const q = kw || keyword if (!q.trim()) return setLoading(true) try { const params: ShopGoodsParam = { keywords: q, isShow: 1, page: 1, limit: 20 } const res = await pageShopGoods(params) setList(res?.list || []) // 保存搜索历史 const newHistory = [q, ...history.filter(h => h !== q)].slice(0, 10) setHistory(newHistory) Taro.setStorageSync('search_history', JSON.stringify(newHistory)) } catch { /* ignore */ } setLoading(false) } const clearHistory = () => { setHistory([]) Taro.removeStorageSync('search_history') } return ( {/* 搜索栏 */} setKeyword(val)} onConfirm={() => doSearch()} /> doSearch()}>搜索 {list.length > 0 ? ( {list.map(item => ( ))} ) : !loading && keyword === '' ? ( {history.length > 0 && ( 搜索历史 清空 {history.map((h, i) => ( { setKeyword(h); doSearch(h) }} > {h} ))} )} 热门搜索 {['羽毛球拍', '球鞋', '运动服', '手胶'].map((h) => ( { setKeyword(h); doSearch(h) }} > {h} ))} ) : ( )} ) } export default SearchPage