index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. console.log("appInfoStore", appInfoStore.appInfo)
  22. const { getBookPageList } = useApi(appInfoStore)
  23. const [pageShow, setPageShow] = 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}>
  81. <View className='index'>
  82. {bookStore?.classifyData?.length > 0 && <BookTypeTabs typeValue={bookStore?.classifyData} typeTabIndex={classifyStore.categoryId} workDirection={bookStore.workDirection}></BookTypeTabs>}
  83. <View className='w row for_top1 pd_btm'>
  84. {
  85. bookStore?.bookList?.records?.length > 0 ? bookStore?.bookList?.records?.map((item, index) => {
  86. return <>
  87. <View key={index} ><BookboxRowBig {...item} /></View>
  88. </>
  89. }) :
  90. <Empty />
  91. }
  92. </View>
  93. </View>
  94. </ScrollViewHoc>
  95. );
  96. });
  97. export default TopNavBar(inject('store')(observer(Index)), {
  98. tab: true,
  99. tabVal: [{ value: "男生", text: "男生" }, { value: "女生", text: "女生" }],
  100. backgroundColor: '#fff',
  101. color: "#333",
  102. search: false,
  103. });