ClockIndex copy.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. Taro.eventCenter.trigger('hideTab', showChoose)
  77. }, [showChoose])
  78. useEffect(() => {
  79. getTime()
  80. }, [count])
  81. useEffect(() => {
  82. if (currentTimeData) {
  83. dispatch(setTimeData(currentTimeData))
  84. }
  85. }, [currentTimeData])
  86. function tapItem(index) {
  87. if (!user.isLogin) {
  88. jumpPage('/_account/pages/ChooseAuth', 'ChooseAuth', navigation)
  89. return
  90. }
  91. var scenarioObj = scenarios[index]
  92. switch (scenarioObj.scenario) {
  93. case 'MEAL':
  94. setShowChoose(true)
  95. setSelScenario('MEAL')
  96. break;
  97. case 'ACTIVITY':
  98. setShowChoose(true)
  99. setSelScenario('ACTIVITY')
  100. break;
  101. case 'FAST':
  102. jumpPage(`/_record/pages/time_record?scenario=FAST`, 'TimeRecord', navigation, {
  103. scenario: 'FAST'
  104. })
  105. break
  106. case 'SLEEP':
  107. jumpPage(`/_record/pages/time_record?scenario=SLEEP`, 'TimeRecord', navigation, {
  108. scenario: 'SLEEP'
  109. })
  110. break
  111. case 'METRIC':
  112. jumpPage('/pages/metric/Metric')
  113. break
  114. case 'MOVE':
  115. jumpPage('/_health/pages/move')
  116. break;
  117. }
  118. }
  119. function getTime() {
  120. if (!home) return
  121. var now = dayjs().format('HH:mm')
  122. for (var i = 0; i < home.time_messages.length; i++) {
  123. var obj = home.time_messages[i]
  124. if (now >= obj.start_time && now < obj.end_time) {
  125. setCurrentTimeData(obj)
  126. return
  127. }
  128. }
  129. setCurrentTimeData(home.time_messages[home.time_messages.length - 1])
  130. }
  131. function getBackground() {
  132. var time = record.time
  133. if (!time) return '#fff'
  134. const { background_colors } = time
  135. if (!background_colors) {
  136. return '#fff'
  137. }
  138. else if (background_colors.length == 1) {
  139. return background_colors[0]
  140. }
  141. return `linear-gradient(to bottom, ${background_colors[0]}, ${background_colors[1]})`
  142. }
  143. function itemTime(item) {
  144. if (item.event && item.event.status == 'OG') {
  145. return TimeFormatter.countdown(item.event.time.start_timestamp)
  146. }
  147. return ''
  148. }
  149. if (!loaded && showRetry) return <NoData refresh={
  150. () => {
  151. getHomeData()
  152. }
  153. } />
  154. function bgView() {
  155. if (process.env.TARO_ENV == 'weapp') {
  156. return <View className="main_bg" style={{ background: getBackground() }} />
  157. }
  158. var time = record.time
  159. if (!time) return <View />
  160. const { background_colors } = time
  161. if (!background_colors) {
  162. return <View />
  163. }
  164. else if (background_colors.length == 1) {
  165. return <View />
  166. }
  167. return <LinearGradient style={{
  168. position: 'absolute',
  169. left: 0,
  170. top: 0,
  171. width: rpxToPx(750),
  172. height: screenHeight,
  173. zIndex: 0,
  174. pointerEvents: 'none'
  175. }}
  176. colors={[background_colors[0], background_colors[1]]}
  177. start={{ x: 0, y: 0 }}
  178. end={{ x: 0, y: 1 }} pointerEvents="none" />
  179. }
  180. if (!loaded) return <View />
  181. return <View style={{ position: 'relative' }}>
  182. {
  183. bgView()
  184. }
  185. <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%' }}>
  186. <View className="h60 bold" style={{ marginLeft: rpxToPx(52), marginTop: rpxToPx(212) }}>{record.time ? record.time.greeting : ''}</View>
  187. <View style={{ display: 'flex', flexDirection: 'row' }}>
  188. <View className="h44 bold" style={{ marginLeft: rpxToPx(52), marginTop: rpxToPx(66), marginBottom: rpxToPx(32), position: 'relative', paddingBottom: rpxToPx(20) }}>{t('health.check_in')}
  189. <View className="tab_border_line" />
  190. </View>
  191. </View>
  192. <View className="operate_panel">
  193. {
  194. scenarios.map((item, index) => {
  195. return <NewButton type={NewButtonType.custom}
  196. key={index}
  197. onClick={() => {
  198. tapItem(index)
  199. }}>
  200. <View className="operate_card" >
  201. <View className="h36 bold">{item.title}</View>
  202. <View className="h50 bold">{itemTime(item)}</View>
  203. <View style={{ flex: 1 }} />
  204. <View style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', }}>
  205. <Image src={item.icon} style={{ width: rpxToPx(48), height: rpxToPx(48) }} />
  206. <Image src={item.action_icon} style={{ width: rpxToPx(36), height: rpxToPx(36) }} />
  207. </View>
  208. </View>
  209. </NewButton>
  210. })
  211. }
  212. </View>
  213. <View style={{ height: 100 }} />
  214. </View>
  215. </ScrollView>
  216. {
  217. showChoose && <ChooseActions
  218. close={() => {
  219. setShowChoose(false)
  220. }}
  221. quick={() => {
  222. jumpPage(`/_record/pages/log_record?scenario=${selScenario}&quick=1`,'LogRecord',navigation,{
  223. scenario:selScenario,
  224. quick:1
  225. })
  226. }}
  227. chooseImg={(array) => {
  228. jumpPage(`/_record/pages/log_record?scenario=${selScenario}&imgs=${JSON.stringify(array)}`,'LogRecord',navigation,{
  229. scenario:selScenario,
  230. imgs:JSON.stringify(array)
  231. })
  232. }}
  233. chooseText={() => {
  234. jumpPage(`/_record/pages/log_record?scenario=${selScenario}&only_text=1`,'LogRecord',navigation,{
  235. scenario:selScenario,
  236. only_text:1
  237. })
  238. }}
  239. />
  240. }
  241. {
  242. process.env.TARO_ENV == 'weapp' && <TabBar index={0} />
  243. }
  244. </View>
  245. }