|
|
|
@ -1,39 +1,9 @@ |
|
|
|
|
import { Input, View, Image, Text, ScrollView } from '@tarojs/components' |
|
|
|
|
import Taro, { useLoad, useShareAppMessage, useShareTimeline } from '@tarojs/taro' |
|
|
|
|
import React, { useState } from 'react' |
|
|
|
|
import searchIcon from '../../assets/images/searchIcon.png' |
|
|
|
|
import errorIcon from '../../assets/images/error.png' |
|
|
|
|
import React, { useState, useEffect } from 'react' |
|
|
|
|
import empty from '../../assets/images/empty.svg' |
|
|
|
|
import './index.less' |
|
|
|
|
|
|
|
|
|
// 自定义防抖 Hook
|
|
|
|
|
function useDebounce<T>(callback: (...args: any[]) => void, delay: number): (...args: any[]) => void { |
|
|
|
|
let timeoutId: ReturnType<typeof setTimeout> | null = null; |
|
|
|
|
return (...args: any[]): void => { |
|
|
|
|
//@ts-ignore
|
|
|
|
|
clearTimeout(timeoutId); |
|
|
|
|
timeoutId = setTimeout(() => callback(...args), delay); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 自定义节流 Hook
|
|
|
|
|
function useThrottle<T>(callback: (...args: any[]) => void, delay: number): (...args: any[]) => void { |
|
|
|
|
let lastCallTime: number | null = null; |
|
|
|
|
let timeoutId: ReturnType<typeof setTimeout> | null = null; |
|
|
|
|
return (...args: any[]): void => { |
|
|
|
|
const now = Date.now(); |
|
|
|
|
if (lastCallTime === null || now - lastCallTime >= delay) { |
|
|
|
|
lastCallTime = now; |
|
|
|
|
callback(...args); |
|
|
|
|
} else if (!timeoutId) { |
|
|
|
|
timeoutId = setTimeout(() => { |
|
|
|
|
lastCallTime = now; |
|
|
|
|
callback(...args); |
|
|
|
|
timeoutId = null; |
|
|
|
|
}, delay - (now - (lastCallTime as number))); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export default function SearchDetail() { |
|
|
|
|
|
|
|
|
|
useShareAppMessage((res) => { |
|
|
|
@ -62,6 +32,7 @@ export default function SearchDetail() { |
|
|
|
|
//管理登录弹窗
|
|
|
|
|
const [isGetUsePhone, setisGetUsePhone] = useState(false) |
|
|
|
|
|
|
|
|
|
const [timer, setTimer] = useState<any>(null); |
|
|
|
|
const [searchText, setSearchText] = useState<string>(''); |
|
|
|
|
|
|
|
|
|
const [searchSingleResults, setSearchSingleResults] = useState<any[]>() |
|
|
|
@ -70,28 +41,22 @@ export default function SearchDetail() { |
|
|
|
|
const [userSelect, setuserSelect] = useState(0) |
|
|
|
|
|
|
|
|
|
// 搜索逻辑
|
|
|
|
|
const handleSearch = (value: string) => { |
|
|
|
|
const handleSearch = () => { |
|
|
|
|
//搜索单本结果
|
|
|
|
|
const searchSingle = userSubscribeSingleList.filter(( item:any ) => item.PrdtTitle.includes(value)) |
|
|
|
|
const searchSingle = userSubscribeSingleList.filter(( item:any ) => item.PrdtTitle.includes(searchText)) |
|
|
|
|
console.log('searchSingle=>', searchSingle) |
|
|
|
|
setSearchSingleResults(searchSingle) |
|
|
|
|
|
|
|
|
|
//搜索套装结果
|
|
|
|
|
const searchCollection = userSubscribeCollectionList.filter(( item:any ) => item.PrdtTitle.includes(value)) |
|
|
|
|
const searchCollection = userSubscribeCollectionList.filter(( item:any ) => item.PrdtTitle.includes(searchText)) |
|
|
|
|
console.log('searchCollection=>', searchCollection) |
|
|
|
|
setsearchCollectionResults(searchCollection) |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// 使用防抖和节流 Hooks
|
|
|
|
|
const debouncedHandleSearch = useDebounce(handleSearch, 300); |
|
|
|
|
const throttledHandleSearch = useThrottle(handleSearch, 500); |
|
|
|
|
|
|
|
|
|
// 监听输入框的变化
|
|
|
|
|
const handleInputChange = (event: any) => { |
|
|
|
|
setSearchText(event.detail.value); |
|
|
|
|
debouncedHandleSearch(event.detail.value); |
|
|
|
|
throttledHandleSearch(event.detail.value); |
|
|
|
|
}; |
|
|
|
|
useEffect(() => { |
|
|
|
|
clearTimeout(timer) |
|
|
|
|
setTimer(setTimeout(handleSearch, 300)) |
|
|
|
|
}, [searchText]) |
|
|
|
|
|
|
|
|
|
//跳转详情页
|
|
|
|
|
const handleToCollectionDetail = (id: string) => { |
|
|
|
@ -108,7 +73,6 @@ export default function SearchDetail() { |
|
|
|
|
|
|
|
|
|
//点击取消按钮清楚搜索栏
|
|
|
|
|
const handleClearResearch = () => { |
|
|
|
|
console.log('点击取消按钮清楚搜索栏') |
|
|
|
|
setSearchText('') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -182,15 +146,15 @@ export default function SearchDetail() { |
|
|
|
|
return ( |
|
|
|
|
<View className='search_bg'> |
|
|
|
|
<View className='subscribe_header'> |
|
|
|
|
<View
|
|
|
|
|
<View |
|
|
|
|
className='subscribe_header_searchContainer' |
|
|
|
|
> |
|
|
|
|
</View> |
|
|
|
|
<Input
|
|
|
|
|
className='subscribe_header_input'
|
|
|
|
|
<Input |
|
|
|
|
className='subscribe_header_input' |
|
|
|
|
placeholder='请输入搜索内容' |
|
|
|
|
value={searchText} |
|
|
|
|
onInput={handleInputChange}
|
|
|
|
|
onInput={(e) => setSearchText(e.detail.value)} |
|
|
|
|
></Input> |
|
|
|
|
<View |
|
|
|
|
className='subscribe_header_dressing' |
|
|
|
@ -209,17 +173,17 @@ export default function SearchDetail() { |
|
|
|
|
</View> |
|
|
|
|
|
|
|
|
|
<View className='subscribe_selection'> |
|
|
|
|
<View
|
|
|
|
|
<View |
|
|
|
|
className='subscribe_selection_item' |
|
|
|
|
style={userSelect === 0 ? {color: '#3ba366', fontSize: '1rem', height: '1rem', lineHeight: '1rem'} : {color: 'rgba(0, 0, 0, .3)', fontSize: '.875rem', height: '.875rem', lineHeight: '.875rem'}} |
|
|
|
|
onClick={()=>{setuserSelect(0)}} |
|
|
|
|
> |
|
|
|
|
<Text className='subscribe_selection_item_text'>单本</Text> |
|
|
|
|
</View> |
|
|
|
|
<View
|
|
|
|
|
<View |
|
|
|
|
className='subscribe_selection_item' |
|
|
|
|
style={userSelect === 1 ? {color: '#3ba366', fontSize: '1rem', height: '1rem', lineHeight: '1rem'} : {color: 'rgba(0, 0, 0, .3)', fontSize: '.875rem', height: '.875rem', lineHeight: '.875rem'}} |
|
|
|
|
onClick={()=>{setuserSelect(1)}}
|
|
|
|
|
onClick={()=>{setuserSelect(1)}} |
|
|
|
|
> |
|
|
|
|
<Text className='subscribe_selection_item_text'>套装</Text> |
|
|
|
|
</View> |
|
|
|
@ -227,74 +191,81 @@ export default function SearchDetail() { |
|
|
|
|
|
|
|
|
|
{/* 列表渲染 */} |
|
|
|
|
{ |
|
|
|
|
!isGetUsePhone &&
|
|
|
|
|
<ScrollView |
|
|
|
|
scrollY |
|
|
|
|
scrollWithAnimation |
|
|
|
|
className='subscribe_content' |
|
|
|
|
enableFlex |
|
|
|
|
> |
|
|
|
|
<View |
|
|
|
|
className='subscribe_container' |
|
|
|
|
style={userSelect === 0 ? {gridTemplateColumns: 'repeat(3, 1fr)'} : {gridTemplateColumns: 'repeat(2, 1fr)'}} |
|
|
|
|
!isGetUsePhone && searchSingleResults && searchSingleResults.length > 0 ? |
|
|
|
|
<ScrollView |
|
|
|
|
scrollY |
|
|
|
|
scrollWithAnimation |
|
|
|
|
className='subscribe_content' |
|
|
|
|
enableFlex |
|
|
|
|
> |
|
|
|
|
{ |
|
|
|
|
userSelect === 0 && searchSingleResults && searchSingleResults.length > 0 &&searchSingleResults.map((item: any, index: number) => ( |
|
|
|
|
<View
|
|
|
|
|
className='subscribe_content_singleItem' |
|
|
|
|
key={index} |
|
|
|
|
onClick={() => handleToSingleDetail(item.PrdtId)} |
|
|
|
|
> |
|
|
|
|
{ |
|
|
|
|
item.isFree === 1 && ( |
|
|
|
|
<View className='relative_container'> |
|
|
|
|
<View className='subscribe_content_singleItem_label'> |
|
|
|
|
<Text className='subscribe_content_singleItem_label_text'>免费绘本</Text> |
|
|
|
|
<View |
|
|
|
|
className='subscribe_container' |
|
|
|
|
style={userSelect === 0 ? {gridTemplateColumns: 'repeat(3, 1fr)'} : {gridTemplateColumns: 'repeat(2, 1fr)'}} |
|
|
|
|
> |
|
|
|
|
{ |
|
|
|
|
userSelect === 0 && searchSingleResults && searchSingleResults.length > 0 &&searchSingleResults.map((item: any, index: number) => ( |
|
|
|
|
<View |
|
|
|
|
className='subscribe_content_singleItem' |
|
|
|
|
key={index} |
|
|
|
|
onClick={() => handleToSingleDetail(item.PrdtId)} |
|
|
|
|
> |
|
|
|
|
{ |
|
|
|
|
item.isFree === 1 && ( |
|
|
|
|
<View className='relative_container'> |
|
|
|
|
<View className='subscribe_content_singleItem_label'> |
|
|
|
|
<Text className='subscribe_content_singleItem_label_text'>免费绘本</Text> |
|
|
|
|
</View> |
|
|
|
|
</View> |
|
|
|
|
</View> |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
<Image |
|
|
|
|
className='subscribe_content_singleItem_cover' |
|
|
|
|
src={item.PrdtCover} |
|
|
|
|
mode='aspectFill' |
|
|
|
|
lazyLoad |
|
|
|
|
></Image> |
|
|
|
|
<View className='subscribe_content_singleItem_title'>{item.PrdtTitle}</View> |
|
|
|
|
</View> |
|
|
|
|
)) |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
<Image |
|
|
|
|
className='subscribe_content_singleItem_cover' |
|
|
|
|
src={item.PrdtCover} |
|
|
|
|
mode='aspectFill' |
|
|
|
|
lazyLoad |
|
|
|
|
></Image> |
|
|
|
|
<View className='subscribe_content_singleItem_title'>{item.PrdtTitle}</View> |
|
|
|
|
</View> |
|
|
|
|
)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
userSelect === 1 && searchCollectionResults && searchCollectionResults.length > 0 && searchCollectionResults.map((item: any, index: number) => ( |
|
|
|
|
<View
|
|
|
|
|
className='subscribe_content_collectionItem' |
|
|
|
|
key={index} |
|
|
|
|
onClick={() => handleToCollectionDetail(item.PrdtId)} |
|
|
|
|
> |
|
|
|
|
{ |
|
|
|
|
item.isFree === 1 && ( |
|
|
|
|
<View className='relative_container'> |
|
|
|
|
<View className='subscribe_content_singleItem_label'> |
|
|
|
|
<Text className='subscribe_content_singleItem_label_text'>免费绘本</Text> |
|
|
|
|
{ |
|
|
|
|
userSelect === 1 && searchCollectionResults && searchCollectionResults.length > 0 && searchCollectionResults.map((item: any, index: number) => ( |
|
|
|
|
<View |
|
|
|
|
className='subscribe_content_collectionItem' |
|
|
|
|
key={index} |
|
|
|
|
onClick={() => handleToCollectionDetail(item.PrdtId)} |
|
|
|
|
> |
|
|
|
|
{ |
|
|
|
|
item.isFree === 1 && ( |
|
|
|
|
<View className='relative_container'> |
|
|
|
|
<View className='subscribe_content_singleItem_label'> |
|
|
|
|
<Text className='subscribe_content_singleItem_label_text'>免费绘本</Text> |
|
|
|
|
</View> |
|
|
|
|
</View> |
|
|
|
|
</View> |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
<Image |
|
|
|
|
className='subscribe_content_singleItem_cover' |
|
|
|
|
src={item.PrdtCover} |
|
|
|
|
mode='aspectFit' |
|
|
|
|
lazyLoad |
|
|
|
|
></Image> |
|
|
|
|
<View className='subscribe_content_singleItem_title'>{item.PrdtTitle}</View> |
|
|
|
|
<View className='subscribe_content_collectionItem_count'>{`共${item.ChildCount}本`}</View> |
|
|
|
|
</View> |
|
|
|
|
)) |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
<Image |
|
|
|
|
className='subscribe_content_singleItem_cover' |
|
|
|
|
src={item.PrdtCover} |
|
|
|
|
mode='aspectFit' |
|
|
|
|
lazyLoad |
|
|
|
|
></Image> |
|
|
|
|
<View className='subscribe_content_singleItem_title'>{item.PrdtTitle}</View> |
|
|
|
|
<View className='subscribe_content_collectionItem_count'>{`共${item.ChildCount}本`}</View> |
|
|
|
|
</View> |
|
|
|
|
)) |
|
|
|
|
} |
|
|
|
|
</View> |
|
|
|
|
</ScrollView> : |
|
|
|
|
<View className='none'> |
|
|
|
|
<Image |
|
|
|
|
className='icon' |
|
|
|
|
src={empty} |
|
|
|
|
></Image> |
|
|
|
|
<View>抱歉,暂无查询结果</View> |
|
|
|
|
</View> |
|
|
|
|
</ScrollView> |
|
|
|
|
} |
|
|
|
|
</View> |
|
|
|
|
) |
|
|
|
|