Journal.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import JournalCover from "@/features/journal/components/journal_cover";
  2. import { getJournals } from "@/services/health";
  3. import { View, Text, Image } from "@tarojs/components";
  4. import { useEffect, useState } from "react";
  5. import './Journal.scss'
  6. import dayjs from "dayjs";
  7. import { rpxToPx } from "@/utils/tools";
  8. import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
  9. import NewHeader, { NewHeaderType } from "@/_health/components/new_header";
  10. import Taro from "@tarojs/taro";
  11. import StickyDateList from "@/_health/components/sticky_date_list";
  12. import { MainColorType } from "@/context/themes/color";
  13. import ListFooter from "@/_health/components/list_footer";
  14. import TimeTitleDesc from "@/_health/components/time_title_desc";
  15. export default function Journal() {
  16. const [journals, setJournals] = useState<any>([])
  17. const [isPulling, setIsPulling] = useState(false)
  18. const [itemLayouts, setItemLayouts] = useState<any>([])
  19. const [showDate, setShowDate] = useState(false)
  20. const [date, setDate] = useState('')
  21. const [page, setPage] = useState(1)
  22. const [total, setTotal] = useState(0)
  23. const [loading, setLoading] = useState(false)
  24. useEffect(() => {
  25. getJounalsData(1)
  26. }, [])
  27. useEffect(() => {
  28. if (journals.length > 0) {
  29. setTimeout(() => {
  30. measureItemLayouts()
  31. }, 500)
  32. }
  33. }, [journals])
  34. function more() {
  35. if (loading) return;
  36. if (total == journals.length) return;
  37. var index = page;
  38. index++;
  39. setPage(index)
  40. getJounalsData(index)
  41. }
  42. function getJounalsData(index = 1) {
  43. setLoading(true)
  44. getJournals({
  45. page: index,
  46. limit: 10
  47. }).then(res => {
  48. let list = (res as any).data
  49. list.forEach(element => {
  50. let array: any = []
  51. element.windows.map(item => {
  52. item.events.map(event => {
  53. event.moments && event.moments.map(moment => {
  54. if (moment.media && moment.media.length > 0) {
  55. moment.media.map(media => {
  56. array.push(media.url)
  57. })
  58. }
  59. })
  60. })
  61. })
  62. element.imgs = array
  63. });
  64. setLoading(false)
  65. if (index == 1) {
  66. setTotal((res as any).total)
  67. setJournals(list)
  68. setIsPulling(false)
  69. }
  70. else {
  71. setJournals([...journals, ...list])
  72. }
  73. }).catch(e => {
  74. setLoading(false)
  75. })
  76. }
  77. function measureItemLayouts() {
  78. const query = Taro.createSelectorQuery()
  79. journals.forEach((item, index) => {
  80. query.select(`#history-${index}`).boundingClientRect()
  81. });
  82. query.exec((res) => {
  83. var layouts: any = []
  84. res.forEach((rect, index) => {
  85. if (rect) {
  86. layouts[index] = rect.top
  87. }
  88. });
  89. setItemLayouts(layouts)
  90. })
  91. }
  92. function onScroll(e) {
  93. var top = e.detail.scrollTop
  94. if (itemLayouts.length > 0) {
  95. var i = -1
  96. var date = ''
  97. journals.forEach((item, index) => {
  98. if (top >= itemLayouts[index] - 50) {
  99. i = index
  100. var currentDate = (journals[index].date + '').substring(0, 6)
  101. date = currentDate.substring(0, 4) + '年' + currentDate.substring(4, 6) + '月'
  102. }
  103. })
  104. setShowDate(i != -1)
  105. setDate(date)
  106. }
  107. else {
  108. setShowDate(false)
  109. setDate('')
  110. }
  111. }
  112. function getTitle(item) {
  113. if (item.title) {
  114. return item.title;
  115. }
  116. if (item.moments) {
  117. return item.moments[0].title
  118. }
  119. return ''
  120. }
  121. function journalItem(window, index) {
  122. var array: any = [];
  123. window.events.map(event => {
  124. event.moments && event.moments.map(moment => {
  125. if (moment.media && moment.media.length > 0) {
  126. moment.media.map(media => {
  127. array.push(media.url)
  128. })
  129. }
  130. })
  131. })
  132. return <View style={{ flex: 1, display: 'flex', flexDirection: 'row', marginBottom: rpxToPx(12), overflow: 'hidden' }} key={index}>
  133. {
  134. array.length > 0 && <JournalCover imgs={array} />
  135. }
  136. <View style={{
  137. display: 'flex',
  138. flexDirection: 'column',
  139. flex: 1,
  140. overflow: 'hidden',
  141. backgroundColor: array.length == 0 ? '#f5f5f5' : 'transparent',
  142. padding: array.length == 0 ? rpxToPx(20) : 0,
  143. marginRight: rpxToPx(40)
  144. }}>
  145. {
  146. window.events.map((item2, index2) => {
  147. return <TimeTitleDesc
  148. key={index2 * 1000}
  149. className="line1"
  150. style={{ width: array.length > 0 ? rpxToPx(370) : rpxToPx(532) }}
  151. time={dayjs(item2.time.timestamp).format('HH:mm')}
  152. title={getTitle(item2)}
  153. desc={item2.moments && item2.moments.length > 0 ? item2.moments[0].description : ''}
  154. />
  155. })
  156. }
  157. </View>
  158. </View>
  159. }
  160. function historyMonth(index, preIndex) {
  161. var showDate = false;
  162. var dateStr = ''
  163. if (index == 0) {
  164. var currentDate = (journals[index].date + '').substring(0, 6)
  165. var now = dayjs().format('YYYYMM')
  166. showDate = true
  167. dateStr = currentDate.substring(0, 4) + '年' + currentDate.substring(4, 6) + '月'
  168. }
  169. else {
  170. var currentDate = (journals[index].date + '').substring(0, 6)
  171. var now = (journals[index - 1].date + '').substring(0, 6)
  172. if (currentDate != now) {
  173. showDate = true
  174. // dateStr = currentDate
  175. dateStr = currentDate.substring(0, 4) + '年' + currentDate.substring(4, 6) + '月'
  176. }
  177. }
  178. if (showDate) {
  179. return <View className="history_year_month h30 g01">{dateStr}</View>
  180. }
  181. return <View />
  182. }
  183. return <StickyDateList onRefresherRefresh={() => {
  184. setIsPulling(true)
  185. getJounalsData()
  186. }} isPulling={isPulling}
  187. onScroll={onScroll}
  188. showDate={showDate}
  189. date={date}
  190. loadMore={() => {
  191. more()
  192. }}
  193. ><View style={{ display: 'flex', flexDirection: 'column' }}>
  194. <NewHeader type={NewHeaderType.left} title="Journal" />
  195. {
  196. journals.map((item, index) => {
  197. return <View key={index}>
  198. {
  199. historyMonth(index, index - 1)
  200. }
  201. <View className="album_item" id={`history-${index}`} onClick={() => {
  202. jumpPage('/pages/account/JournalDetail?detail=' + JSON.stringify(item))
  203. }}>
  204. <Text className="cell_date">{(item.date + '').substring(6, 9)}</Text>
  205. <View style={{ display: 'flex', flexDirection: 'column', flex: 1 }}>
  206. {
  207. item.windows.map((window, i) => {
  208. return journalItem(window, i)
  209. })
  210. }
  211. </View>
  212. <View className="border_footer_line" />
  213. </View></View>
  214. })
  215. }
  216. <ListFooter noMore={(journals.length > 0) && (total == journals.length)} />
  217. </View></StickyDateList>
  218. }