张延森 3 лет назад
Родитель
Сommit
db12df0126
3 измененных файлов: 54 добавлений и 42 удалений
  1. 27
    29
      src/components/List/index.jsx
  2. 4
    4
      src/pages/index/components/Card.js
  3. 23
    9
      src/pages/index/tabs/Recommend.jsx

+ 27
- 29
src/components/List/index.jsx Просмотреть файл

@@ -1,35 +1,29 @@
1
-import React, { useEffect, useMemo, useRef, useState } from 'react';
2
-import VirtualList from '@tarojs/components/virtual-list';
1
+import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2
+import { ScrollView } from '@tarojs/components';
3 3
 import Taro from '@tarojs/taro';
4 4
 
5 5
 export default (props) => {
6 6
   const {
7
-    className,
8
-    height,
9
-    width = '100%',
10
-    itemHeight = 100,
11 7
     render,
12 8
     request,
13 9
     params,
14 10
     pageSize = 10,
15
-    onError
11
+    onError,
12
+    onDataChange,
13
+    ...leftProps
16 14
   } = props
17 15
 
18 16
   const loadingRef = useRef(false)
19 17
   const [payload, setPayload] = useState({})
20 18
   const [list, setList] = useState([])
21
-  const pageRef = useRef({ current: 1, total: 0 })
22
-
23
-  const dataLen = list.length
19
+  const pageRef = useRef({ current: 1, pages: 0 })
24 20
 
25 21
   // 滚动
26
-  const handleScroll = ({ scrollDirection, scrollOffset }) => {
22
+  const handleScrollToLower = (e) => {
27 23
     const loading = loadingRef.current
28
-    const isUping = scrollDirection === 'forward'
29
-    const canScroll = scrollOffset > ((dataLen - 5) * itemHeight + 100)
30
-    const hasMore = pageRef.current.current < pageRef.current.total
24
+    const hasMore = pageRef.current.current < pageRef.current.pages
31 25
 
32
-    if (!loading && isUping && canScroll && hasMore) {
26
+    if (!loading && hasMore) {
33 27
       setPayload({
34 28
         ...payload,
35 29
         pageNum: pageRef.current.current + 1
@@ -41,19 +35,24 @@ export default (props) => {
41 35
   useEffect(() => {
42 36
     setPayload({
43 37
       ...params,
44
-      pageNum: pageRef.current,
38
+      pageNum: 1,
45 39
       pageSize,
46 40
     })
47 41
   }, [pageSize, params])
48 42
 
49
-
50 43
   // 请求数据
51 44
   useEffect(() => {
52 45
     Taro.showLoading()
53 46
     loadingRef.current = true
54 47
     request(payload).then((res) => {
55 48
       const { records, ...pageInfo } = res || {}
56
-      setList(records || [])
49
+
50
+      const lst = pageInfo.current === 1 ? records || [] : list.concat(records || [])
51
+      setList(lst)
52
+      if (onDataChange) {
53
+        onDataChange(lst)
54
+      }
55
+
57 56
       pageRef.current = pageInfo
58 57
       loadingRef.current = false
59 58
       Taro.hideLoading()
@@ -67,17 +66,16 @@ export default (props) => {
67 66
     })
68 67
   }, [payload])
69 68
 
70
-  const Row = useMemo(render, [])
71
-
72 69
   return (
73
-    <VirtualList
74
-      className={className}
75
-      width={width}  
76
-      height={height}
77
-      itemData={list}
78
-      itemCount={dataLen}
79
-      itemSize={itemHeight}
80
-      onScroll={handleScroll}
81
-    >{Row}</VirtualList>
70
+    <ScrollView
71
+      scrollY
72
+      onScrollToLower={handleScrollToLower}
73
+      {...leftProps}
74
+    >
75
+      { !render
76
+          ? props.children
77
+          : list.map((item, index) => render({ item, index }))
78
+      }
79
+    </ScrollView>
82 80
   )
83 81
 }

+ 4
- 4
src/pages/index/components/Card.js Просмотреть файл

@@ -10,20 +10,20 @@ import love from '@/assets/icons/housemantj/爱心.png'
10 10
 
11 11
 export default (props) => {
12 12
 
13
-  const { item } = props
13
+  const { item, style } = props
14 14
 
15 15
   const [isSaved, toggleSave] = useSave(item.isSaved, item.targetType, item.targetId)
16 16
   const Detail = () => {
17 17
     if (item.targetType == 'tourist') {
18
-      let location = item.lng + ',' + item.lat
19
-      Taro.navigateTo({ url: `/pages/details/mjDetails/sceneryDetails?id=${item.targetId}&distance=${(item.distance / 1000).toFixed(2)}&location=${location}` });
18
+      let loc = item.lng + ',' + item.lat
19
+      Taro.navigateTo({ url: `/pages/details/mjDetails/sceneryDetails?id=${item.targetId}&distance=${(item.distance / 1000).toFixed(2)}&location=${loc}` });
20 20
     }
21 21
     else {
22 22
       Taro.navigateTo({ url: `/pages/details/foodDetails/foodDetails?id=${item.targetId}` })
23 23
     }
24 24
   }
25 25
   return (
26
-    <view className='contentCard'>
26
+    <view className='contentCard' style={style}>
27 27
       <view className='cardTop'>
28 28
         <image mode='widthFix' onClick={Detail} src={item.poster} className='cCardimg' />
29 29
         <image className='lefttips' src={item.targetType === 'tourist' ? mjTip : msTip} />

+ 23
- 9
src/pages/index/tabs/Recommend.jsx Просмотреть файл

@@ -3,6 +3,7 @@ import { React, useState, useEffect } from 'react'
3 3
 import iconsearch from '@/assets/icons/housemantj/search.png'
4 4
 import locationimg from '@/assets/icons/housemantj/location.png'
5 5
 import Tip from '@/components/tip'
6
+import List from '@/components/List';
6 7
 import { getIndexType,  getResourceList } from '@/services/home'
7 8
 import Card from '../components/Card'
8 9
 import './less/Recommend.less'
@@ -34,18 +35,18 @@ export default (props) => {
34 35
     }
35 36
   }
36 37
 
37
-  useEffect(() => {    
38
+  useEffect(() => {
38 39
     //查询分类标签表
39 40
     getIndexType({ pageSize: 20 }).then((res) => {
40 41
       setTypeList(res.records || [])
41 42
     })    
42 43
   }, [])
43 44
 
44
-  useEffect(() => {
45
-      getResourceList(queryParams).then((res) => {
46
-        setAllList(res.records || [])
47
-      })
48
-    }, [queryParams])
45
+  // useEffect(() => {
46
+  //     getResourceList(queryParams).then((res) => {
47
+  //       setAllList(res.records || [])
48
+  //     })
49
+  //   }, [queryParams])
49 50
 
50 51
   const onSearch = () => {
51 52
     // 用绝对路径
@@ -53,7 +54,7 @@ export default (props) => {
53 54
   }
54 55
 
55 56
   return (
56
-    <view style={{ height: '100%', overflow: 'auto' }}>
57
+    <view style={{ height: '100%', overflow: 'hidden' }}>
57 58
       <view className='search' onClick={onSearch}>
58 59
         <input className='searchInput' disabled />
59 60
         <image className='searchicon' src={iconsearch} />
@@ -79,7 +80,20 @@ export default (props) => {
79 80
           </mp-tabs>
80 81
         </scroll-view>
81 82
       </view>
82
-      <scroll-view scroll-y='true' scroll-view='true'  	bindscrolltolower='lower' bindscrolltoupper='upper'	 bindscroll='scroll'
83
+
84
+      <List
85
+        style={{ height: 'calc(100% - 160px)' }}
86
+        request={getResourceList}
87
+        params={queryParams}
88
+        onDataChange={setAllList}
89
+      >
90
+        <view className='waterfall'>
91
+          {
92
+            alllist.map((item) => <Card key={item.resourceNo} item={item} />)
93
+          }
94
+        </view>
95
+      </List>
96
+      {/* <scroll-view scroll-y='true' scroll-view='true'  	bindscrolltolower='lower' bindscrolltoupper='upper'	 bindscroll='scroll'
83 97
       
84 98
       >
85 99
         <view className='waterfall'>
@@ -88,7 +102,7 @@ export default (props) => {
88 102
           }
89 103
         </view>
90 104
         <view className='botton'>已经到底了~</view>
91
-      </scroll-view>
105
+      </scroll-view> */}
92 106
     </view>
93 107
   )
94 108
 }