ClockNew.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import { View, ScrollView, Swiper, SwiperItem } from "@tarojs/components";
  2. import './Clock.scss'
  3. import { useEffect, useRef, useState } from "react";
  4. import MainDayNightCard from "@/features/health/MainDayNightCard";
  5. import MainFastEatCard from "@/features/health/MainFastEatCard";
  6. import MainSleepActiveCard from "@/features/health/MainSleepActiveCard";
  7. import TabBar from "@/components/navigation/TabBar";
  8. import { rpxToPx } from "@/utils/tools";
  9. import MainSwiper from "@/features/health/MainSwiper";
  10. import MainConsole from "@/features/health/MainConsole";
  11. import MainHistory from "@/features/health/MainHistory";
  12. import { WindowType } from "@/utils/types";
  13. import { getArchived, windows } from "@/services/health";
  14. import { useDispatch, useSelector } from "react-redux";
  15. import health, { setEatArchived, setFastWithSleep, setLongFast, setRefreshs, setTitle, setWindows } from "@/store/health";
  16. import dayjs from "dayjs";
  17. import Modal from "@/components/layout/Modal.weapp";
  18. import Streak from "@/features/health/Streak";
  19. import Calendar from "@/features/health/calendar";
  20. import { MainColorType } from "@/context/themes/color";
  21. import NewButton, { NewButtonType } from "@/_health/base/new_button";
  22. import { getThemeColor } from "@/features/health/hooks/health_hooks";
  23. export default function ClockNew() {
  24. const [count, setCount] = useState(0)
  25. const [scrollLeft, setScrollLeft] = useState(rpxToPx(750) * 0)
  26. const user = useSelector((state: any) => state.user);
  27. const health = useSelector((state: any) => state.health);
  28. const [type, setType] = useState(WindowType.day)
  29. const [showCalendar, setShowCalendar] = useState(false)
  30. const [isPulling,setIsPulling] = useState(false)
  31. const healthRef = useRef(health)
  32. const dispatch = useDispatch();
  33. global.dispatch = dispatch;
  34. useEffect(() => {
  35. healthRef.current = health;
  36. }, [health])
  37. useEffect(() => {
  38. archived()
  39. }, [health.mode])
  40. useEffect(() => {
  41. setInterval(() => {
  42. setCount(index => index + 1)
  43. healthRef.current.refreshs.map((item) => {
  44. const strTime = dayjs(item).format('YYYY-MM-DDTHH:mm:ss')
  45. const now = dayjs().format('YYYY-MM-DDTHH:mm:ss')
  46. if (strTime == now) {
  47. getWindows()
  48. // archived();
  49. getArchived('EAT').then(res => {
  50. dispatch(setEatArchived((res as any).latest))
  51. })
  52. global.refreshHistory()
  53. }
  54. })
  55. }, 1000)
  56. }, [])
  57. useEffect(() => {
  58. getWindows();
  59. }, [user.isLogin])
  60. global.refreshWindow = () => {
  61. getWindows();
  62. archived();
  63. }
  64. function getWindows() {
  65. windows().then(res => {
  66. if (!(res as any).windows.night_day.night.target) {
  67. var date = new Date()
  68. var hour = date.getHours()
  69. if (hour >= 6 && hour < 18) {
  70. var date1 = new Date()
  71. date1.setHours(6)
  72. date1.setMinutes(0)
  73. date1.setSeconds(0)
  74. var date2 = new Date()
  75. date2.setHours(18)
  76. date2.setMinutes(0)
  77. date2.setSeconds(0);
  78. (res as any).windows.night_day.day.target = {
  79. start_timestamp: date1.getTime(),
  80. end_timestamp: date2.getTime(),
  81. duration: 12 * 60 * 60 * 1000
  82. };
  83. (res as any).windows.night_day.night.target = {
  84. start_timestamp: date2.getTime(),
  85. end_timestamp: date2.getTime() + 12 * 60 * 60 * 1000,
  86. duration: 12 * 60 * 60 * 1000
  87. }
  88. }
  89. else {
  90. var date1 = new Date()
  91. date1.setHours(18)
  92. date1.setMinutes(0)
  93. date1.setSeconds(0);
  94. (res as any).windows.night_day.day.target = {
  95. start_timestamp: date1.getTime(),
  96. end_timestamp: date1.getTime() + 12 * 60 * 60 * 1000,
  97. duration: 12 * 60 * 60 * 1000
  98. };
  99. (res as any).windows.night_day.night.target = {
  100. start_timestamp: date1.getTime() + 12 * 60 * 60 * 1000,
  101. end_timestamp: date1.getTime() + 12 * 60 * 60 * 1000 + 12 * 60 * 60 * 1000,
  102. duration: 12 * 60 * 60 * 1000
  103. }
  104. }
  105. }
  106. dispatch(setFastWithSleep((res as any).fast_with_sleep))
  107. dispatch(setWindows((res as any).windows))
  108. dispatch(setLongFast((res as any).long_fast))
  109. dispatch(setRefreshs((res as any).refresh_timestamps))
  110. setIsPulling(false)
  111. })
  112. archived()
  113. }
  114. function archived() {
  115. if (health.mode == 'EAT' || health.mode == 'ACTIVE') {
  116. getArchived(health.mode).then(res => {
  117. if (health.mode == 'EAT') {
  118. dispatch(setEatArchived((res as any).latest))
  119. }
  120. })
  121. }
  122. }
  123. function tapScroll(index) {
  124. setScrollLeft(rpxToPx(750) * index)
  125. }
  126. function scroll(e) {
  127. console.log(e.detail.scrollLeft)
  128. }
  129. function pageChanged(index) {
  130. }
  131. function typeChanged(str) {
  132. }
  133. function detail() {
  134. if (!health.windows) {
  135. return <View />
  136. }
  137. return <ScrollView style='height:100vh'
  138. refresherEnabled
  139. refresherBackground={MainColorType.g05}
  140. onRefresherRefresh={()=>{
  141. global.refreshWindow()
  142. setIsPulling(true)
  143. }}
  144. refresherTriggered={isPulling}
  145. scrollY onScroll={e => {
  146. if (e.detail.scrollTop > 240) {
  147. dispatch(setTitle(health.mode))
  148. }
  149. else {
  150. dispatch(setTitle(''))
  151. }
  152. }}>
  153. <MainSwiper count={count} pageChanged={pageChanged} typeChanged={typeChanged} />
  154. <MainConsole type={type} />
  155. <MainHistory />
  156. <View style={{ height: 200 }} />
  157. </ScrollView>
  158. }
  159. //https://blog.csdn.net/weixin_43525284/article/details/130182218
  160. return <View style={{ flex: 1 }}>
  161. {
  162. process.env.TARO_ENV == 'weapp' ? detail() : <ScrollView style={{ flex: 1, backgroundColor: MainColorType.bg }} onScroll={e => {
  163. if (e.detail.scrollTop > 240) {
  164. dispatch(setTitle(health.mode))
  165. }
  166. else {
  167. dispatch(setTitle(''))
  168. }
  169. }}>
  170. {
  171. detail()
  172. }
  173. </ScrollView>
  174. }
  175. {/* <View className="tabs">
  176. <View className="item" onClick={() => tapScroll(0)}> tab 0</View>
  177. <View className="item" onClick={() => tapScroll(1)}>tab 1</View>
  178. <View className="item" onClick={() => tapScroll(2)}>tab 2</View>
  179. </View>
  180. <ScrollView scrollX
  181. scrollLeft={scrollLeft}
  182. scrollWithAnimation={true}
  183. pagingEnabled={true}
  184. enableFlex
  185. onScroll={scroll}
  186. style={{ width: rpxToPx(750), display: 'flex', flexDirection: 'row', overflow: 'hidden' }}
  187. enhanced>
  188. <View style={{ display: 'flex', flexShrink: 0, width: rpxToPx(750) }}>
  189. <MainDayNightCard count={count} />
  190. </View>
  191. <View style={{ display: 'flex', flexShrink: 0, width: rpxToPx(750) }}>
  192. <MainFastEatCard count={count} />
  193. </View>
  194. <View style={{ display: 'flex', flexShrink: 0, width: rpxToPx(750) }}>
  195. <MainSleepActiveCard count={count} />
  196. </View>
  197. </ScrollView> */}
  198. <TabBar index={0} />
  199. </View>
  200. }