index.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import React, { useEffect, useMemo, useState } from 'react';
  2. import { observer, inject } from 'mobx-react';
  3. import { useDidHide, useDidShow } from '@tarojs/taro';
  4. //==========components组件引用========================
  5. import TopNavBar from '@src/components/TopNavBar';
  6. import { Store } from '@src/app';
  7. import useApi from '@src/Hook/useApi';
  8. import BannerBox from '@src/components/PupPetry/BannerBox'
  9. import { View } from '@tarojs/components';
  10. import Taro from '@tarojs/taro';
  11. import BookHead from '@src/components/PupPetry/BookHead';
  12. import BookHotTabs from '@src/components/PupPetry/BookHotTabs';
  13. import BookboxColumnSmall from '@src/components/PupPetry/BookBox/BookboxColumnSmall';
  14. import BookboxRowMiddle from '@src/components/PupPetry/BookBox/BookboxRowMiddle';
  15. interface Props {
  16. store: Store;
  17. }
  18. const Index: React.FC<Props> = ({ store }) => {
  19. const { appInfoStore, bookStore, indexStore } = store
  20. const { indexComponents, getBookPageList } = useApi(appInfoStore)
  21. const [pageShow, setPageShow] = useState(false)//防止小程序切换页面组件不会被真实卸载,阻止不必要的操作
  22. // 首页配置信息
  23. const indexConfig = useMemo(() => {
  24. let obj = {}
  25. let thatPage = appInfoStore?.appInfo?.appPageComponentList.find(item => item.pagePath === 'pages/index/index')
  26. let thatWd = thatPage?.appComponentListVOS?.find(item => item.workDirection === bookStore.workDirection)
  27. thatWd?.appComponentListVOS?.forEach((item, index) => {
  28. return obj[item.componentType] = { ...item, eq: index }
  29. })
  30. return obj
  31. }, [indexComponents, pageShow, bookStore.workDirection])
  32. // 页面显示请求数据
  33. useEffect(() => {
  34. if (pageShow) {
  35. indexCompontent()
  36. }
  37. }, [pageShow, appInfoStore.token, bookStore.workDirection, appInfoStore.appInfo]); // Dependency array, runs effect only once on mount and unmount
  38. // 使用 useDidShow 代替 onShow
  39. useDidShow(() => {
  40. // 分享消息配置
  41. setPageShow(true)
  42. // log("Page is shown or brought to foreground");
  43. });
  44. // 使用 useDidHide 如果需要在页面隐藏时做一些处理
  45. useDidHide(() => {
  46. setPageShow(false)
  47. bookStore.setData({ bookList: [] })
  48. // log("Page is hidden or sent to background");
  49. });
  50. // 处理首页展示的组件的请求
  51. const indexCompontent = () => {
  52. // 小程序的组件配置
  53. Object.keys(indexConfig)?.forEach(key => {
  54. switch (key) {
  55. case "banners"://轮播
  56. getBanners()
  57. break;
  58. case "hot_books"://今日热书
  59. getToDayHotBooks()
  60. break;
  61. case "hot_category"://热门分类
  62. getHotCategory()
  63. break;
  64. }
  65. })
  66. }
  67. // 获取banners
  68. const getBanners = () => {
  69. indexComponents?.banners(bookStore.workDirection)
  70. }
  71. // 获取热门分类
  72. const getHotCategory = () => {
  73. indexComponents?.hot_category(bookStore.workDirection).then((res: any) => {
  74. let data = res?.data?.data
  75. if (data?.bookCategoryList?.length > 0) {
  76. getHotBooks(data?.bookCategoryList?.[0].id)
  77. }
  78. })
  79. }
  80. // 热门分类书籍
  81. const getHotBooks = (categoryId) => {
  82. if (categoryId) {
  83. getBookPageList({ pageNum: 1, pageSize: 8, workDirection: bookStore.workDirection, categoryId })
  84. }
  85. }
  86. // 今日热书
  87. const getToDayHotBooks = () => {
  88. indexComponents?.hot_books(bookStore.workDirection)
  89. }
  90. return <View style={{ display: "flex", flexFlow: 'column' }}>
  91. {/* 轮播 */}
  92. {indexConfig['banners'] && <View style={{ order: indexConfig['banners'].eq }}>
  93. <BannerBox data={indexStore?.indexBanners || []} />
  94. </View>}
  95. {/* 今日热门 */}
  96. {indexConfig['hot_books'] && <View style={{ order: indexConfig['banners'].eq }}>
  97. <BookHead showBtn={indexStore?.indexHotBooks?.showRightButton} title={indexStore?.indexHotBooks?.name || "今日热书"} fitTitle="更多" imgUrl={require('../../icon/right.png')} fitOnBack={() => {
  98. Taro.navigateTo({ url: `/pages/indexMore/index?title=今日热书&dataName=indexHotBooks` })
  99. }}></BookHead>
  100. <View ><BookboxRowMiddle {...indexStore?.indexHotBooks?.[appInfoStore?.appInfo?.appCategory === 1 ? "longBookInfoAppVOS" : "shortBookInfoAppVOS"]?.[0]} /></View>
  101. <View className='w row'>
  102. {
  103. indexStore?.indexHotBooks?.[appInfoStore?.appInfo?.appCategory === 1 ? "longBookInfoAppVOS" : "shortBookInfoAppVOS"]?.map((item, index) => {
  104. if (index > 0 && index < 5) {
  105. return <View key={index}><BookboxColumnSmall {...item} /></View>
  106. }
  107. })
  108. }
  109. </View>
  110. </View>}
  111. {/* 热门分类 */}
  112. {indexConfig['hot_category'] && <View style={{ order: indexConfig['banners'].eq }}>
  113. <BookHead showBtn={indexStore?.indexHotCategory?.showRightButton} title={indexStore?.indexHotCategory?.name || "热门分类"} fitTitle="全部分类" imgUrl={require('../../icon/right.png')} fitOnBack={() => { Taro.switchTab({ url: '/pages/classify/index' }) }} />
  114. <BookHotTabs hotValue={indexStore?.indexHotCategory?.bookCategoryList || []} onBack={(value) => {
  115. getHotBooks(value)
  116. }} />
  117. <View className='w row4'>
  118. {
  119. bookStore?.bookList?.records?.map((item, index) => {
  120. return <View key={index}><BookboxColumnSmall {...item} /></View>
  121. })
  122. }
  123. </View>
  124. </View>}
  125. </View>
  126. };
  127. export default TopNavBar(inject('store')(observer(Index)), {
  128. tab: true,
  129. tabVal: [{ value: 0, text: "男生" }, { value: 1, text: "女生" }],
  130. backgroundColor: '#fff',
  131. color: "#333",
  132. search: true
  133. });