Console.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import { recordCheck } from "@/services/trackTimeDuration";
  2. import { View, Text, PickerView } from "@tarojs/components";
  3. import trackTimeService, { machine } from "@/store/trackTimeMachine"
  4. import { useEffect, useState } from "react";
  5. import { TimeFormatter } from "@/utils/time_format";
  6. import TimePickers from '@/components/TimePickers'
  7. import { useSelector } from "react-redux";
  8. import Taro from "@tarojs/taro";
  9. import LimitPickers from '@/components/LimitPickers';
  10. import { endFast, endSleep, startFast, startSleep } from "../actions/TrackTimeActions";
  11. import { durationDatas, durationIndex, pickerDurations } from "../hooks/Console";
  12. import PickerViews from "@/components/PickerViews";
  13. import Modal from "@/components/Modal";
  14. import Stepper from "@/components/Stepper";
  15. export default function Component() {
  16. const [key, setKey] = useState('');
  17. const [value, setValue] = useState('');
  18. const user = useSelector((state: any) => state.user);
  19. const common = useSelector((state: any) => state.common);
  20. const [isFast, setIsFast] = useState(true);
  21. const [fastValues, setFastValues] = useState<number[]>([0, 0]);
  22. const [sleepValues, setSleepValues] = useState<number[]>([0, 0]);
  23. const [fastDuration, setFastDuration] = useState<number>(0);
  24. const [sleepDuration, setSleepDuration] = useState<number>(0);
  25. const [fastStr, setFastStr] = useState('00:00');
  26. const [sleepStr, setSleepStr] = useState('00:00');
  27. const [isOpen, setIsOpen] = useState(false);
  28. const [showModal, setShowModal] = useState(false);
  29. const [fastPickerValue, setFastPickerValue] = useState([0, 0])
  30. const [sleepPickerValue, setSleepPickerValue] = useState([0, 0])
  31. const [isOpenDurationPicker, setIsOpenDurationPicker] = useState(false)
  32. // const [pickerValue, setPickerValue] = useState([0,0])
  33. // const pickerDurations = pickerDurations();
  34. // console.log(pickerDurations())
  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 fastTime = TimeFormatter.formateHourMinute(current_record.fast.target_start_time,
  84. current_record.fast.target_end_time);
  85. setFastValues(fastTime.split(':').map(x => parseInt(x)));
  86. setFastStr(fastTime);
  87. var fastCount = current_record.fast.target_end_time - current_record.fast.target_start_time
  88. setFastDuration(fastCount)
  89. setFastPickerValue(durationIndex(current_record.fast.target_start_time, current_record.fast.target_end_time, common))
  90. // setFastPickerValue([fastCount / 60000 / 5 - 12])
  91. }
  92. if (current_record.sleep) {
  93. var sleepTime = TimeFormatter.formateHourMinute(current_record.sleep.target_start_time,
  94. current_record.sleep.target_end_time);
  95. setSleepValues(sleepTime.split(':').map(x => parseInt(x)));
  96. setSleepStr(sleepTime);
  97. var sleepCount = current_record.sleep.target_end_time - current_record.sleep.target_start_time
  98. setSleepDuration(sleepCount)
  99. setSleepPickerValue(durationIndex(current_record.sleep.target_start_time, current_record.sleep.target_end_time, common))
  100. // setSleepPickerValue([sleepCount / 60000 / 5 - 12])
  101. }
  102. }
  103. }
  104. function showPicker() {
  105. setShowModal(true)
  106. setIsOpen(true)
  107. global.set_time = new Date().getTime()
  108. console.log(global.set_time)
  109. }
  110. function hidePicker() {
  111. setIsOpen(false)
  112. setTimeout(() => {
  113. setShowModal(false)
  114. }, 1000)
  115. }
  116. function layoutContent() {
  117. var limit = new Date().getTime() - 7 * 3600 * 1000 * 24;
  118. var current_record = machine.context.checkData ? (machine.context.checkData as any).current_record : null;
  119. if (current_record && current_record.last_real_check_time)
  120. limit = current_record.last_real_check_time
  121. return <View style={{ backgroundColor: '#fff' }}>
  122. <LimitPickers limit={limit} limitDay={7} onCancel={hidePicker} onChange={(e) => {
  123. console.log(new Date(e))
  124. pickerConfirm(e)
  125. hidePicker()
  126. }} />
  127. </View>
  128. }
  129. function pickerConfirm(t: number) {
  130. debugger
  131. console.log(t)
  132. var date = new Date(t)
  133. var setDate = new Date(global.set_time);
  134. date.setMilliseconds(setDate.getMilliseconds());
  135. date.setSeconds(setDate.getSeconds());
  136. t = date.getTime();
  137. console.log(t);
  138. if (isFast) {
  139. if (value == 'WAIT_FOR_START') {
  140. // const duration = fastValues[0] * 3600 * 1000 + fastValues[1] * 60 * 1000
  141. startFast(t, fastDuration);
  142. }
  143. else {
  144. endFast(t)
  145. }
  146. }
  147. else {
  148. if (value == 'WAIT_FOR_START' || value == 'ONGOING1') {
  149. // const duration = sleepValues[0] * 3600 * 1000 + sleepValues[1] * 60 * 1000
  150. startSleep(t, sleepDuration);
  151. }
  152. else {
  153. endSleep(t)
  154. }
  155. }
  156. }
  157. function mixedBtns() {
  158. return <View>
  159. {
  160. (value == 'WAIT_FOR_START' || value == 'DONE') &&
  161. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>Start Fast</Text>
  162. }
  163. {
  164. (value == 'ONGOING'/* ||value == 'ONGOING1' || value == 'ONGOING2'*/ || value == 'ONGOING3') &&
  165. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>End Fast</Text>
  166. }
  167. {
  168. value == 'ONGOING1' && <Text style={{ color: '#00ffff' }} onClick={showPicker}>Start Sleep</Text>
  169. }
  170. {
  171. value == 'ONGOING2' && <Text style={{ color: '#00ffff' }} onClick={showPicker}>End Sleep</Text>
  172. }
  173. </View>
  174. }
  175. function fastBtns() {
  176. return <View>
  177. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>{value == 'ONGOING' ? 'End Fast' : 'Start Fast'}</Text>
  178. </View>
  179. }
  180. function sleepBtns() {
  181. return <View>
  182. {
  183. value == 'ONGOING' ? <Text style={{ color: '#00ffff' }} onClick={showPicker}>End Sleep</Text> :
  184. <Text style={{ color: '#00ffff' }} onClick={showPicker}>Start Sleep</Text>
  185. }
  186. </View>
  187. }
  188. const handlePickerChange = (e: string) => {
  189. var [hour, minute] = e.split(':').map(x => parseInt(x))
  190. isFast ? setFastValues([hour, minute]) : setSleepValues([hour, minute]);
  191. };
  192. function durationChange(e) {
  193. var count = (e[0] + common.duration.min) * 60 + e[1] * common.duration.step
  194. isFast ? setFastDuration(count * 60000) : setSleepDuration(count * 60000);
  195. isFast ? setFastPickerValue(e) : setSleepPickerValue(e)
  196. setIsOpenDurationPicker(false)
  197. }
  198. function login() {
  199. Taro.navigateTo({
  200. url: '/pages/ChooseAuth'
  201. })
  202. }
  203. function durationFormate() {
  204. if (isFast) {
  205. var t = fastDuration / 60000
  206. var hour = Math.floor(t / 60)
  207. var minute = Math.floor(t % 60)
  208. return `${hour > 0 ? hour + '小时' : ''}${minute > 0 ? minute + '分钟' : ''}`
  209. }
  210. else {
  211. var t = sleepDuration / 60000
  212. var hour = Math.floor(t / 60)
  213. var minute = Math.floor(t % 60)
  214. return `${hour > 0 ? hour + '小时' : ''}${minute > 0 ? minute + '分钟' : ''}`
  215. }
  216. }
  217. function showDurationPicker() {
  218. setIsOpenDurationPicker(true)
  219. }
  220. if (!user.isLogin) {
  221. return <View style={{ display: 'flex', flexDirection: 'column', width: '100%', alignItems: 'center' }}>
  222. <Text>16:00</Text>
  223. {/* <PickerViews onChange={() => { }} items={[pickerDurations()]} value={[12 * 15]} /> */}
  224. {/* <TimePickers time={isFast ? fastStr : sleepStr} content="" change={handlePickerChange} isPickerView={true} /> */}
  225. <Text style={{ color: '#AAFF00' }} onClick={login}>Start Fast</Text>
  226. </View>
  227. }
  228. function durationPickerContent() {
  229. return <View style={{ color: '#000', backgroundColor: '#fff' }}>
  230. <PickerViews onChange={durationChange} items={durationDatas(common)} value={isFast ? fastPickerValue : sleepPickerValue} height={200} showBtns={true} onCancel={() => { setIsOpenDurationPicker(false) }} />
  231. </View>
  232. }
  233. function minus() {
  234. if (isFast) {
  235. var count = fastDuration - 5 * 60 * 1000
  236. setFastDuration(count)
  237. var hour = count / 60000 / 60
  238. var minute = count / 60000 % 60
  239. setFastPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common))
  240. }
  241. else {
  242. var count = sleepDuration - 5 * 60 * 1000
  243. setSleepDuration(count)
  244. var hour = count / 60000 / 60
  245. var minute = count / 60000 % 60
  246. setSleepPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common))
  247. }
  248. }
  249. function plus() {
  250. if (isFast) {
  251. var count = fastDuration + 5 * 60 * 1000
  252. setFastDuration(count)
  253. var hour = count / 60000 / 60
  254. var minute = count / 60000 % 60
  255. setFastPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common))
  256. }
  257. else {
  258. var count = sleepDuration + 5 * 60 * 1000
  259. setSleepDuration(count)
  260. var hour = count / 60000 / 60
  261. var minute = count / 60000 % 60
  262. setSleepPickerValue(durationIndex('00:00', `${hour > 10 ? hour : '0' + hour}:${minute > 10 ? minute : '0' + minute}`, common))
  263. }
  264. }
  265. function disableMinus() {
  266. if (isFast) {
  267. if (fastDuration <= 60 * 60 * 1000) {
  268. return true;
  269. }
  270. }
  271. else {
  272. if (sleepDuration <= 60 * 60 * 1000) {
  273. return true;
  274. }
  275. }
  276. return false;
  277. }
  278. function disablePlus() {
  279. if (isFast) {
  280. if (fastDuration >= 23 * 60 * 60 * 1000) {
  281. return true;
  282. }
  283. }
  284. else {
  285. if (sleepDuration >= 23 * 60 * 60 * 1000) {
  286. return true;
  287. }
  288. }
  289. return false;
  290. }
  291. return (
  292. <View style={{ display: 'flex', flexDirection: 'column', width: '100%', alignItems: 'center' }}>
  293. {
  294. machine.context.checkData && (machine.context.checkData as any).current_record.last_real_check_time &&
  295. <Text style={{ width: '100%', textAlign: 'center', color: 'red' }}>
  296. {TimeFormatter.formateTime((machine.context.checkData as any).current_record.last_real_check_time)}
  297. </Text>
  298. }
  299. {
  300. value == 'ONGOING' && <View>
  301. <Text>{(machine.context.checkData as any).current_record.scenario == 'FAST' ?
  302. TimeFormatter.countdown((machine.context.checkData as any).current_record.fast.target_end_time) :
  303. TimeFormatter.countdown((machine.context.checkData as any).current_record.sleep.target_end_time)}</Text>
  304. </View>
  305. }
  306. {
  307. (value == 'ONGOING1' || value == 'WAIT_FOR_START') && <Stepper style={{ marginBottom: 10 }} child={
  308. <Text style={{ width: 150, textAlign: 'center' }} onClick={showDurationPicker}>{durationFormate()}</Text>
  309. } minus={minus} plus={plus} disableMinus={disableMinus()} disablePlus={disablePlus()} />
  310. // <Text style={{marginBottom:10}} onClick={showDurationPicker}>时长:{durationFormate()}</Text>
  311. }
  312. {
  313. machine.context.checkData && value == 'ONGOING2' && <View>
  314. <Text>{TimeFormatter.countdown((machine.context.checkData as any).current_record.sleep.target_end_time)}</Text>
  315. </View>
  316. }
  317. {
  318. machine.context.checkData && value == 'ONGOING3' && <View>
  319. <Text>{TimeFormatter.countdown((machine.context.checkData as any).current_record.fast.target_end_time)}</Text>
  320. </View>
  321. }
  322. <View>
  323. {
  324. key === 'FAST_SLEEP' && mixedBtns()
  325. }
  326. {
  327. key === 'FAST' && fastBtns()
  328. }
  329. {
  330. key === 'SLEEP' && sleepBtns()
  331. }
  332. </View>
  333. {
  334. showModal && isOpen && <Modal children={layoutContent()} dismiss={() => setIsOpen(false)} />
  335. }
  336. {
  337. isOpenDurationPicker && <Modal children={durationPickerContent()} dismiss={() => setIsOpenDurationPicker(false)} />
  338. }
  339. {/* {
  340. showModal && <AtFloatLayout
  341. isOpened={isOpen}
  342. onClose={() => {
  343. setIsOpen(false)
  344. setShowModal(false)
  345. }}
  346. title="">
  347. {
  348. layoutContent()
  349. }
  350. </AtFloatLayout>
  351. }
  352. <AtFloatLayout
  353. isOpened={isOpenDurationPicker}
  354. onClose={() => {
  355. setIsOpenDurationPicker(false)
  356. }}
  357. title="">
  358. {
  359. durationPickerContent()
  360. }
  361. </AtFloatLayout> */}
  362. </View>
  363. )
  364. }