Console3.tsx 18 KB

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