PhotoWall.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { View, Image } from "@tarojs/components";
  2. import './PhotoWall.scss'
  3. import Taro, { usePullDownRefresh, useReachBottom, useRouter } from "@tarojs/taro";
  4. import { useEffect, useState } from "react";
  5. import NewTimePicker from "@/_health/base/new_timepicker";
  6. import { getAlbums } from "@/services/health";
  7. import ListFooter from "@/_health/components/list_footer";
  8. import Layout from "@/components/layout/layout";
  9. import { NaviBarTitleShowType, TemplateType } from "@/utils/types";
  10. import NoRecord from "@/_health/components/no_record";
  11. import { useTranslation } from "react-i18next";
  12. let useRoute;
  13. let useNavigation;
  14. let scenario = '';
  15. if (process.env.TARO_ENV == 'rn') {
  16. useRoute = require("@react-navigation/native").useRoute
  17. useNavigation = require("@react-navigation/native").useNavigation
  18. }
  19. const scale = '?x-oss-process=image/resize,w_150,limit_0'
  20. export default function PhotoWall() {
  21. const screenWidth = Taro.getWindowInfo().screenWidth
  22. const space = 2
  23. const itemWidth = (screenWidth - space * 2) / 3.0
  24. const pageSize = 10
  25. const [pageIndex, setPageIndex] = useState(1)
  26. const [triggered, setTriggered] = useState(false)
  27. const [list, setList] = useState<any>([])
  28. const [total, setTotal] = useState(0)
  29. const [loading, setLoading] = useState(false)
  30. const [loaded, setLoaded] = useState(false)
  31. const {t} = useTranslation()
  32. let router
  33. let navigation;
  34. if (useNavigation) {
  35. navigation = useNavigation()
  36. }
  37. if (process.env.TARO_ENV == 'rn') {
  38. router = useRoute()
  39. }
  40. else {
  41. router = useRouter()
  42. }
  43. useEffect(() => {
  44. refresh()
  45. Taro.setNavigationBarTitle({
  46. title:t('health.photo_wall')
  47. })
  48. // getAlbumsData(router.params.window)
  49. }, [])
  50. usePullDownRefresh(() => {
  51. refresh()
  52. })
  53. function refresh() {
  54. getList(1)
  55. }
  56. useReachBottom(() => {
  57. more()
  58. // setPageIndex(pageIndex+1)
  59. // getHistory()
  60. })
  61. function more() {
  62. if (total <= pageIndex*pageSize || loading) {
  63. return;
  64. }
  65. var page = pageIndex + 1
  66. getList(page)
  67. }
  68. function getList(page) {
  69. if (loading) return
  70. setPageIndex(page)
  71. setLoading(true)
  72. if (page == 1)
  73. setTriggered(true)
  74. getAlbums({
  75. page: page,
  76. limit: pageSize,
  77. window: router.params.window
  78. }).then(res => {
  79. var array: any = [];
  80. (res as any).data.map(item => {
  81. array.push(...item.images)
  82. })
  83. // setList(array)
  84. setLoaded(true)
  85. setLoading(false)
  86. setTriggered(false)
  87. if (page == 1) {
  88. setTotal((res as any).total)
  89. setList(array)
  90. } else {
  91. setList(list.concat(array))
  92. }
  93. Taro.stopPullDownRefresh()
  94. // setMedias((res as any).data)
  95. })
  96. }
  97. function getCountImgs(index) {
  98. // var imgs:any = []
  99. const start = Math.max(0, index - 10)
  100. const end = Math.min(list.length, index + 10 + 1)
  101. return list.slice(start, end)
  102. }
  103. function detail() {
  104. return <View style={{ display: 'flex', flexDirection: 'column' }}>
  105. <View className="photo_wall2">
  106. {
  107. list.map((item, index) => {
  108. return <Image key={index}
  109. src={item + scale}
  110. mode="aspectFill"
  111. onClick={() => {
  112. Taro.previewImage({
  113. current: item,
  114. urls: getCountImgs(index)
  115. })
  116. }}
  117. style={{ width: itemWidth, height: itemWidth, backgroundColor: '#f5f5f5', marginBottom: space }} />
  118. })
  119. }
  120. <View style={{ width: itemWidth, height: itemWidth }} />
  121. <View style={{ width: itemWidth, height: itemWidth }} />
  122. </View>
  123. {
  124. loaded && pageIndex*pageSize >= total && <ListFooter noMore={true} />
  125. }
  126. </View>
  127. }
  128. return <View style={{ position: 'relative' }}>
  129. <Layout children={detail()}
  130. // title={router.params.title}
  131. isFastSleepTheme={router.params.type == 'time'}
  132. secondPage={true}
  133. titleColor={'#fff'}
  134. title={t('health.photo_wall')}
  135. type={TemplateType.customHeader}
  136. refresh={() => { refresh() }}
  137. triggered={triggered}
  138. more={() => { more() }}
  139. titleShowStyle={NaviBarTitleShowType.scrollToShow}
  140. />
  141. {
  142. loaded && list.length == 0 && <NoRecord />
  143. }
  144. </View>
  145. }