import { View, Image } from "@tarojs/components"; import './time_record.scss' import Taro, { useRouter, useShareAppMessage } from "@tarojs/taro"; import { rpxToPx } from "@/utils/tools"; import { MainColorType } from "@/context/themes/color"; import NewButton, { NewButtonType } from "@/_health/base/new_button"; import { useEffect, useState } from "react"; import NewDateTimePicker from "@/_health/base/new_date_time_picker"; import dayjs from "dayjs"; import RingProgress from "../components/ring_progress"; import NewDurationPicker, { DurationPickerType } from "@/_health/base/new_durationpicker"; import { IconArrow, IconClose } from "@/components/basic/Icons"; import { useSelector } from "react-redux"; import { TimeFormatter } from "@/utils/time_format"; import { addEvents } from "@/services/health"; import Modal from "@/components/layout/Modal.weapp"; import { getDurationArc, getStartArc } from "@/features/health/hooks/health_hooks"; import ShareBtn from "@/components/basic/ShareBtn"; import PickerCard from "../components/picker_card"; let timer let useRoute; let useNavigation; if (process.env.TARO_ENV == 'rn') { useRoute = require("@react-navigation/native").useRoute useNavigation = require("@react-navigation/native").useNavigation } export default function TimeRecord() { const systemInfo: any = Taro.getWindowInfo ? Taro.getWindowInfo() : Taro.getSystemInfoSync(); const navigationBarHeight = systemInfo.statusBarHeight + 44; const record = useSelector((state: any) => state.record); const [enterTime] = useState(new Date().getTime()) const [showDatePicker, setShowDatePicker] = useState(false) const [showDurationPicker, setShowDurationPicker] = useState(false) const [showEndDatePicker, setShowEndDatePicker] = useState(false) const [count, setCount] = useState(0) const [status, setStatus] = useState('WFS') const [info, setInfo] = useState(null) const [loaded, setLoaded] = useState(false) const [showHighlight, setShowHighlight] = useState(false) const [showShare, setShowShare] = useState(false) const [shareUrl, setShareUrl] = useState('') let router let navigation; if (useNavigation) { navigation = useNavigation() } if (process.env.TARO_ENV == 'rn') { router = useRoute() } else { router = useRouter() } const { scenario } = router.params if (process.env.TARO_ENV == 'weapp') { useShareAppMessage((e) => { return { title: '分享标题', path: 'pages/clock/Clock', imageUrl: shareUrl } }) } useEffect(() => { const { events } = record events.map((item) => { if (item.scenario == scenario) { setInfo(item) setStatus(item.status) setLoaded(true) } }) downloadFile() timer = setInterval(() => { if (showDatePicker || showEndDatePicker || showDurationPicker) return setCount(count => count + 1) }, 1000) return () => { clearInterval(timer) } }, []) function downloadFile() { Taro.downloadFile({ url: 'https://background-pictures.oss-cn-beijing.aliyuncs.com/inapp/checkmark.png', success: (res) => { if (res.statusCode === 200) { global.checkImg = res.tempFilePath } else { } }, fail: (err) => { } }); } function tapClock() { if (status == 'WFS') setShowDurationPicker(true) } function start() { // var date = TimeFormatter.stringToDate(selDate, time) // date.setMilliseconds(new Date(enterTimestmap).getMilliseconds()) var params: any = { scenario: scenario, //ACTIVITY type: 'INTERVAL', sub_type: scenario, time: { start_timestamp: new Date().getTime(), duration: info.time.duration } } if (router.params.join_id) { params.join_key = router.params.join_id } // if (event_id && event_id != 'undefined') { // params.event_id = event_id // } // if (is_temp) { // params.event = window == 'EAT' ? 'EAT_CUSTOM' : 'ACTIVE_CUSTOM' // } // params.op_page = window == 'EAT' ? 'HOME_EAT' : 'HOME_ACTIVE' params.extra = { set_time: global.set_time ? global.set_time : new Date().getTime(), confirm_time: new Date().getTime() } // console.log('打卡提交数据', params) // if (posting) return // setPosting(true) addEvents(params).then(res => { Taro.eventCenter.trigger('refreshClockIndex') setShowHighlight(true) setTimeout(() => { setShowHighlight(false) }, 3000) setInfo((res as any).data) setStatus((res as any).data.status) // Taro.showToast({ // title: '成功', // icon: 'success' // }) }) } function end() { var params: any = { scenario: scenario, //ACTIVITY type: 'INTERVAL', sub_type: scenario, time: { end_timestamp: new Date().getTime() } } if (router.params.join_id) { params.join_key = router.params.join_id } params.extra = { set_time: global.set_time ? global.set_time : new Date().getTime(), confirm_time: new Date().getTime() } addEvents(params).then(res => { setShowHighlight(true) setTimeout(() => { setShowHighlight(false) }, 3000) Taro.eventCenter.trigger('refreshClockIndex') setInfo((res as any).data) setStatus((res as any).data.status) setShareUrl('') // Taro.showToast({ // title: '成功', // icon: 'success' // }) }) } function update(params) { var data: any = { id: info.id, time: { ...params } } addEvents(data).then(res => { Taro.eventCenter.trigger('refreshClockIndex') setInfo((res as any).data) setShowDatePicker(false) setShowDurationPicker(false) setShowEndDatePicker(false) }) } function tapStart() { setShowDatePicker(true) } function tapGoal() { setShowDurationPicker(true) } function ringExtra() { if (status == 'WFS') { const count = info.time.duration / 1000 var str = TimeFormatter.formateDurationBySeconds(count) return { header: '', value: str, footer: 'Edit', color: MainColorType.orange } } var percent = 100 * (new Date().getTime() - info.time.start_timestamp) / info.time.duration percent = parseInt(percent + '') return { header: '', value: TimeFormatter.countdown(info.time.start_timestamp), footer: `ELAPSED ${percent}%`, color: MainColorType.g02 } } function getBackground() { var time = record.time if (!time) return '#fff' const { background_colors } = time if (!background_colors) { return '#fff' } else if (background_colors.length == 1) { return background_colors[0] } return `linear-gradient(to bottom, ${background_colors[0]}, ${background_colors[1]})` } if (!loaded) { return } function doneComponent() { if (status == 'DONE') { return { shareUrl.length == 0 && { setShareUrl(url) } } /> } { Taro.navigateBack() }}> { shareUrl.length > 0 ? : } { { setShowDatePicker(true) }}> STARTED {TimeFormatter.dateTimeFormate(info.time.start_timestamp, true)} Edit Start { setShowEndDatePicker(true) }}> FINISHED {TimeFormatter.dateTimeFormate(info.time.end_timestamp, true)} Edit Finish } } } function render() { return { Taro.navigateBack() }} /> upcoming fast { tapClock() // setShowDurationPicker(true) }}> { status == 'OG' && { setShowDatePicker(true) }}> STARTED {TimeFormatter.dateTimeFormate(info.time.start_timestamp, true)} Edit Start { setShowDurationPicker(true) }}> {TimeFormatter.formateDurationBySeconds(info.time.duration / 1000)} Goal {TimeFormatter.dateTimeFormate(info.time.target_end_timestamp, true)} Edit Goal } { status == 'WFS' && } { status == 'OG' && } { status == 'OG' && { setShareUrl('') setShowShare(true) }} src={require('@assets/_health/wechat.png')} style={{ width: rpxToPx(72), height: rpxToPx(72) }} /> } { status == 'WFS' && { Taro.redirectTo({ url: `./log_record?scenario=${scenario == 'FAST' ? 'MEAL' : 'ACTIVITY'}` }) }}> Or share your meals if you haven't started fasting yet } { status == 'OG' && { Taro.redirectTo({ url: `./log_record?scenario=${scenario == 'FAST' ? 'MEAL' : 'ACTIVITY'}` }) }}> How are you feeling? } {/* { showDurationPicker && { setShowDurationPicker(false) }} confirm={() => { }}> { { console.log(e) var obj = JSON.parse(JSON.stringify(info)) obj.time.duration = e setInfo(obj) }} /> } } */} { showShare && { shareUrl == '' && { setShareUrl(url) } } /> } { setShowShare(false) }}> { shareUrl.length > 0 ? : } { status == 'OG' && STARTED {TimeFormatter.dateTimeFormate(info.time.start_timestamp, true)} {TimeFormatter.formateDurationBySeconds(info.time.duration / 1000)} Goal {TimeFormatter.dateTimeFormate(info.time.target_end_timestamp, true)} } } } return { status == 'DONE' ? doneComponent() : render() } { showDatePicker && { setShowDatePicker(false) }} value={info.time.start_timestamp} type="datetime" onConfirm={(e) => { update({ start_timestamp: e }) console.log(e) // var obj = JSON.parse(JSON.stringify(info)) // obj.time.duration = e // setInfo(obj) }} /> } { showEndDatePicker && { setShowDatePicker(false) }} value={info.time.end_timestamp} type="datetime" onConfirm={(e) => { update({ end_timestamp: e }) console.log(e) // var obj = JSON.parse(JSON.stringify(info)) // obj.time.duration = e // setInfo(obj) }} /> } { showDurationPicker && { setShowDurationPicker(false) }} value={info.time.duration} type="duration" onConfirm={(e) => { var obj = JSON.parse(JSON.stringify(info)) obj.time.duration = e setInfo(obj) if (status == 'WFS') return update({ duration: e }) }} /> } }