IndexConsole.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. var fastCount = currentRecord.fast.target_end_time - currentRecord.fast.target_start_time
  34. setFastDuration(fastCount)
  35. var sleepCount = currentRecord.sleep.target_end_time - currentRecord.sleep.target_start_time
  36. setSleepDuration(sleepCount)
  37. }, [props.record])
  38. function tapStartFast() {
  39. if (!user.isLogin) {
  40. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  41. return
  42. }
  43. operateType = 'startFast'
  44. showPicker()
  45. }
  46. function tapStartSleep() {
  47. if (!user.isLogin) {
  48. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth')
  49. return
  50. }
  51. if (status != 'ONGOING1') {
  52. vibrate()
  53. return;
  54. }
  55. operateType = 'startSleep'
  56. showPicker()
  57. }
  58. function tapEndSleep() {
  59. if (!user.isLogin) {
  60. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth')
  61. return
  62. }
  63. if (status != 'ONGOING2') {
  64. vibrate()
  65. return;
  66. }
  67. operateType = 'endSleep'
  68. showPicker()
  69. }
  70. function tapEndFast() {
  71. if (!user.isLogin) {
  72. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth')
  73. return
  74. }
  75. if (status == 'WAIT_FOR_START') {
  76. vibrate()
  77. return;
  78. }
  79. operateType = 'endFast'
  80. showPicker()
  81. }
  82. function layoutContent() {
  83. var limit = global.set_time - 7 * 3600 * 1000 * 24;
  84. global.limit = limit
  85. if (currentRecord.last_real_check_time) {
  86. limit = currentRecord.last_real_check_time
  87. global.limit = limit
  88. //当set_time秒数<=latest_record_time秒数时,最小限制时间戳需+1分钟
  89. if (new Date(global.set_time).getSeconds() <= new Date(currentRecord.last_real_check_time).getSeconds() && global.set_time - currentRecord.last_real_check_time > 60000) {
  90. limit = limit + 60 * 1000
  91. }
  92. }
  93. var title = getTimePickerTitle(currentRecord, t, operateType == 'endFast')
  94. var color = getColor(currentRecord, operateType == 'endFast')
  95. return <View className="modal_content">
  96. <LimitPickers ref={limitPickerRef} limit={limit} limitDay={8}
  97. themeColor={color}
  98. title={title}
  99. onCancel={hidePicker} onChange={(e) => {
  100. pickerConfirm(e)
  101. hidePicker()
  102. }} />
  103. </View>
  104. }
  105. function showPicker() {
  106. // setShowPageContainer(true)
  107. // return
  108. global.scenario = 'FAST_SLEEP'
  109. if (global.testInfotimer) {
  110. return
  111. }
  112. global.set_time = new Date().getTime()
  113. updateNodeInfo()
  114. if (!global.isDebug) {
  115. return
  116. }
  117. // global.testInfotimer = setInterval(() => {
  118. // updateNodeInfo()
  119. // }, 1000)
  120. }
  121. function updateNodeInfo() {
  122. var node = layoutContent()
  123. global.showIndexModal(true, node, null);
  124. }
  125. function hidePicker() {
  126. var node = layoutContent()
  127. global.showIndexModal(false, node, null);
  128. }
  129. function pickerConfirm(t: number) {
  130. hidePicker()
  131. var date = new Date(t)
  132. var setDate = new Date(global.set_time);
  133. date.setMilliseconds(setDate.getMilliseconds());
  134. date.setSeconds(setDate.getSeconds());
  135. t = date.getTime();
  136. switch (operateType) {
  137. case 'startFast':
  138. startFast(t, fastDuration).then(res => {
  139. global.indexPageRefresh()
  140. })
  141. break
  142. case 'startSleep':
  143. startSleep(t, sleepDuration).then(res => {
  144. global.indexPageRefresh()
  145. })
  146. break
  147. case 'endSleep':
  148. endSleep(t).then(res => {
  149. global.indexPageRefresh()
  150. })
  151. break
  152. case 'endFast':
  153. endFast(t).then(res => {
  154. global.indexPageRefresh()
  155. })
  156. break
  157. }
  158. }
  159. return <View style={{marginTop:rpxToPx(40)}}>
  160. {
  161. status == 'WAIT_FOR_START' ?
  162. <View onClick={tapStartFast} className='console_btn'>
  163. <Text style={{ fontWeight: 'bold' }}>{t('feature.track_time_duration.common.start_fast')}</Text>
  164. </View> :
  165. <View onClick={() => { vibrate() }} className='stage_btn'>
  166. <Text style={{ flex: 1 }}>睡前断食</Text>
  167. {
  168. status == 'ONGOING1' ?
  169. <Text>{TimeFormatter.countdown(currentRecord.fast.real_start_time)}</Text> :
  170. <Text>{TimeFormatter.countdown(currentRecord.fast.real_start_time, currentRecord.sleep.real_start_time)}</Text>
  171. }
  172. </View>
  173. }
  174. <View className='btn_line' />
  175. {
  176. (status == 'WAIT_FOR_START' || status == 'ONGOING1') &&
  177. <View onClick={tapStartSleep} className={status == 'ONGOING1' ? 'console_btn btn_sleep' : 'console_btn btn_sleep btn_disable'}>
  178. {
  179. status != 'ONGOING1' && <Image className='lock' src={require('@assets/images/lock.png')} />
  180. }
  181. <Text style={{ fontWeight: 'bold' }}>{t('feature.track_time_duration.common.start_sleep')}</Text>
  182. </View>
  183. }
  184. {
  185. (status != 'WAIT_FOR_START' && status != 'ONGOING1') &&
  186. <View onClick={() => { vibrate() }} className='stage_btn'>
  187. <Text style={{ flex: 1 }}>睡眠期间断食</Text>
  188. {
  189. status == 'ONGOING2' ? <Text>{TimeFormatter.countdown(currentRecord.sleep.real_start_time)}</Text> :
  190. <Text>{TimeFormatter.countdown(currentRecord.sleep.real_start_time, currentRecord.sleep.real_end_time)}</Text>
  191. }
  192. </View>
  193. }
  194. {(expand || (status != 'WAIT_FOR_START' && status != 'ONGOING1')) && <View className='btn_line' />}
  195. {
  196. (expand || (status != 'WAIT_FOR_START' && status != 'ONGOING1')) && (status == 'WAIT_FOR_START' || status == 'ONGOING1' || status == 'ONGOING2') &&
  197. <View onClick={tapEndSleep} className={status == 'ONGOING2' ? 'console_btn btn_sleep' : 'console_btn btn_sleep btn_disable'}>
  198. {
  199. status != 'ONGOING2' && <Image className='lock' src={require('@assets/images/lock.png')} />
  200. }
  201. <Text style={{ fontWeight: 'bold' }}>{t('feature.track_time_duration.common.end_sleep')}</Text>
  202. </View>
  203. }
  204. {
  205. status == 'ONGOING3' &&
  206. <View onClick={() => { vibrate() }} className='stage_btn'>
  207. <Text style={{ flex: 1 }}>起床后断食</Text>
  208. <Text>{TimeFormatter.countdown(currentRecord.sleep.real_end_time)}</Text>
  209. </View>
  210. }
  211. {(expand || (status != 'WAIT_FOR_START')) && <View className='btn_line' />}
  212. {
  213. (expand || status != 'WAIT_FOR_START') && (status == 'WAIT_FOR_START' || status == 'ONGOING1' || status == 'ONGOING2' || status == 'ONGOING3') &&
  214. <View onClick={tapEndFast} className={status == 'ONGOING3' ? 'console_btn' : 'console_btn btn_disable'}>
  215. {
  216. status == 'WAIT_FOR_START' && <Image className='lock' src={require('@assets/images/lock.png')} />
  217. }
  218. <Text style={{ fontWeight: 'bold' }}>{t('feature.track_time_duration.common.end_fast')}</Text>
  219. </View>
  220. }
  221. {
  222. (status == 'WAIT_FOR_START' || status == 'ONGOING1') &&
  223. <View>
  224. {
  225. expand ? <Text className='expand' onClick={() => { setExpand(false) }}>收起</Text> : <Text className='expand' onClick={() => { setExpand(true) }}>展开</Text>
  226. }
  227. </View>
  228. }
  229. </View>
  230. }