IndexConsole.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import { View, Text, Image } from '@tarojs/components'
  2. import './IndexConsole.scss'
  3. import { useTranslation } from 'react-i18next'
  4. import { useSelector } from 'react-redux';
  5. import { endFast, endSleep, startFast, startSleep } from "../actions/TrackTimeActions";
  6. import { jumpPage } from '../hooks/Common';
  7. import { useEffect, useRef, useState } from 'react';
  8. import LimitPickers from '@/components/input/LimitPickers';
  9. import { getColor, getTimePickerTitle } from '../hooks/Console';
  10. import { rpxToPx, vibrate } from '@/utils/tools';
  11. import { TimeFormatter } from '@/utils/time_format';
  12. let useNavigation;
  13. if (process.env.TARO_ENV == 'rn') {
  14. useNavigation = require("@react-navigation/native").useNavigation
  15. }
  16. let operateType = ''
  17. export default function IndexConsole(props: { record: any }) {
  18. const user = useSelector((state: any) => state.user);
  19. const { status } = props.record.current_record;
  20. const currentRecord = props.record.current_record;
  21. const { t } = useTranslation()
  22. const [fastDuration, setFastDuration] = useState<number>(0);
  23. const [sleepDuration, setSleepDuration] = useState<number>(0);
  24. const [expand, setExpand] = useState(false);
  25. // const [fastPickerValue, setFastPickerValue] = useState([0, 0])
  26. // const [sleepPickerValue, setSleepPickerValue] = useState([0, 0])
  27. const limitPickerRef = useRef(null)
  28. let navigation;
  29. if (useNavigation) {
  30. navigation = useNavigation()
  31. }
  32. useEffect(() => {
  33. if (currentRecord.fast) {
  34. var fastCount = currentRecord.fast.target_end_time - currentRecord.fast.target_start_time
  35. setFastDuration(fastCount)
  36. }
  37. if (currentRecord.sleep) {
  38. var sleepCount = currentRecord.sleep.target_end_time - currentRecord.sleep.target_start_time
  39. setSleepDuration(sleepCount)
  40. }
  41. }, [props.record])
  42. function tapStartFast() {
  43. if (!user.isLogin) {
  44. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  45. return
  46. }
  47. operateType = 'startFast'
  48. showPicker()
  49. }
  50. function tapStartSleep() {
  51. if (!user.isLogin) {
  52. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth')
  53. return
  54. }
  55. if (status != 'ONGOING1' && props.record.scenario.name == 'FAST_SLEEP') {
  56. vibrate()
  57. return;
  58. }
  59. operateType = 'startSleep'
  60. showPicker()
  61. }
  62. function tapEndSleep() {
  63. if (!user.isLogin) {
  64. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth')
  65. return
  66. }
  67. if (status != 'ONGOING2' && status!='ONGOING') {
  68. vibrate()
  69. return;
  70. }
  71. operateType = 'endSleep'
  72. showPicker()
  73. }
  74. function tapEndFast() {
  75. if (!user.isLogin) {
  76. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth')
  77. return
  78. }
  79. if (status == 'WAIT_FOR_START') {
  80. vibrate()
  81. return;
  82. }
  83. operateType = 'endFast'
  84. showPicker()
  85. }
  86. function layoutContent() {
  87. var limit = global.set_time - 7 * 3600 * 1000 * 24;
  88. global.limit = limit
  89. if (currentRecord.last_real_check_time) {
  90. limit = currentRecord.last_real_check_time
  91. global.limit = limit
  92. //当set_time秒数<=latest_record_time秒数时,最小限制时间戳需+1分钟
  93. if (new Date(global.set_time).getSeconds() <= new Date(currentRecord.last_real_check_time).getSeconds() && global.set_time - currentRecord.last_real_check_time > 60000) {
  94. limit = limit + 60 * 1000
  95. }
  96. }
  97. var title = getTimePickerTitle(currentRecord, t, operateType == 'endFast')
  98. var color = getColor(currentRecord, operateType == 'endFast')
  99. return <View className="modal_content">
  100. <LimitPickers ref={limitPickerRef} limit={limit} limitDay={8}
  101. themeColor={color}
  102. title={title}
  103. onCancel={hidePicker} onChange={(e) => {
  104. pickerConfirm(e)
  105. hidePicker()
  106. }} />
  107. </View>
  108. }
  109. function showPicker() {
  110. // global.scenario = 'FAST_SLEEP'
  111. if (global.testInfotimer) {
  112. return
  113. }
  114. global.set_time = new Date().getTime()
  115. updateNodeInfo()
  116. if (!global.isDebug) {
  117. return
  118. }
  119. // global.testInfotimer = setInterval(() => {
  120. // updateNodeInfo()
  121. // }, 1000)
  122. }
  123. function updateNodeInfo() {
  124. var node = layoutContent()
  125. global.showIndexModal(true, node, null);
  126. }
  127. function hidePicker() {
  128. var node = layoutContent()
  129. global.showIndexModal(false, node, null);
  130. }
  131. function pickerConfirm(t: number) {
  132. hidePicker()
  133. var date = new Date(t)
  134. var setDate = new Date(global.set_time);
  135. date.setMilliseconds(setDate.getMilliseconds());
  136. date.setSeconds(setDate.getSeconds());
  137. t = date.getTime();
  138. switch (operateType) {
  139. case 'startFast':
  140. startFast(t, fastDuration).then(res => {
  141. global.indexPageRefresh()
  142. })
  143. break
  144. case 'startSleep':
  145. startSleep(t, sleepDuration).then(res => {
  146. global.indexPageRefresh()
  147. })
  148. break
  149. case 'endSleep':
  150. endSleep(t).then(res => {
  151. global.indexPageRefresh()
  152. })
  153. break
  154. case 'endFast':
  155. endFast(t).then(res => {
  156. global.indexPageRefresh()
  157. })
  158. break
  159. }
  160. }
  161. if (props.record.scenario.name == 'FAST') {
  162. return <View style={{ marginTop: rpxToPx(40) }}>
  163. {
  164. status == 'WAIT_FOR_START' &&
  165. <View onClick={tapStartFast} className='console_btn'>
  166. <Text style={{ fontWeight: 'bold' }}>{t('feature.track_time_duration.common.start_fast')}</Text>
  167. </View>
  168. }
  169. {
  170. status == 'WAIT_FOR_START' && <View className='btn_line' />
  171. }
  172. <View onClick={tapEndFast} className={status == 'ONGOING' ? 'console_btn' : 'console_btn btn_disable'}>
  173. <Text style={{ fontWeight: 'bold' }}>{t('feature.track_time_duration.common.end_fast')}</Text>
  174. </View>
  175. </View>
  176. }
  177. else if (props.record.scenario.name == 'SLEEP') {
  178. return <View style={{ marginTop: rpxToPx(40) }}>
  179. {
  180. status == 'WAIT_FOR_START' &&
  181. <View onClick={tapStartSleep} className='console_btn btn_sleep'>
  182. <Text style={{ fontWeight: 'bold' }}>{t('feature.track_time_duration.common.start_sleep')}</Text>
  183. </View>
  184. }
  185. {
  186. status == 'WAIT_FOR_START' && <View className='btn_line' />
  187. }
  188. <View onClick={tapEndSleep} className={status == 'ONGOING' ? 'console_btn btn_sleep' : 'console_btn btn_sleep btn_disable'}>
  189. <Text style={{ fontWeight: 'bold' }}>{t('feature.track_time_duration.common.end_sleep')}</Text>
  190. </View>
  191. </View>
  192. }
  193. return <View style={{ marginTop: rpxToPx(40) }}>
  194. {
  195. status == 'WAIT_FOR_START' ?
  196. <View onClick={tapStartFast} className='console_btn'>
  197. <Text style={{ fontWeight: 'bold' }}>{t('feature.track_time_duration.common.start_fast')}</Text>
  198. </View> :
  199. <View onClick={() => { vibrate() }} className='stage_btn'>
  200. <Text style={{ flex: 1 }}>睡前断食</Text>
  201. {
  202. status == 'ONGOING1' ?
  203. <Text>{TimeFormatter.countdown(currentRecord.fast.real_start_time)}</Text> :
  204. <Text>{TimeFormatter.countdown(currentRecord.fast.real_start_time, currentRecord.sleep.real_start_time)}</Text>
  205. }
  206. </View>
  207. }
  208. <View className='btn_line' />
  209. {
  210. (status == 'WAIT_FOR_START' || status == 'ONGOING1') &&
  211. <View onClick={tapStartSleep} className={status == 'ONGOING1' ? 'console_btn btn_sleep' : 'console_btn btn_sleep btn_disable'}>
  212. {
  213. status != 'ONGOING1' && <Image className='lock' src={require('@assets/images/lock.png')} />
  214. }
  215. <Text style={{ fontWeight: 'bold' }}>{t('feature.track_time_duration.common.start_sleep')}</Text>
  216. </View>
  217. }
  218. {
  219. (status != 'WAIT_FOR_START' && status != 'ONGOING1') &&
  220. <View onClick={() => { vibrate() }} className='stage_btn'>
  221. <Text style={{ flex: 1 }}>睡眠期间断食</Text>
  222. {
  223. status == 'ONGOING2' ? <Text>{TimeFormatter.countdown(currentRecord.sleep.real_start_time)}</Text> :
  224. <Text>{TimeFormatter.countdown(currentRecord.sleep.real_start_time, currentRecord.sleep.real_end_time)}</Text>
  225. }
  226. </View>
  227. }
  228. {(expand || (status != 'WAIT_FOR_START' && status != 'ONGOING1')) && <View className='btn_line' />}
  229. {
  230. (expand || (status != 'WAIT_FOR_START' && status != 'ONGOING1')) && (status == 'WAIT_FOR_START' || status == 'ONGOING1' || status == 'ONGOING2') &&
  231. <View onClick={tapEndSleep} className={status == 'ONGOING2' ? 'console_btn btn_sleep' : 'console_btn btn_sleep btn_disable'}>
  232. {
  233. status != 'ONGOING2' && <Image className='lock' src={require('@assets/images/lock.png')} />
  234. }
  235. <Text style={{ fontWeight: 'bold' }}>{t('feature.track_time_duration.common.end_sleep')}</Text>
  236. </View>
  237. }
  238. {
  239. status == 'ONGOING3' &&
  240. <View onClick={() => { vibrate() }} className='stage_btn'>
  241. <Text style={{ flex: 1 }}>起床后断食</Text>
  242. <Text>{TimeFormatter.countdown(currentRecord.sleep.real_end_time)}</Text>
  243. </View>
  244. }
  245. {(expand || (status != 'WAIT_FOR_START')) && <View className='btn_line' />}
  246. {
  247. (expand || status != 'WAIT_FOR_START') && (status == 'WAIT_FOR_START' || status == 'ONGOING1' || status == 'ONGOING2' || status == 'ONGOING3') &&
  248. <View onClick={tapEndFast} className={status == 'ONGOING3' ? 'console_btn' : 'console_btn btn_disable'}>
  249. {
  250. status == 'WAIT_FOR_START' && <Image className='lock' src={require('@assets/images/lock.png')} />
  251. }
  252. <Text style={{ fontWeight: 'bold' }}>{t('feature.track_time_duration.common.end_fast')}</Text>
  253. </View>
  254. }
  255. {
  256. (status == 'WAIT_FOR_START' || status == 'ONGOING1') &&
  257. <View>
  258. {
  259. expand ? <Text className='expand' onClick={() => { setExpand(false) }}>收起</Text> : <Text className='expand' onClick={() => { setExpand(true) }}>展开</Text>
  260. }
  261. </View>
  262. }
  263. </View>
  264. }