index.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { forwardRef, useEffect, useRef, useState } from 'react'
  2. import Taro, { useShareAppMessage } from '@tarojs/taro'
  3. import { useDidHide, useDidShow } from '@tarojs/taro'
  4. import { observer, inject } from 'mobx-react'
  5. import { View } from '@tarojs/components'
  6. import './index.less'
  7. //==========components组件引用========================
  8. import TopNavBar from '@src/components/TopNavBar/index'
  9. import ScrollViewHoc from '@src/components/ScrollView'
  10. import BookboxRowBig from '@src/components/PupPetry/BookBox/BookboxRowBig'
  11. import BookTypeTabs from '@src/components/PupPetry/BookTypeTabs'
  12. import Empty from '@src/components/Empty'
  13. import { Store } from '@src/app'
  14. import useApi from '@src/Hook/useApi'
  15. import { getBookCategoryList } from '@src/server/classify'
  16. import { share } from '@src/utils'
  17. interface Props {
  18. store: Store;
  19. }
  20. const Index = forwardRef((props: Props) => {
  21. const { classifyStore, indexStore } = props.store
  22. const { getBookPageList } = useApi()
  23. const [pageShow, setPageShow] = useState(false)//防止小程序切换页面组件不会被真实卸载,阻止不必要的操作
  24. const [open, setOpen] = useState(false)
  25. const [showEmpty, setShowEmpty] = useState(false)
  26. const [isSetOpen, setIsSetOpen] = useState(true)
  27. // 获取分类列表
  28. useEffect(() => {
  29. if (pageShow) {
  30. let { workDirection } = classifyStore
  31. getBookCategoryList(workDirection).then((res: any) => {
  32. if (res?.data?.code === 200) {
  33. classifyStore.setData({ classifyData: res?.data?.data })
  34. }
  35. })
  36. }
  37. }, [classifyStore.workDirection, pageShow]); // Dependency array, runs effect only once on mount and unmount
  38. //获取书籍列表
  39. useEffect(() => {
  40. if (pageShow) {
  41. let { workDirection } = classifyStore
  42. let { categoryId } = classifyStore
  43. categoryId.id != 0 && classifyStore.categoryId && getList({ workDirection, categoryId: categoryId.id, pageNum: 1, pageSize: 10, type: 'class' }).then((res: any) => {
  44. if (res?.data?.data?.total === 0) {
  45. setShowEmpty(true)
  46. }else{
  47. setShowEmpty(false)
  48. }
  49. })
  50. }
  51. }, [classifyStore.workDirection, classifyStore.categoryId, pageShow])
  52. // 使用 useDidShow 代替 onShow
  53. useDidShow(() => {
  54. setPageShow(true)
  55. });
  56. // 使用 useDidHide 如果需要在页面隐藏时做一些处理
  57. useDidHide(() => {
  58. setPageShow(false)
  59. });
  60. //上拉刷新
  61. const refresh = async () => {
  62. let { workDirection } = classifyStore
  63. let { categoryId } = classifyStore
  64. getList({ workDirection, categoryId: categoryId.id, pageNum: 1, pageSize: 10, type: 'class' }).then((res: any) => {
  65. if (res?.data?.data?.total === 0) {
  66. setShowEmpty(true)
  67. }else{
  68. setShowEmpty(false)
  69. }
  70. })
  71. }
  72. //下拉加载
  73. const load = () => {
  74. return new Promise((resolve, reject) => {
  75. let { size, total, records } = classifyStore.bookList
  76. let pageSize = size + 10
  77. let { workDirection } = classifyStore
  78. let { categoryId } = classifyStore
  79. if (records?.length >= total) {
  80. Taro.showToast({
  81. title: '没有更多了~~',
  82. duration: 2000,
  83. icon: 'none'
  84. })
  85. reject()
  86. return
  87. } else {
  88. getList({ workDirection, categoryId: categoryId.id, pageNum: 1, pageSize, type: 'class' }).then((res:any) => {
  89. if (res?.data?.data?.total === 0) {
  90. setShowEmpty(true)
  91. }else{
  92. setShowEmpty(false)
  93. }
  94. resolve(true)
  95. })
  96. }
  97. })
  98. }
  99. const getList = async (params) => {
  100. return getBookPageList(params)
  101. }
  102. // 分享
  103. useShareAppMessage(async () => {
  104. // 获取当前页面栈
  105. const pages = Taro.getCurrentPages()
  106. // 获取栈顶的页面,即当前页面
  107. const currentPage = pages[pages.length - 1]
  108. // 获取页面的路径和参数
  109. const route = currentPage.route // 页面路径
  110. if (route) {
  111. let shareInfo = await share({ pagePath: route })
  112. let { sharePicUrl, shareTitles, pageName, pagePath } = shareInfo
  113. return {
  114. title: shareTitles || app.appInfo?.appName + '-' + pageName,
  115. path: pagePath || undefined,
  116. imageUrl: sharePicUrl || undefined
  117. }
  118. }
  119. return {}
  120. })
  121. return (
  122. <View className='index'>
  123. {classifyStore?.classifyData?.length > 0 && <View style={{ background: "#fff" }} className='classList' onClick={() => { setIsSetOpen(false) }}>
  124. <BookTypeTabs open={open} onCallback={setOpen} typeValue={classifyStore?.classifyData} typeTabIndex={classifyStore.categoryId} workDirection={classifyStore.workDirection} ></BookTypeTabs>
  125. </View>}
  126. <View onTouchStart={() => { setIsSetOpen(true) }}>
  127. <ScrollViewHoc
  128. callback={(top) => {
  129. if (isSetOpen) {
  130. if (top > 100 && open) {
  131. setOpen(false)
  132. }
  133. if (top < 100 && !open) {
  134. setOpen(true)
  135. }
  136. }
  137. }} load={load} refresh={refresh} reloadState={open} filterClassName='.classList' navHeight={indexStore.navHeight}>
  138. <View className='w row for_top1 pd_btm'>
  139. {
  140. classifyStore?.bookList?.records?.length > 0 && classifyStore?.bookList?.records?.map((item, index) => {
  141. return <>
  142. <View key={item.bookId} ><BookboxRowBig {...item} /></View>
  143. </>
  144. })
  145. }
  146. {
  147. showEmpty && <Empty />
  148. }
  149. </View>
  150. </ScrollViewHoc>
  151. </View>
  152. </View>
  153. );
  154. });
  155. export default TopNavBar(inject('store')(observer(Index)), {
  156. tab: true,
  157. tabVal: [{ value: 0, text: "男生" }, { value: 1, text: "女生" }],
  158. backgroundColor: '#fff',
  159. color: "#333",
  160. search: false,
  161. });