ClockIndex.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
  2. import { View, Image, ScrollView } from "@tarojs/components";
  3. import './ClockIndex.scss'
  4. import TabBar from "@/components/navigation/TabBar";
  5. import { IconClose, IconNext } from "@/components/basic/Icons";
  6. import { rpxToPx } from "@/utils/tools";
  7. import { useDispatch, useSelector } from "react-redux";
  8. import { useEffect, useState } from "react";
  9. import { homeInfo } from "@/services/health";
  10. import dayjs from "dayjs";
  11. import { setActions, setEvents, setTimeData } from "@/store/record";
  12. import { TimeFormatter } from "@/utils/time_format";
  13. import { getInfo } from "@/services/user";
  14. import Taro from "@tarojs/taro";
  15. import ChooseActions from "./components/choose_actions";
  16. import NewButton, { NewButtonType } from "@/_health/base/new_button";
  17. import { BASE_IMG_URL } from "@/services/http/api";
  18. import { useTranslation } from "react-i18next";
  19. import NoData from "@/_health/components/no_data";
  20. var timer
  21. let useNavigation,LinearGradient;
  22. if (process.env.TARO_ENV == 'rn') {
  23. useNavigation = require("@react-navigation/native").useNavigation
  24. LinearGradient = require('react-native-linear-gradient').default
  25. }
  26. export default function ClockIndex() {
  27. const systemInfo: any = Taro.getWindowInfo ? Taro.getWindowInfo() : Taro.getSystemInfoSync();
  28. // const navigationBarHeight = systemInfo.statusBarHeight + 44;
  29. const screenHeight = systemInfo.screenHeight
  30. const user = useSelector((state: any) => state.user);
  31. const record = useSelector((state: any) => state.record);
  32. const [loaded, setLoaded] = useState(false)
  33. const [showRetry, setShowRetry] = useState(false)
  34. const [home, setHome] = useState<any>(null)
  35. const [count, setCount] = useState(0)
  36. const [currentTimeData, setCurrentTimeData] = useState<any>(null)
  37. const [showChoose, setShowChoose] = useState(false)
  38. const [selScenario, setSelScenario] = useState('')
  39. const [scenarios, setScenarios] = useState<any>([])
  40. const dispatch = useDispatch();
  41. const { t } = useTranslation()
  42. global.dispatch = dispatch;
  43. dayjs.locale(global.language == 'en' ? 'en' : 'zh-cn');
  44. let navigation;
  45. if (useNavigation) {
  46. navigation = useNavigation()
  47. }
  48. useEffect(() => {
  49. getHomeData()
  50. }, [user.isLogin])
  51. function getHomeData() {
  52. homeInfo().then(res => {
  53. setLoaded(true)
  54. setHome(res)
  55. setCount(1)
  56. setScenarios((res as any).scenarios ?? [])
  57. dispatch(setEvents((res as any).events))
  58. dispatch(setActions((res as any).actions))
  59. setShowRetry(false)
  60. }).catch(e => {
  61. setShowRetry(true)
  62. })
  63. }
  64. useEffect(() => {
  65. getInfo()
  66. Taro.eventCenter.on('refreshClockIndex', getHomeData)
  67. timer = setInterval(() => {
  68. setCount(pre => pre + 1)
  69. }, 1000)
  70. return () => {
  71. clearInterval(timer)
  72. Taro.eventCenter.off('refreshClockIndex')
  73. }
  74. }, [])
  75. useEffect(() => {
  76. getTime()
  77. }, [count])
  78. useEffect(() => {
  79. if (currentTimeData) {
  80. dispatch(setTimeData(currentTimeData))
  81. }
  82. }, [currentTimeData])
  83. function tapItem(index) {
  84. if (!user.isLogin) {
  85. jumpPage('/_account/pages/ChooseAuth', 'ChooseAuth', navigation)
  86. return
  87. }
  88. var scenarioObj = scenarios[index]
  89. switch (scenarioObj.scenario) {
  90. case 'MEAL':
  91. setShowChoose(true)
  92. setSelScenario('MEAL')
  93. break;
  94. case 'ACTIVITY':
  95. setShowChoose(true)
  96. setSelScenario('ACTIVITY')
  97. break;
  98. case 'FAST':
  99. jumpPage(`/_record/pages/time_record?scenario=FAST`)
  100. break
  101. case 'SLEEP':
  102. jumpPage(`/_record/pages/time_record?scenario=SLEEP`)
  103. break
  104. case 'METRIC':
  105. jumpPage('/pages/metric/Metric')
  106. break
  107. case 'MOVE':
  108. jumpPage('/_health/pages/move')
  109. break;
  110. }
  111. }
  112. function getTime() {
  113. if (!home) return
  114. var now = dayjs().format('HH:mm')
  115. for (var i = 0; i < home.time_messages.length; i++) {
  116. var obj = home.time_messages[i]
  117. if (now >= obj.start_time && now < obj.end_time) {
  118. setCurrentTimeData(obj)
  119. return
  120. }
  121. }
  122. setCurrentTimeData(home.time_messages[home.time_messages.length - 1])
  123. }
  124. function getBackground() {
  125. var time = record.time
  126. if (!time) return '#fff'
  127. const { background_colors } = time
  128. if (!background_colors) {
  129. return '#fff'
  130. }
  131. else if (background_colors.length == 1) {
  132. return background_colors[0]
  133. }
  134. return `linear-gradient(to bottom, ${background_colors[0]}, ${background_colors[1]})`
  135. }
  136. function itemTime(item) {
  137. if (item.event && item.event.status == 'OG') {
  138. return TimeFormatter.countdown(item.event.time.start_timestamp)
  139. }
  140. return ''
  141. }
  142. if (!loaded && showRetry) return <NoData refresh={
  143. () => {
  144. getHomeData()
  145. }
  146. } />
  147. function bgView() {
  148. if (process.env.TARO_ENV == 'weapp') {
  149. return <View className="main_bg" style={{ background: getBackground() }} />
  150. }
  151. var time = record.time
  152. if (!time) return <View />
  153. const { background_colors } = time
  154. if (!background_colors) {
  155. return <View />
  156. }
  157. else if (background_colors.length == 1) {
  158. return <View />
  159. }
  160. return <LinearGradient style={{
  161. position: 'absolute',
  162. left: 0,
  163. top: 0,
  164. width: rpxToPx(750),
  165. height: screenHeight,
  166. zIndex: 0,
  167. pointerEvents: 'none'
  168. }}
  169. colors={[background_colors[0], background_colors[1]]}
  170. start={{ x: 0, y: 0 }}
  171. end={{ x: 0, y: 1 }} pointerEvents="none" />
  172. }
  173. if (!loaded) return <View />
  174. return <View style={{ position: 'relative' }}>
  175. {
  176. bgView()
  177. }
  178. <ScrollView style={{ height: process.env.TARO_ENV == 'weapp' ? '100vh' : '100%' }} scrollY><View style={{ position: 'relative', overflow: 'hidden', minHeight: process.env.TARO_ENV == 'weapp' ? '101vh' : '100%' }}>
  179. <View className="h60 bold" style={{ marginLeft: rpxToPx(52), marginTop: rpxToPx(212) }}>{record.time ? record.time.greeting : ''}</View>
  180. <View style={{ display: 'flex', flexDirection: 'row' }}>
  181. <View className="h44 bold" style={{ marginLeft: rpxToPx(52), marginTop: rpxToPx(66), marginBottom: rpxToPx(32), position: 'relative', paddingBottom: rpxToPx(20) }}>{t('health.check_in')}
  182. <View className="tab_border_line" />
  183. </View>
  184. </View>
  185. <View className="operate_panel">
  186. {
  187. scenarios.map((item, index) => {
  188. return <NewButton type={NewButtonType.custom}
  189. key={index}
  190. onClick={() => {
  191. tapItem(index)
  192. }}>
  193. <View className="operate_card" >
  194. <View className="h36 bold">{item.title}</View>
  195. <View className="h50 bold">{itemTime(item)}</View>
  196. <View style={{ flex: 1 }} />
  197. <View style={{ display: 'flex',flexDirection:'row', justifyContent: 'space-between', alignItems: 'center', }}>
  198. <Image src={item.icon} style={{ width: rpxToPx(48), height: rpxToPx(48) }} />
  199. <Image src={item.action_icon} style={{ width: rpxToPx(36), height: rpxToPx(36) }} />
  200. </View>
  201. </View>
  202. </NewButton>
  203. })
  204. }
  205. </View>
  206. <View style={{ height: 100 }} />
  207. </View>
  208. </ScrollView>
  209. {
  210. showChoose && <ChooseActions
  211. close={() => {
  212. setShowChoose(false)
  213. }}
  214. quick={() => {
  215. jumpPage(`/_record/pages/log_record?scenario=${selScenario}&quick=1`)
  216. }}
  217. chooseImg={(array) => {
  218. jumpPage(`/_record/pages/log_record?scenario=${selScenario}&imgs=${JSON.stringify(array)}`)
  219. }}
  220. chooseText={() => {
  221. jumpPage(`/_record/pages/log_record?scenario=${selScenario}&only_text=1`)
  222. }}
  223. />
  224. }
  225. {
  226. process.env.TARO_ENV == 'weapp' && <TabBar index={0} />
  227. }
  228. </View>
  229. }