Album.tsx 13 KB

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