Console.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. import { View, Text, PickerView } from "@tarojs/components";
  2. import { useEffect, useRef, useState } from "react";
  3. import { TimeFormatter } from "@/utils/time_format";
  4. import { useDispatch, useSelector } from "react-redux";
  5. import Taro from "@tarojs/taro";
  6. import LimitPickers from '@/components/input/LimitPickers';
  7. import { endFast, endSleep, startFast, startSleep } from "../actions/TrackTimeActions";
  8. import { durationDatas, durationIndex, getColor, getTitle, textAlpha, textNextStepAlpha } from "../hooks/Console";
  9. import PickerViews from "@/components/input/PickerViews";
  10. import Modal from "@/components/layout/Modal";
  11. import Stepper from "@/components/input/Stepper";
  12. import { EndFastBtn, EndSleepBtn, StartFastBtn, StartSleepBtn } from "@/features/common/SpecBtns";
  13. import Box from "@/components/layout/Box";
  14. import './Console.scss'
  15. import { ConsoleType, changeConsoleStatus } from "@/store/console";
  16. export default function Component(props: { isNextStep?: boolean }) {
  17. const scenario = useSelector((state: any) => state.scenario);
  18. const time = useSelector((state: any) => state.time);
  19. const user = useSelector((state: any) => state.user);
  20. const common = useSelector((state: any) => state.common);
  21. const [isFast, setIsFast] = useState(true);
  22. const [fastDuration, setFastDuration] = useState<number>(0);
  23. const [sleepDuration, setSleepDuration] = useState<number>(0);
  24. const [fastPickerValue, setFastPickerValue] = useState([0, 0])
  25. const [sleepPickerValue, setSleepPickerValue] = useState([0, 0])
  26. const limitPickerRef = useRef(null)
  27. const durationPickerRef = useRef(null)
  28. const dispatch = useDispatch();
  29. useEffect(() => {
  30. getStateDetail();
  31. }, [time.fast, time.sleep, time.status, time.scenario]);
  32. function getStateDetail() {
  33. if (time.fast) {
  34. var fastCount = time.fast.target_end_time - time.fast.target_start_time
  35. setFastDuration(fastCount)
  36. setFastPickerValue(durationIndex(time.fast.target_start_time, time.fast.target_end_time, common))
  37. }
  38. if (time.sleep) {
  39. var sleepCount = time.sleep.target_end_time - time.sleep.target_start_time
  40. setSleepDuration(sleepCount)
  41. setSleepPickerValue(durationIndex(time.sleep.target_start_time, time.sleep.target_end_time, common))
  42. }
  43. if (time.scenario == 'FAST') {
  44. setIsFast(true)
  45. }
  46. else if (time.scenario == 'SLEEP') {
  47. setIsFast(false)
  48. }
  49. else {
  50. if (time.status == 'WAIT_FOR_START' || time.status == 'ONGOING3') {
  51. setIsFast(true)
  52. }
  53. else {
  54. setIsFast(false)
  55. }
  56. }
  57. }
  58. function showPicker() {
  59. if (global.testInfotimer) {
  60. return
  61. }
  62. global.set_time = new Date().getTime()
  63. updateNodeInfo()
  64. global.testInfotimer = setInterval(() => {
  65. updateNodeInfo()
  66. }, 1000)
  67. }
  68. function updateNodeInfo() {
  69. var node = (<Modal testInfo={testLayout()} children={layoutContent()} dismiss={() => {
  70. console.log('aaaaaa')
  71. global.showClockModal(false, null);
  72. hidePicker()
  73. }} confirm={() => {
  74. hidePicker()
  75. var picker = limitPickerRef.current;
  76. pickerConfirm((picker as any).getConfirmData());
  77. global.showClockModal(false, null);
  78. }} />);
  79. global.showClockModal(true, node);
  80. }
  81. function hidePicker() {
  82. global.showClockModal(false, null);
  83. if (global.testInfotimer) {
  84. clearInterval(global.testInfotimer)
  85. global.testInfotimer = null
  86. }
  87. else {
  88. console.log('global test info timer')
  89. }
  90. }
  91. function testLayout() {
  92. if (!user.test_user) {
  93. return <View />
  94. }
  95. var isStart = false;
  96. if (time.status == 'WAIT_FOR_START' || time.status == 'ONGOING1') {
  97. isStart = true
  98. }
  99. var isFast = false;
  100. switch (time.status) {
  101. case 'WAIT_FOR_START':
  102. case 'ONGOING':
  103. {
  104. isFast = (scenario.name == 'FAST' || scenario.name == 'FAST_SLEEP')
  105. }
  106. break;
  107. case 'ONGOING1':
  108. case 'ONGOING2':
  109. {
  110. isFast = false;
  111. }
  112. break
  113. case 'ONGOING3':
  114. {
  115. isFast = true;
  116. }
  117. break;
  118. }
  119. if (props.isNextStep) {
  120. isFast = true
  121. isStart = false
  122. }
  123. return <View style={{ color: '#fff', paddingTop: 30, paddingLeft: 30, display: 'flex', flexDirection: 'column' }}>
  124. <Text>check scenario:{isFast ? 'fast' : 'sleep'}</Text>
  125. <Text>check type:{isStart ? 'start' : 'end'}</Text>
  126. <Text style={{ marginTop: 30 }}>picker restriction</Text>
  127. <Text style={{ marginLeft: 50 }}>min:{TimeFormatter.formatTimestamp(global.limit)}</Text>
  128. <Text style={{ marginLeft: 50 }}>pick:{TimeFormatter.formatTimestamp(global.picker_time)}</Text>
  129. <Text style={{ marginLeft: 50 }}>max{TimeFormatter.formatTimestamp(global.set_time)}</Text>
  130. <Text style={{ marginTop: 30 }}>now:{TimeFormatter.formatTimestamp(new Date().getTime())}</Text>
  131. <Text>elapsed:{TimeFormatter.calculateTimeDifference(global.picker_time, new Date().getTime())}</Text>
  132. <Text style={{ marginTop: 30 }}>real start:{isStart ? '-' : TimeFormatter.formatTimestamp(isFast ? time.fast.real_start_time : time.sleep.real_start_time)}</Text>
  133. <Text>real duration:{isStart ? '-' : TimeFormatter.calculateTimeDifference(isFast ? time.fast.real_start_time : time.sleep.real_start_time, new Date().getTime())}</Text>
  134. </View>
  135. }
  136. function layoutContent() {
  137. var limit = global.set_time - 7 * 3600 * 1000 * 24;
  138. global.limit = limit
  139. if (time.last_real_check_time) {
  140. limit = time.last_real_check_time
  141. global.limit = limit
  142. //当set_time秒数<=latest_record_time秒数时,最小限制时间戳需+1分钟
  143. if (new Date(global.set_time).getSeconds() <= new Date(time.last_real_check_time).getSeconds()) {
  144. limit = limit + 60 * 1000
  145. }
  146. }
  147. var title = getTitle(time)
  148. var color = getColor(time)
  149. if (props.isNextStep) {
  150. title = '结束断食'
  151. color = '#AAFF00'
  152. }
  153. return <LimitPickers ref={limitPickerRef} limit={limit} limitDay={8}
  154. themeColor={color}
  155. title={title}
  156. onCancel={hidePicker} onChange={(e) => {
  157. pickerConfirm(e)
  158. hidePicker()
  159. }} />
  160. }
  161. function pickerConfirm(t: number) {
  162. var date = new Date(t)
  163. var setDate = new Date(global.set_time);
  164. date.setMilliseconds(setDate.getMilliseconds());
  165. date.setSeconds(setDate.getSeconds());
  166. t = date.getTime();
  167. if (props.isNextStep) {
  168. endFast(t)
  169. return;
  170. }
  171. if (isFast) {
  172. if (time.status == 'WAIT_FOR_START') {
  173. startFast(t, fastDuration).then(res=>{
  174. if ((res as any).current_record.scenario=='FAST_SLEEP' &&
  175. (res as any).current_record.status=='ONGOING1'){
  176. global.consoleType = 'going'
  177. dispatch(changeConsoleStatus({status:ConsoleType.going}));
  178. }
  179. });
  180. }
  181. else {
  182. endFast(t)
  183. }
  184. }
  185. else {
  186. if (time.status == 'WAIT_FOR_START' || time.status == 'ONGOING1') {
  187. startSleep(t, sleepDuration);
  188. }
  189. else {
  190. endSleep(t)
  191. }
  192. }
  193. }
  194. function targetView() {
  195. if (props.isNextStep && (time.status == 'ONGOING1' || time.status == 'ONGOING2')) {
  196. var opacity = textNextStepAlpha(time)
  197. if (opacity == 1) {
  198. return <View className="target_view" style={{ backgroundColor: '#FA51511A' }}>
  199. <Text className="target_text" style={{ color: '#FA5151' }}>超出目标结束</Text>
  200. </View>
  201. }
  202. return <View className="target_view" style={{ backgroundColor: '#AAFF001A', opacity: opacity }}>
  203. <Text className="target_text" style={{ color: '#AAFF00' }}>距离目标结束</Text>
  204. </View>
  205. }
  206. if (time.status == 'ONGOING' || time.status == 'ONGOING2' || time.status == 'ONGOING3') {
  207. var opacity = textAlpha(time)
  208. if (opacity == 1) {
  209. return <View className="target_view" style={{ backgroundColor: '#FA51511A' }}>
  210. <Text className="target_text" style={{ color: '#FA5151' }}>超出目标结束</Text>
  211. </View>
  212. }
  213. return <View className="target_view" style={{ backgroundColor: isFast ? '#AAFF001A' : '#00FFFF1A', opacity: opacity }}>
  214. <Text className="target_text" style={{ color: isFast ? '#AAFF00' : '#00FFFF' }}>距离目标结束</Text>
  215. </View>
  216. }
  217. return <View />
  218. }
  219. function mixedBtns() {
  220. return <View className="btn_bg">
  221. {
  222. (time.status == 'WAIT_FOR_START' || time.status == 'DONE') &&
  223. <StartFastBtn onClick={showPicker} />
  224. }
  225. {
  226. (time.status == 'ONGOING' || time.status == 'ONGOING3') &&
  227. <EndFastBtn onClick={showPicker} lowLight={time.fast.target_end_time > new Date().getTime()} />
  228. }
  229. {
  230. time.status == 'ONGOING1' && <StartSleepBtn onClick={showPicker} />
  231. }
  232. {
  233. time.status == 'ONGOING2' && <EndSleepBtn onClick={showPicker} lowLight={time.sleep.target_end_time > new Date().getTime()} />
  234. }
  235. </View>
  236. }
  237. function nextStepBtns() {
  238. return <View className="btn_bg">
  239. {
  240. (time.status == 'WAIT_FOR_START') &&
  241. <StartSleepBtn onClick={showPicker} />
  242. }
  243. {
  244. time.status == 'ONGOING1' && <EndFastBtn onClick={showPicker} lowLight={time.fast.target_end_time > new Date().getTime()} />
  245. }
  246. {
  247. time.status == 'ONGOING2' && <EndFastBtn onClick={showPicker} lowLight={time.fast.target_end_time > new Date().getTime()} />
  248. }
  249. </View>
  250. }
  251. function fastBtns() {
  252. return <View className="btn_bg">
  253. {
  254. time.status == 'ONGOING' ? <EndFastBtn onClick={showPicker} lowLight={textAlpha(time) != 1} /> : <StartFastBtn onClick={showPicker} />
  255. }
  256. </View>
  257. }
  258. function sleepBtns() {
  259. return <View className="btn_bg">
  260. {
  261. time.status == 'ONGOING' ? <EndSleepBtn onClick={showPicker} lowLight={textAlpha(time) != 1} /> : <StartSleepBtn onClick={showPicker} />
  262. }
  263. </View>
  264. }
  265. function durationChange(e) {
  266. var count = (e[0] + common.duration.min) * 60 + e[1] * common.duration.step
  267. isFast ? setFastDuration(count * 60000) : setSleepDuration(count * 60000);
  268. isFast ? setFastPickerValue(e) : setSleepPickerValue(e)
  269. global.showClockModal2(false, null)
  270. }
  271. function login() {
  272. Taro.navigateTo({
  273. url: '/pages/account/ChooseAuth'
  274. })
  275. }
  276. function durationFormate() {
  277. var t = isFast ? fastDuration / 60000 : sleepDuration / 60000
  278. if (props.isNextStep) t = sleepDuration / 60000
  279. var hour = Math.floor(t / 60)
  280. var minute = Math.floor(t % 60)
  281. return `${hour > 0 ? hour + '小时' : ''}${minute > 0 ? minute + '分钟' : ''}`
  282. }
  283. function showDurationPicker() {
  284. var node = <Modal children={durationPickerContent()} dismiss={() => global.showClockModal2(false, null)} confirm={() => {
  285. var picker = durationPickerRef.current;
  286. durationChange((picker as any).getConfirmData());
  287. global.showClockModal2(false, null);
  288. }} />
  289. global.showClockModal2(true, node);
  290. }
  291. function durationPickerContent() {
  292. var color = getColor(time)
  293. var title = getTitle(time)
  294. return <View style={{ color: '#fff', backgroundColor: 'transparent' }}>
  295. <PickerViews ref={durationPickerRef}
  296. onChange={durationChange}
  297. items={durationDatas(common)}
  298. value={isFast ? fastPickerValue : sleepPickerValue}
  299. themeColor={color}
  300. title={title}
  301. showBtns={true} onCancel={() => { global.showClockModal2(false, null); }} />
  302. </View>
  303. }
  304. function minus() {
  305. if (isFast) {
  306. var count = fastDuration - 5 * 60 * 1000
  307. setFastDuration(count)
  308. var hour = count / 60000 / 60
  309. var minute = count / 60000 % 60
  310. setFastPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common))
  311. }
  312. else {
  313. var count = sleepDuration - 5 * 60 * 1000
  314. setSleepDuration(count)
  315. var hour = count / 60000 / 60
  316. var minute = count / 60000 % 60
  317. setSleepPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common))
  318. }
  319. }
  320. function plus() {
  321. if (isFast) {
  322. var count = fastDuration + 5 * 60 * 1000
  323. setFastDuration(count)
  324. var hour = count / 60000 / 60
  325. var minute = count / 60000 % 60
  326. setFastPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common))
  327. }
  328. else {
  329. var count = sleepDuration + 5 * 60 * 1000
  330. setSleepDuration(count)
  331. var hour = count / 60000 / 60
  332. var minute = count / 60000 % 60
  333. setSleepPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common))
  334. }
  335. }
  336. function disableMinus() {
  337. if (isFast) {
  338. if (fastDuration <= 60 * 60 * 1000) {
  339. return true;
  340. }
  341. }
  342. else {
  343. if (sleepDuration <= 60 * 60 * 1000) {
  344. return true;
  345. }
  346. }
  347. return false;
  348. }
  349. function disablePlus() {
  350. if (isFast) {
  351. if (fastDuration >= 23 * 60 * 60 * 1000) {
  352. return true;
  353. }
  354. }
  355. else {
  356. if (sleepDuration >= 23 * 60 * 60 * 1000) {
  357. return true;
  358. }
  359. }
  360. return false;
  361. }
  362. var textColor = getColor(time)
  363. if (time.status != 'ONGOING1' && time.status != 'WAIT_FOR_START') {
  364. if (textAlpha(time) == 1) {
  365. textColor = '#FA5151';
  366. }
  367. }
  368. if (!user.isLogin) {
  369. return <View style={{ display: 'flex', flexDirection: 'column', width: '100%', alignItems: 'center' }}>
  370. <Text>16:00</Text>
  371. <Text style={{ color: '#AAFF00' }} onClick={login}>Start Fast</Text>
  372. </View>
  373. }
  374. if (props.isNextStep) {
  375. return <Box >
  376. <View style={{
  377. display: 'flex', flexDirection: 'column', alignItems: 'center', flexShrink: 0,
  378. opacity: time.status == 'WAIT_FOR_START' ? 0.4 : 1,
  379. pointerEvents: time.status == 'WAIT_FOR_START' ? 'none' : 'all'
  380. }}>
  381. {
  382. (time.status == 'WAIT_FOR_START') && <Stepper child={
  383. <Text className="stepper_text" style={{ color: '#00FFFF', opacity: textAlpha(time) }} onClick={showDurationPicker}>{durationFormate()}</Text>
  384. } minus={minus} plus={plus} disableMinus={disableMinus()} disablePlus={disablePlus()} />
  385. }
  386. {
  387. time.status == 'ONGOING1' && <View>
  388. <Text className="counter_text" style={{ color: textNextStepAlpha(time) == 1 ? '#FA5151' : '#AAFF00', opacity: textNextStepAlpha(time) }}>{TimeFormatter.countdown(time.fast.target_end_time)}</Text>
  389. </View>
  390. }
  391. {
  392. time.status == 'ONGOING2' && <View>
  393. <Text className="counter_text" style={{ color: textNextStepAlpha(time) == 1 ? '#FA5151' : '#AAFF00', opacity: textNextStepAlpha(time) }}>{TimeFormatter.countdown(time.fast.target_end_time)}</Text>
  394. </View>
  395. }
  396. {
  397. targetView()
  398. }
  399. <View>
  400. {
  401. nextStepBtns()
  402. }
  403. </View>
  404. </View>
  405. </Box>
  406. }
  407. return (
  408. <Box >
  409. <View style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', flexShrink: 0 }}>
  410. {
  411. time.status == 'ONGOING' && <View>
  412. <Text className="counter_text" style={{ color: textColor, opacity: textAlpha(time) }}>{time.scenario == 'FAST' ?
  413. TimeFormatter.countdown(time.fast.target_end_time) :
  414. TimeFormatter.countdown(time.sleep.target_end_time)}</Text>
  415. </View>
  416. }
  417. {
  418. (time.status == 'ONGOING1' || time.status == 'WAIT_FOR_START') && <Stepper child={
  419. <Text className="stepper_text" style={{ color: textColor, opacity: textAlpha(time) }} onClick={showDurationPicker}>{durationFormate()}</Text>
  420. } minus={minus} plus={plus} disableMinus={disableMinus()} disablePlus={disablePlus()} />
  421. }
  422. {
  423. time.status == 'ONGOING2' && <View>
  424. <Text className="counter_text" style={{ color: textColor, opacity: textAlpha(time) }}>{TimeFormatter.countdown(time.sleep.target_end_time)}</Text>
  425. </View>
  426. }
  427. {
  428. time.status == 'ONGOING3' && <View>
  429. <Text className="counter_text" style={{ color: textColor, opacity: textAlpha(time) }}>{TimeFormatter.countdown(time.fast.target_end_time)}</Text>
  430. </View>
  431. }
  432. {
  433. targetView()
  434. }
  435. <View>
  436. {
  437. time.scenario === 'FAST_SLEEP' && mixedBtns()
  438. }
  439. {
  440. time.scenario === 'FAST' && fastBtns()
  441. }
  442. {
  443. time.scenario === 'SLEEP' && sleepBtns()
  444. }
  445. </View>
  446. </View>
  447. </Box>
  448. )
  449. }