Album.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
  2. import { getAlbums, getAlbumsStat } from "@/services/health";
  3. import { View, Text, Image, ScrollView } from "@tarojs/components";
  4. import { useEffect, useState } from "react";
  5. import './Album.scss'
  6. import '@/features/health/History.scss'
  7. import NewHeader, { NewHeaderType } from "@/_health/components/new_header";
  8. import { rpxToPx } from "@/utils/tools";
  9. import NewButton, { NewButtonType } from "@/_health/base/new_button";
  10. import { MainColorType } from "@/context/themes/color";
  11. import { getThemeColor } from "@/features/health/hooks/health_hooks";
  12. import dayjs from "dayjs";
  13. import Taro from "@tarojs/taro";
  14. import StickyDateList from "@/_health/components/sticky_date_list";
  15. import ListFooter from "@/_health/components/list_footer";
  16. import CoverList from "@/_health/components/cover_list";
  17. import { TimeFormatter } from "@/utils/time_format";
  18. import { useTranslation } from "react-i18next";
  19. import RightArrowRow from "@/_health/components/right_arrow_row";
  20. let myScrollTop = 0
  21. export default function Album() {
  22. const [medias, setMedias] = useState<any>([])
  23. const [window, setWindow] = useState('')
  24. const [stat, setStat] = useState<any>(null)
  25. const [isPulling, setIsPulling] = useState(false)
  26. const [itemLayouts, setItemLayouts] = useState<any>([])
  27. const [showDate, setShowDate] = useState(false)
  28. const [date, setDate] = useState('')
  29. const [page, setPage] = useState(1)
  30. const [total, setTotal] = useState(0)
  31. const [loading, setLoading] = useState(false)
  32. const { t } = useTranslation()
  33. useEffect(() => {
  34. getAlbumsData('')
  35. }, [])
  36. useEffect(() => {
  37. if (medias.length > 0) {
  38. setTimeout(() => {
  39. measureItemLayouts()
  40. }, 500)
  41. }
  42. }, [medias])
  43. function more() {
  44. if (loading) return;
  45. if (total == medias.length) return;
  46. var index = page;
  47. index++;
  48. setPage(index)
  49. getAlbumsData(window, index)
  50. }
  51. function getAlbumsData(str, index = 1) {
  52. // setIsPulling(true)
  53. setLoading(true)
  54. getAlbums({
  55. page: index,
  56. limit: 10,
  57. window: str
  58. }).then(res => {
  59. if (index == 1) {
  60. setMedias((res as any).data)
  61. setTotal((res as any).total)
  62. }
  63. else {
  64. setMedias([...medias, ...(res as any).data])
  65. }
  66. setIsPulling(false)
  67. setLoading(false)
  68. }).catch(e => {
  69. setLoading(false)
  70. })
  71. getAlbumsStat().then(res => {
  72. setStat(res)
  73. })
  74. }
  75. // function historyMonth(index, preIndex) {
  76. // var showDate = false;
  77. // var dateStr = ''
  78. // if (index == 0) {
  79. // var currentDate = (medias[index].date + '').substring(0, 6)
  80. // var now = dayjs().format('YYYYMM')
  81. // showDate = true
  82. // dateStr = currentDate.substring(0, 4) + '年' + currentDate.substring(4, 6) + '月'
  83. // }
  84. // else {
  85. // var currentDate = (medias[index].date + '').substring(0, 6)
  86. // var now = (medias[index - 1].date + '').substring(0, 6)
  87. // if (currentDate != now) {
  88. // showDate = true
  89. // // dateStr = currentDate
  90. // dateStr = currentDate.substring(0, 4) + '年' + currentDate.substring(4, 6) + '月'
  91. // }
  92. // }
  93. // if (showDate) {
  94. // return <View className="history_year_month h30 g01">{dateStr}</View>
  95. // }
  96. // return <View />
  97. // }
  98. function measureItemLayouts() {
  99. const query = Taro.createSelectorQuery()
  100. medias.forEach((item, index) => {
  101. query.select(`#history-${index}`).boundingClientRect()
  102. });
  103. query.exec((res) => {
  104. var layouts: any = []
  105. res.forEach((rect, index) => {
  106. if (rect) {
  107. layouts[index] = rect.top + myScrollTop
  108. }
  109. });
  110. setItemLayouts(layouts)
  111. })
  112. }
  113. function onScroll(e) {
  114. var top = e.detail.scrollTop
  115. myScrollTop = top
  116. if (e.detail.scrollTop > 60) {
  117. Taro.setNavigationBarTitle({
  118. title: t('health.photos')
  119. })
  120. }
  121. else {
  122. Taro.setNavigationBarTitle({
  123. title: ''
  124. })
  125. }
  126. if (itemLayouts.length > 0) {
  127. var i = -1
  128. var date = ''
  129. medias.forEach((item, index) => {
  130. if (top >= itemLayouts[index] - 50) {
  131. i = index
  132. var currentDate = (medias[index].date + '').substring(0, 6)
  133. date = currentDate.substring(0, 4) + '年' + currentDate.substring(4, 6) + '月'
  134. }
  135. })
  136. setShowDate(i != -1)
  137. setDate(date)
  138. }
  139. else {
  140. setShowDate(false)
  141. setDate('')
  142. }
  143. }
  144. function historyYear(index) {
  145. var showDate = false;
  146. var dateStr2: any = ''
  147. if (index == 0) {
  148. var currentDate = global.language == 'en' ? dayjs(medias[index].timestamp).format('YYYY') : dayjs(medias[index].timestamp).format('YYYY年')
  149. var now = global.language == 'en' ? dayjs().format('YYYY') : dayjs().format('YYYY年')
  150. if (currentDate != now) {
  151. showDate = true
  152. dateStr2 = currentDate
  153. }
  154. }
  155. else {
  156. var currentDate = global.language == 'en' ? dayjs(medias[index].timestamp).format('YYYY') : dayjs(medias[index].timestamp).format('YYYY年')
  157. var now = global.language == 'en' ? dayjs(medias[index - 1].timestamp).format('YYYY') : dayjs(medias[index - 1].timestamp).format('YYYY年')
  158. if (currentDate != now) {
  159. showDate = true
  160. dateStr2 = currentDate
  161. }
  162. }
  163. if (showDate) {
  164. return <View className="history_year_month h42 bold">{dateStr2}</View>
  165. }
  166. return <View />
  167. }
  168. function historyDate(item, index) {
  169. if (index == 0) {
  170. if (global.language == 'zh' && TimeFormatter.isToday(item.timestamp)) {
  171. return '今天'
  172. }
  173. if (global.language == 'zh' && TimeFormatter.isYesterday(item.timestamp)) {
  174. return '昨天'
  175. }
  176. return dayjs(item.timestamp).format('DD')
  177. }
  178. if (dayjs(item.timestamp).format('MM-DD') ==
  179. dayjs(medias[index - 1].timestamp).format('MM-DD')) {
  180. return ''
  181. }
  182. if (global.language == 'zh' && TimeFormatter.isToday(item.timestamp)) {
  183. return '今天'
  184. }
  185. if (global.language == 'zh' && TimeFormatter.isYesterday(item.timestamp)) {
  186. return '昨天'
  187. }
  188. return dayjs(item.timestamp).format('DD')
  189. }
  190. function historyMonth(item, index) {
  191. if (index == 0) {
  192. if (global.language == 'zh' && TimeFormatter.isToday(item.timestamp)) {
  193. return ''
  194. }
  195. if (global.language == 'zh' && TimeFormatter.isYesterday(item.timestamp)) {
  196. return ''
  197. }
  198. return dayjs(item.timestamp).format('MMM')
  199. }
  200. if (dayjs(item.timestamp).format('MM-DD') ==
  201. dayjs(medias[index - 1].timestamp).format('MM-DD')) {
  202. return ''
  203. }
  204. if (global.language == 'zh' && TimeFormatter.isToday(item.timestamp)) {
  205. return ''
  206. }
  207. if (global.language == 'zh' && TimeFormatter.isYesterday(item.timestamp)) {
  208. return ''
  209. }
  210. return dayjs(item.timestamp).format('MMM')
  211. }
  212. return <StickyDateList onRefresherRefresh={() => {
  213. setIsPulling(true)
  214. setPage(1)
  215. getAlbumsData(window, 1)
  216. }} isPulling={isPulling}
  217. onScroll={onScroll}
  218. showDate={showDate}
  219. date={date}
  220. loadMore={more}
  221. >
  222. <View style={{ display: 'flex', flexDirection: 'column' }}>
  223. <NewHeader type={NewHeaderType.left} title={t('health.photos')} />
  224. {/* {stat && <ScrollView style={{ width: rpxToPx(750), flexDirection: 'row', display: 'flex', height: rpxToPx(72) }} scrollX enableFlex showScrollbar={false}>
  225. <View style={{ width: rpxToPx(40), flexShrink: 0 }} />
  226. <NewButton type={NewButtonType.img} onClick={() => {
  227. setWindow('')
  228. getAlbumsData('')
  229. }}>
  230. <View className="streak_toolbar_btn"
  231. style={{ backgroundColor: window == '' ? '#B2B2B21A' : 'transparent' }}
  232. >
  233. <Text className={window == '' ? 'bold h30' : 'h30'}
  234. style={{ color: window == '' ? '#000' : MainColorType.g01, marginRight: 5 }}>全部 </Text>
  235. <Text className="h20" style={{ color: window == '' ? '#000' : MainColorType.g01 }}>{stat.total}</Text>
  236. </View>
  237. </NewButton>
  238. {
  239. stat.items.map((item, index) => {
  240. return <View key={index}>
  241. <NewButton type={NewButtonType.img} onClick={() => {
  242. setWindow(item.window)
  243. getAlbumsData(item.window)
  244. }}>
  245. <View className="streak_toolbar_btn"
  246. style={{ backgroundColor: window == item.window ? getThemeColor(item.window) + '1A' : 'transparent' }}
  247. >
  248. <Text className={window == item.window ? 'bold h30' : 'h30'}
  249. style={{ color: window == item.window ? getThemeColor(item.window) : MainColorType.g01, marginRight: 5 }}>{item.window} </Text>
  250. <Text className="h20" style={{ color: window == item.window ? getThemeColor(item.window) : MainColorType.g01 }}>{' ' + item.image_count}</Text>
  251. </View>
  252. </NewButton>
  253. </View>
  254. })
  255. }
  256. <View style={{ width: rpxToPx(40), flexShrink: 0 }} />
  257. </ScrollView>} */}
  258. {/* <View style={{ height: rpxToPx(36) }} /> */}
  259. {/* <View className="photo_wall" onClick={() => {
  260. jumpPage('/pages/account/PhotoWall?window=' + window)
  261. }}>
  262. <View style={{ flex: 1 }} />
  263. <Text className="photo_wall_text">Photo Wall</Text>
  264. <Image className="photo_wall_arrow" src={require('@assets/_health/arrow2.png')} />
  265. <View className="album_line" />
  266. </View> */}
  267. <RightArrowRow title={t('health.photo_wall')} onClick={() => {
  268. jumpPage('/pages/account/PhotoWall?window=' + window)
  269. }} />
  270. {
  271. medias.map((item, index) => {
  272. return <View key={index} id={`history-${index}`} style={{ display: 'flex', flexDirection: 'column', paddingTop: rpxToPx(6), backgroundColor: '#fff' }}>
  273. {
  274. historyYear(index)
  275. }
  276. <View className="history_item2" style={{ paddingRight: rpxToPx(30) }}>
  277. <View className="cell_date" >
  278. <View className="h42 bold" style={{ lineHeight: rpxToPx(60) + 'px' }}>{historyDate(item, index)}</View>
  279. <View className="h24 bold" style={{ marginLeft: rpxToPx(6), marginTop: rpxToPx(13), lineHeight: rpxToPx(47) + 'px' }}>{historyMonth(item, index)}</View>
  280. </View>
  281. {/* <Text className="cell_date">{(item.date + '').substring(6, 9)}</Text> */}
  282. <View style={{ display: 'flex', flex: 1 }}>
  283. <CoverList
  284. imgs={item.images}
  285. count={item.images.length}
  286. />
  287. {/* <View className="media" style={{ marginRight: item.images.length == 4 ? 80 : 0 }}>
  288. {
  289. item.images.map((photo, i) => {
  290. return <Image onClick={() => {
  291. Taro.previewImage({
  292. current: photo,
  293. urls: item.images
  294. })
  295. }} mode="aspectFill" src={photo} key={i * 900} className="media_item" />
  296. })
  297. }
  298. </View> */}
  299. </View>
  300. {/* <View className="border_footer_line" /> */}
  301. </View>
  302. </View>
  303. })
  304. }
  305. <View style={{ height: rpxToPx(40), flexShrink: 0, backgroundColor: '#fff' }} />
  306. <ListFooter noMore={(medias.length > 0) && (total == medias.length)} />
  307. </View>
  308. </StickyDateList>
  309. }