Console_backup.tsx 22 KB

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