import { View, Text, Image } from '@tarojs/components' import './IndexConsole.scss' import { useTranslation } from 'react-i18next' import { useSelector } from 'react-redux'; import { endFast, endSleep, startFast, startSleep } from "../actions/TrackTimeActions"; import { jumpPage } from '../hooks/Common'; import { useEffect, useRef, useState } from 'react'; import LimitPickers from '@/components/input/LimitPickers'; import { getColor, getTimePickerTitle } from '../hooks/Console'; import { rpxToPx, vibrate } from '@/utils/tools'; import { TimeFormatter } from '@/utils/time_format'; let useNavigation; if (process.env.TARO_ENV == 'rn') { useNavigation = require("@react-navigation/native").useNavigation } let operateType = '' export default function IndexConsole(props: { record: any }) { const user = useSelector((state: any) => state.user); const { status } = props.record.current_record; const currentRecord = props.record.current_record; const { t } = useTranslation() const [fastDuration, setFastDuration] = useState(0); const [sleepDuration, setSleepDuration] = useState(0); const [expand, setExpand] = useState(false); // const [fastPickerValue, setFastPickerValue] = useState([0, 0]) // const [sleepPickerValue, setSleepPickerValue] = useState([0, 0]) const limitPickerRef = useRef(null) let navigation; if (useNavigation) { navigation = useNavigation() } useEffect(() => { if (currentRecord.fast) { var fastCount = currentRecord.fast.target_end_time - currentRecord.fast.target_start_time setFastDuration(fastCount) } if (currentRecord.sleep) { var sleepCount = currentRecord.sleep.target_end_time - currentRecord.sleep.target_start_time setSleepDuration(sleepCount) } }, [props.record]) function tapStartFast() { if (!user.isLogin) { jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation) return } operateType = 'startFast' showPicker() } function tapStartSleep() { if (!user.isLogin) { jumpPage('/pages/account/ChooseAuth', 'ChooseAuth') return } if (status != 'ONGOING1' && props.record.scenario.name == 'FAST_SLEEP') { vibrate() return; } operateType = 'startSleep' showPicker() } function tapEndSleep() { if (!user.isLogin) { jumpPage('/pages/account/ChooseAuth', 'ChooseAuth') return } if (status != 'ONGOING2' && status!='ONGOING') { vibrate() return; } operateType = 'endSleep' showPicker() } function tapEndFast() { if (!user.isLogin) { jumpPage('/pages/account/ChooseAuth', 'ChooseAuth') return } if (status == 'WAIT_FOR_START') { vibrate() return; } operateType = 'endFast' showPicker() } function layoutContent() { var limit = global.set_time - 7 * 3600 * 1000 * 24; global.limit = limit if (currentRecord.last_real_check_time) { limit = currentRecord.last_real_check_time global.limit = limit //当set_time秒数<=latest_record_time秒数时,最小限制时间戳需+1分钟 if (new Date(global.set_time).getSeconds() <= new Date(currentRecord.last_real_check_time).getSeconds() && global.set_time - currentRecord.last_real_check_time > 60000) { limit = limit + 60 * 1000 } } var title = getTimePickerTitle(currentRecord, t, operateType == 'endFast') var color = getColor(currentRecord, operateType == 'endFast') return { pickerConfirm(e) hidePicker() }} /> } function showPicker() { // global.scenario = 'FAST_SLEEP' if (global.testInfotimer) { return } global.set_time = new Date().getTime() updateNodeInfo() if (!global.isDebug) { return } // global.testInfotimer = setInterval(() => { // updateNodeInfo() // }, 1000) } function updateNodeInfo() { var node = layoutContent() global.showIndexModal(true, node, null); } function hidePicker() { var node = layoutContent() global.showIndexModal(false, node, null); } function pickerConfirm(t: number) { hidePicker() var date = new Date(t) var setDate = new Date(global.set_time); date.setMilliseconds(setDate.getMilliseconds()); date.setSeconds(setDate.getSeconds()); t = date.getTime(); switch (operateType) { case 'startFast': startFast(t, fastDuration).then(res => { global.indexPageRefresh() }) break case 'startSleep': startSleep(t, sleepDuration).then(res => { global.indexPageRefresh() }) break case 'endSleep': endSleep(t).then(res => { global.indexPageRefresh() }) break case 'endFast': endFast(t).then(res => { global.indexPageRefresh() }) break } } if (props.record.scenario.name == 'FAST') { return { status == 'WAIT_FOR_START' && {t('feature.track_time_duration.common.start_fast')} } { status == 'WAIT_FOR_START' && } {t('feature.track_time_duration.common.end_fast')} } else if (props.record.scenario.name == 'SLEEP') { return { status == 'WAIT_FOR_START' && {t('feature.track_time_duration.common.start_sleep')} } { status == 'WAIT_FOR_START' && } {t('feature.track_time_duration.common.end_sleep')} } return { status == 'WAIT_FOR_START' ? {t('feature.track_time_duration.common.start_fast')} : { vibrate() }} className='stage_btn'> 睡前断食 { status == 'ONGOING1' ? {TimeFormatter.countdown(currentRecord.fast.real_start_time)} : {TimeFormatter.countdown(currentRecord.fast.real_start_time, currentRecord.sleep.real_start_time)} } } { (status == 'WAIT_FOR_START' || status == 'ONGOING1') && { status != 'ONGOING1' && } {t('feature.track_time_duration.common.start_sleep')} } { (status != 'WAIT_FOR_START' && status != 'ONGOING1') && { vibrate() }} className='stage_btn'> 睡眠期间断食 { status == 'ONGOING2' ? {TimeFormatter.countdown(currentRecord.sleep.real_start_time)} : {TimeFormatter.countdown(currentRecord.sleep.real_start_time, currentRecord.sleep.real_end_time)} } } {(expand || (status != 'WAIT_FOR_START' && status != 'ONGOING1')) && } { (expand || (status != 'WAIT_FOR_START' && status != 'ONGOING1')) && (status == 'WAIT_FOR_START' || status == 'ONGOING1' || status == 'ONGOING2') && { status != 'ONGOING2' && } {t('feature.track_time_duration.common.end_sleep')} } { status == 'ONGOING3' && { vibrate() }} className='stage_btn'> 起床后断食 {TimeFormatter.countdown(currentRecord.sleep.real_end_time)} } {(expand || (status != 'WAIT_FOR_START')) && } { (expand || status != 'WAIT_FOR_START') && (status == 'WAIT_FOR_START' || status == 'ONGOING1' || status == 'ONGOING2' || status == 'ONGOING3') && { status == 'WAIT_FOR_START' && } {t('feature.track_time_duration.common.end_fast')} } { (status == 'WAIT_FOR_START' || status == 'ONGOING1') && { expand ? { setExpand(false) }}>收起 : { setExpand(true) }}>展开 } } }