Recommend.jsx 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import Taro from '@tarojs/taro'
  2. import { React, useState, useEffect, useRef, useMemo } from 'react'
  3. import iconsearch from '@/assets/icons/housemantj/search.png'
  4. import locationimg from '@/assets/icons/housemantj/location.png'
  5. import Tip from '@/components/tip'
  6. import List from '@/components/List';
  7. import MasonryLayout from '@/components/MasonryLayout';
  8. import { getIndexType, getResourceList } from '@/services/home'
  9. import { random } from '@/utils'
  10. import Card from '../components/Card'
  11. import './less/Recommend.less'
  12. import { OfficialAccount } from '@tarojs/components'
  13. const listStyle = { height: 'calc(100% - 145px)' }
  14. export default (props) => {
  15. const { router, person, location } = props
  16. const listClass = useMemo(() => random('f'), [])
  17. const [activeTab, setActiveTab] = useState(0)
  18. const [typeList, setTypeList] = useState([])
  19. const listRef = useRef()
  20. const [queryParams, setQueryParams] = useState({ location, pageNum: 1, pageSize: 10, typeId: '' })
  21. const rfTimes = useRef(0)
  22. // 获取资源表信息
  23. // const [alllist, setAllList] = useState([])
  24. const [listData, setListData] = useState({ list: [], rfTimes: 0 })
  25. //分类标签
  26. const tabs = [{ title: '附近' }].concat(typeList.map(x => ({ ...x, title: x.typeName })))
  27. const details=(index,tab)=>{
  28. setActiveTab(index)
  29. setQueryParams({
  30. ...queryParams,
  31. typeId: tab
  32. })
  33. }
  34. //切换上面的标签
  35. const handleTabChange = (e) => {
  36. const { index } = e.detail
  37. details(index,tabs[index].typeId||'')
  38. }
  39. const handleDataChange = (value, e) => {
  40. if (e.paramsChanged) {
  41. rfTimes.current += 1
  42. //如果context有的话代表他滚动了 那么切换tab页就置顶
  43. if (listRef.current?.context) {
  44. listRef.current.context.scrollTo({ top: 0 })
  45. }
  46. }
  47. setListData({ list: value, rfTimes: rfTimes.current })
  48. }
  49. useEffect(() => {
  50. //查询分类标签表
  51. getIndexType({ pageSize: 20 }).then((res) => {
  52. setTypeList(res.records || [])
  53. details(1,res.records[0].typeId)
  54. })
  55. }, [])
  56. const onSearch = () => {
  57. // 用绝对路径
  58. Taro.navigateTo({ url: '/pages/search/search' });
  59. }
  60. // useDidShow(() => {
  61. // setQueryParams({ ...queryParams })
  62. // })
  63. return (
  64. <>
  65. <OfficialAccount>s21</OfficialAccount>
  66. <view className='search' onClick={onSearch} >
  67. <input className='searchInput' disabled />
  68. <image className='searchicon' src={iconsearch} />
  69. <view className='searchword'>搜索</view>
  70. </view>
  71. <Tip />
  72. <view className='index-tabs'>
  73. <view className='position'>
  74. <image className='location' src={locationimg} />
  75. <view className='city'>南京</view>
  76. <view className='line' />
  77. </view>
  78. <scroll-view scrollX>
  79. <mp-tabs
  80. tabClass='tabs-Unselected'
  81. swiperClass='tabs-swiper'
  82. activeClass='tabs-Selected'
  83. tabs={tabs}
  84. current={activeTab}
  85. onChange={handleTabChange}
  86. activeTab={activeTab}
  87. >
  88. </mp-tabs>
  89. </scroll-view>
  90. </view>
  91. <List
  92. ref={listRef}
  93. style={listStyle}
  94. request={getResourceList}
  95. params={queryParams}
  96. refresherEnabled={false}
  97. onDataChange={handleDataChange}
  98. >
  99. <view style={{ padding: '30rpx 15rpx' }}>
  100. <MasonryLayout
  101. itemKey='resourceNo'
  102. listData={listData}
  103. render={(item, callback) => <Card item={item} onImageLoad={callback} />}
  104. />
  105. </view>
  106. </List>
  107. </>
  108. )
  109. }