Console.tsx 20 KB

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