index.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { forwardRef, useEffect, useState } from 'react'
  2. import { useDidHide, useDidShow } from '@tarojs/taro'
  3. import { View } from '@tarojs/components'
  4. import { observer, inject } from 'mobx-react'
  5. import './index.less'
  6. //==========components组件引用========================
  7. import TopNavBar from '@src/components/TopNavBar/index'
  8. import ScrollViewHoc from '@src/components/ScrollView'
  9. import BookboxRowBig from '@src/components/PupPetry/BookBox/BookboxRowBig'
  10. import BookTypeTabs from '@src/components/PupPetry/BookTypeTabs'
  11. import Empty from '@src/components/Empty'
  12. import { Store } from '@src/app'
  13. import useApi from '@src/Hook/useApi'
  14. import { getBookCategoryList } from '@src/server/classify'
  15. import Taro from '@tarojs/taro'
  16. interface Props {
  17. store: Store;
  18. }
  19. const Index = forwardRef((props: Props) => {
  20. const { bookStore, appInfoStore, classifyStore } = props.store
  21. const { getBookPageList } = useApi(appInfoStore)
  22. const [pageShow, setPageShow] = useState(false)//防止小程序切换页面组件不会被真实卸载,阻止不必要的操作
  23. const [isFixed, setIsFixed] = useState(false)
  24. // 获取分类列表
  25. useEffect(() => {
  26. if (pageShow) {
  27. let { workDirection } = bookStore
  28. getBookCategoryList(workDirection)
  29. }
  30. }, [appInfoStore.token, bookStore.workDirection, pageShow]); // Dependency array, runs effect only once on mount and unmount
  31. //获取书籍列表
  32. useEffect(() => {
  33. if (pageShow) {
  34. let { workDirection } = bookStore
  35. let { categoryId } = classifyStore
  36. categoryId.id != 0 && classifyStore.categoryId && getList({ workDirection, categoryId: categoryId.id, pageNum: 1, pageSize: 10 })
  37. }
  38. }, [appInfoStore.token, bookStore.workDirection, classifyStore.categoryId, pageShow])
  39. // 使用 useDidShow 代替 onShow
  40. useDidShow(() => {
  41. setPageShow(true)
  42. });
  43. // 使用 useDidHide 如果需要在页面隐藏时做一些处理
  44. useDidHide(() => {
  45. setPageShow(false)
  46. });
  47. //上拉刷新
  48. const refresh = async () => {
  49. let { workDirection } = bookStore
  50. let { categoryId } = classifyStore
  51. getList({ workDirection, categoryId: categoryId.id, pageNum: 1, pageSize: 10 })
  52. }
  53. //下来加载
  54. const load = () => {
  55. return new Promise((resolve, reject) => {
  56. let { size, total, records } = bookStore.bookList
  57. let pageSize = size + 10
  58. let { workDirection } = bookStore
  59. let { categoryId } = classifyStore
  60. if (records?.length >= total) {
  61. Taro.showToast({
  62. title: '没有更多了~~',
  63. duration: 2000,
  64. icon: 'none'
  65. })
  66. reject()
  67. return
  68. } else {
  69. getList({ workDirection, categoryId: categoryId.id, pageNum: 1, pageSize }).then(res => {
  70. console.log("Res", res)
  71. resolve(true)
  72. })
  73. }
  74. })
  75. }
  76. const getList = async (params) => {
  77. return getBookPageList(params)
  78. }
  79. return (
  80. <ScrollViewHoc load={load} refresh={refresh} callback={(num) => {
  81. // if(num > 100 && !isFixed){
  82. // setIsFixed(true)
  83. // }
  84. // if(num <100 && isFixed){
  85. // setIsFixed(false)
  86. // }
  87. }}>
  88. <View className='index'>
  89. {bookStore?.classifyData?.length > 0 && <View style={isFixed ? { position: 'fixed', background: "#fff" } : {background: "#fff"}}>
  90. <BookTypeTabs typeValue={bookStore?.classifyData} typeTabIndex={classifyStore.categoryId} workDirection={bookStore.workDirection} ></BookTypeTabs>
  91. </View>}
  92. <View className='w row for_top1 pd_btm'>
  93. {
  94. bookStore?.bookList?.records?.length > 0 ? bookStore?.bookList?.records?.map((item, index) => {
  95. return <>
  96. <View key={item.bookId} ><BookboxRowBig {...item} /></View>
  97. </>
  98. }) :
  99. <Empty />
  100. }
  101. </View>
  102. </View>
  103. </ScrollViewHoc>
  104. );
  105. });
  106. export default TopNavBar(inject('store')(observer(Index)), {
  107. tab: true,
  108. tabVal: [{ value: "男生", text: "男生" }, { value: "女生", text: "女生" }],
  109. backgroundColor: '#fff',
  110. color: "#333",
  111. search: false,
  112. });