| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220 |
- import { View, Text, Image } from "@tarojs/components";
- import './log_time.scss'
- import './fast_sleep.scss'
- import { useSelector } from "react-redux";
- import { useEffect, useRef, useState } from "react";
- import NewButton, { NewButtonType } from "../base/new_button";
- import { rpxToPx } from "@/utils/tools";
- import { getScenario, getThemeColor } from "@/features/health/hooks/health_hooks";
- import NewTimePicker from "../base/new_timepicker";
- import { IconCalendar, IconError } from "@/components/basic/Icons";
- import dayjs from "dayjs";
- import { useRouter } from "@tarojs/taro";
- import Card from "../components/card";
- import { MainColorType } from "@/context/themes/color";
- import Taro from "@tarojs/taro";
- import { clockTimes, fastWithSleep } from "@/services/health";
- import NewDurationPicker, { DurationPickerType } from "../base/new_durationpicker";
- import showAlert from "@/components/basic/Alert";
- import NewDatePicker, { NewDatePickerType } from "../base/new_date_picker";
- import ChooseDateTime from "../components/choose_date_time";
- import { TimeFormatter } from "@/utils/time_format";
- import StatusIndicator, { StatusType } from "../base/status_indicator";
- import { t } from "i18next";
- let useRoute;
- let useNavigation;
- let scenario = '';
- let AppState;
- if (process.env.TARO_ENV == 'rn') {
- useRoute = require("@react-navigation/native").useRoute
- useNavigation = require("@react-navigation/native").useNavigation
- AppState = require("react-native").AppState
- }
- export default function LogTime() {
- let router
- let navigation;
- if (useNavigation) {
- navigation = useNavigation()
- }
- if (process.env.TARO_ENV == 'rn') {
- router = useRoute()
- }
- else {
- router = useRouter()
- }
- const [enterTime] = useState(new Date().getTime())
- const [requestId, setRequestId] = useState(new Date().getTime())
- const isSingle = router.params.single == '1'
- const isFast = router.params.window == 'FAST'
- const isStart = router.params.is_start == '1'
- const isFastWithSleep = router.params.is_fast_with_sleep == '1'
- const type = router.params.type
- const [data, setData] = useState<any>(null)
- const health = useSelector((state: any) => state.health);
- const [showGoal, setShowGoal] = useState(router.params.longfast)
- const tapIndex = router.params.index ?? 0
- const [longDuration, setLongDuration] = useState(24)
- const [expandIndex, setExpandIndex] = useState(router.params.initIndex ? router.params.initIndex : tapIndex)
- const [chooseDate, setChooseDate] = useState(false)
- const [array, setArray] = useState<any>([])
- const [errors, setErrors] = useState<any>([])
- const [showSuccess, setShowSuccess] = useState(false)
- const [conflicts, setConflicts] = useState<any>([])
- const [loaded, setLoaded] = useState(false)
- const [autoCheck, setAutoCheck] = useState(false)
- const [initCheck, setInitCheck] = useState(router.params.initCheck == '1')
- const [initSeconds, setInitSeconds] = useState(router.params.initCheck == '1') //初始化时,带入打卡时的秒数
- const [count, setCount] = useState(0)
- const [ignoreCheck, setIgnoreCheck] = useState(false)
- const [posting, setPosting] = useState(false)
- useEffect(() => {
- loadData()
- var now = new Date()
- var t = now.getTime()
- now.setHours(0)
- now.setMilliseconds(0)
- now.setSeconds(0)
- now.setMinutes(0)
- var t2 = now.getTime()
- t2 += 24 * 3600 * 1000
- var left = t2 - t + 500
- setTimeout(() => {
- setCount(count => count + 1)
- setArray(array => {
- array.map((item) => {
- item.today = false
- })
- return array;
- })
- }, left)
- if (process.env.TARO_ENV == 'rn') {
- AppState.addEventListener('change', handleAppStateChange);
- }
- Taro.onAppShow(() => {
- setCount(count => count + 1)
- })
- }, [])
- const handleAppStateChange = (nextAppState) => {
- if (nextAppState != 'active') {
- return
- }
- if (nextAppState == 'active') {
- setCount(count => count + 1)
- }
- };
- useEffect(() => {
- if (autoCheck) {
- if (ignoreCheck) {
- setIgnoreCheck(false)
- }
- else {
- tapCommit(true)
- }
- }
- if (initCheck && array.length > 0) {
- tapCommit(true, true)
- setInitCheck(false)
- }
- }, [array])
- function loadData() {
- fastWithSleep().then(res => {
- setData(res)
- initDatas(res)
- })
- }
- function getLongDuration() {
- // var day = Math.floor(longDuration / 24)
- // var hour = longDuration % 24
- // var str = ''
- // if (day > 0)
- // str = day + '天'
- // if (hour > 0)
- // str += hour + '小时'
- // return str
- var days = Math.floor(longDuration / 24)
- var hours = longDuration % 24
- var str = `${days} 天`
- if (global.language == 'en') {
- str = days == 1 ? `${days} day` : `${days} days`
- }
- if (hours > 0) {
- if (global.language == 'en') {
- str = hours == 1 ? `${str} ${hours} hr` : `${str} ${hours} hrs`
- }
- else {
- str = `${str} ${hours} 小时`
- }
- }
- return str
- }
- function initDatas(res = data) {
- const { fast, sleep, status } = res
- var list: any = []
- if (isSingle) {
- var timeline: any;
- var time = dayjs().format('HH:mm')
- var date = dayjs().format('YYYY-MM-DD')
- if (isFast) {
- timeline = isStart ? fast.timeline[0] : fast.timeline[1]
- var fastScenario = getScenario(health.windows, 'FAST')
- if (!isFastWithSleep) {
- timeline = isStart ? fastScenario.timeline[0] : fastScenario.timeline[1]
- }
- Taro.setNavigationBarTitle({
- title: isStart ? fast.timeline[0].title : fast.timeline[1].title
- });
- //特殊处理单场景下,结束,超过24小时,picker设置为target_end_time
- if (isFastWithSleep) {
- if (!isStart && new Date().getTime() - fast.timeline[0].target.timestamp >= 24 * 3600 * 1000) {
- time = dayjs(fast.timeline[1].target.timestamp).format('HH:mm')
- date = dayjs(fast.timeline[1].target.timestamp).format('YYYY-MM-DD')
- }
- }
- else {
- if (!isStart && new Date().getTime() - fastScenario.timeline[0].target.timestamp >= 24 * 3600 * 1000) {
- time = dayjs(fastScenario.timeline[1].target.timestamp).format('HH:mm')
- date = dayjs(fastScenario.timeline[1].target.timestamp).format('YYYY-MM-DD')
- }
- }
- }
- else {
- timeline = isStart ? sleep.timeline[0] : sleep.timeline[1]
- var fastScenario = getScenario(health.windows, 'SLEEP')
- if (!isFastWithSleep) {
- timeline = isStart ? fastScenario.timeline[0] : fastScenario.timeline[1]
- }
- Taro.setNavigationBarTitle({
- title: isStart ? sleep.timeline[0].title : sleep.timeline[1].title
- });
- //特殊处理单场景下,结束,超过24小时,picker设置为target_end_time
- if (isFastWithSleep) {
- if (!isStart && new Date().getTime() - sleep.timeline[0].target.timestamp >= 24 * 3600 * 1000) {
- time = dayjs(sleep.timeline[1].target.timestamp).format('HH:mm')
- date = dayjs(sleep.timeline[1].target.timestamp).format('YYYY-MM-DD')
- }
- }
- else {
- if (!isStart && new Date().getTime() - fastScenario.timeline[0].target.timestamp >= 24 * 3600 * 1000) {
- time = dayjs(fastScenario.timeline[1].target.timestamp).format('HH:mm')
- date = dayjs(fastScenario.timeline[1].target.timestamp).format('YYYY-MM-DD')
- }
- }
- }
- if (router.params.longfast) {
- Taro.setNavigationBarTitle({
- title: t('health.long_fast')
- })
- }
- var time = dayjs().format('HH:mm')
- var date = dayjs().format('YYYY-MM-DD')
- list.push({
- event_id: timeline.event_id,
- schedule_id: timeline.schedule_id,
- time: time,
- date: date,
- extra: {
- set_time: enterTime,
- confirm_time: enterTime
- }
- })
- setExpandIndex(0)
- }
- else {
- for (var i = 0; i <= tapIndex; i++) {
- // if (type == 'home'){
- // if (tapIndex == 2){
- // if (i == 0 || i==3){
- // continue;
- // }
- // }
- // else if (tapIndex ==3){
- // if (i==1||i==2){
- // continue;
- // }
- // }
- // }
- var timeline: any;
- if (i == 0 || i == 3) {
- timeline = i == 0 ? fast.timeline[0] : fast.timeline[1]
- Taro.setNavigationBarTitle({
- title: i == 0 ? fast.timeline[0].title : fast.timeline[1].title
- });
- }
- else {
- timeline = i == 1 ? sleep.timeline[0] : sleep.timeline[1]
- Taro.setNavigationBarTitle({
- title: i == 1 ? sleep.timeline[0].title : sleep.timeline[1].title
- });
- }
- list.push({
- event_id: timeline.event_id,
- schedule_id: timeline.schedule_id,
- time: dayjs().format('HH:mm'),
- date: dayjs().format('YYYY-MM-DD'),
- extra: {
- set_time: enterTime,
- confirm_time: enterTime
- }
- })
- }
- //逆推初始化时间
- var fastStartTime = new Date().getTime()
- var fastEndTime = new Date().getTime()
- var sleepStartTime = new Date().getTime()
- var sleepEndTime = new Date().getTime()
- fastStartTime = fastEndTime - fast.target.duration
- var fastEndtoWakeDuration = 0
- var time1 = fast.period.end_time
- var time2 = sleep.period.end_time
- var t1 = parseInt(time1.split(':')[0]) * 60 + parseInt(time1.split(':')[1])
- var t2 = parseInt(time2.split(':')[0]) * 60 + parseInt(time2.split(':')[1])
- fastEndtoWakeDuration = t1 - t2 > 0 ? (t1 - t2) * 60 * 1000 : (t1 - t2) * 60 * 1000 + 24 * 3600 * 1000
- sleepEndTime = fastEndTime - fastEndtoWakeDuration//(fast.target.end_timestamp-sleep.target.end_timestamp)
- sleepStartTime = sleepEndTime - sleep.target.duration
- var isConflict = true
- // var isConflict = false;
- // if (fast.real && fast.real.start_timestamp && fast.real.start_timestamp < fastStartTime) {
- // isConflict = true;
- // }
- // else if (sleep.real && sleep.real.start_timestamp && sleep.real.start_timestamp > sleepStartTime) {
- // isConflict = true;
- // }
- // else if (sleep.real && sleep.real.end_timestamp && sleep.real.end_timestamp > sleepEndTime) {
- // isConflict = true;
- // }
- // if (!isConflict) {
- // list[0].date = dayjs(fastStartTime).format('YYYY-MM-DD')
- // list[0].time = dayjs(fastStartTime).format('HH:mm')
- // list[1].date = dayjs(sleepStartTime).format('YYYY-MM-DD')
- // list[1].time = dayjs(sleepStartTime).format('HH:mm')
- // if (list.length > 2) {
- // list[2].date = dayjs(sleepEndTime).format('YYYY-MM-DD')
- // list[2].time = dayjs(sleepEndTime).format('HH:mm')
- // }
- // }
- if (fast.real && fast.real.start_timestamp) {
- list[0].date = dayjs(fast.real.start_timestamp).format('YYYY-MM-DD')
- list[0].disable = true
- list[0].time = dayjs(fast.real.start_timestamp).format('HH:mm')
- }
- if (sleep.real && sleep.real.start_timestamp) {
- list[1].date = dayjs(sleep.real.start_timestamp).format('YYYY-MM-DD')
- list[1].disable = true
- list[1].time = dayjs(sleep.real.start_timestamp).format('HH:mm')
- }
- if (list.length > 2 && sleep.real && sleep.real.end_timestamp) {
- list[2].date = dayjs(sleep.real.end_timestamp).format('YYYY-MM-DD')
- list[2].disable = true
- list[2].time = dayjs(sleep.real.end_timestamp).format('HH:mm')
- }
- }
- if (status == 'OG2_NO1' && isFastWithSleep) {
- list[0].time = ''
- list[0].date = ''
- }
- setArray(list)
- setLoaded(true)
- }
- function footerBtnColor() {
- if (isSingle) {
- return isFast ? MainColorType.fast : MainColorType.sleep
- }
- else {
- if (tapIndex == '1' || tapIndex == '2') return MainColorType.sleep
- }
- return MainColorType.fast
- }
- function getTimestamp(obj) {
- const { event_id } = obj
- var tempObj: any = null
- if (data.fast) {
- data.fast.timeline.map((item) => {
- if (item.event_id == event_id) {
- tempObj = item
- }
- })
- }
- if (data.sleep) {
- data.sleep.timeline.map((item) => {
- if (item.event_id == event_id) {
- tempObj = item
- }
- })
- }
- var time = obj.time
- if (tempObj && tempObj.real && dayjs(tempObj.real.timestamp).format('YYYY-MM-DDTHH:mm') == obj.date + 'T' + time) {
- return tempObj.real.timestamp
- }
- // var hour = parseInt(time.split(':')[0])
- // var minute = parseInt(time.split(':')[1])
- // var date = obj.date
- // var now = new Date(obj.date + 'T' + time + ':' + dayjs(enterTime).format('ss'))
- var now = TimeFormatter.stringToDate(obj.date, time)
- now.setSeconds(parseInt(dayjs(enterTime).format('ss')))
- now.setMilliseconds(new Date(enterTime).getMilliseconds())
- // now.setHours(hour)
- // now.setMinutes(minute)
- // now.setSeconds(0)
- // now.setMilliseconds(0)
- var timestamp = now.getTime()
- return timestamp
- }
- function tapDone() {
- var findError = false
- for (var i = 0; i < array.length; i++) {
- var obj = array[i]
- var str = dayjs().format('HH:mm')
- var date = new Date(obj.date + ' ' + obj.time + ':00')
- var now = new Date()
- if (date.getTime() > now.getTime()) {
- // setTime(str)
- array[i].time = str
- console.log(str)
- setArray(array)
- setCount(count => count + 1)
- findError = true
- }
- }
- if (findError) {
- Taro.showToast({
- title: t('health.future_time_limit'),
- icon: 'none'
- })
- return
- }
- tapCommit(false)
- }
- function tapCommit(onlyCheck, firstCheck = false, save_confirm = null) {
- for (var i = 0; i < array.length; i++) {
- var obj = array[i]
- if (obj.time == '' || obj.date == '') {
- Taro.showToast({
- title: t('health.missing_date_time'),
- icon: 'none'
- })
- return;
- }
- }
- var list: any = []
- if (isSingle) {
- var obj = array[0]
- var timestamp = getTimestamp(obj)
- var params: any = {
- schedule_id: obj.schedule_id,
- event_id: obj.event_id ? obj.event_id : null,
- extra: obj.extra,
- date: dayjs(timestamp).format('YYYYMMDD'),
- timestamp: timestamp,
- }
- if (isStart) {
- params.duration = isFast ? data.fast.target.duration : data.sleep.target.duration
- }
- list.push(params)
- if (router.params.longfast && isStart) {
- list = [{
- schedule_id: health.long_fast.timeline[0].schedule_id,
- date: dayjs(timestamp).format('YYYYMMDD'),
- timestamp: timestamp,
- extra: {
- long_fast_goal: longDuration * 60,
- set_time: global.set_time ? global.set_time : new Date().getTime(),
- confirm_time: new Date().getTime()
- }
- }]
- }
- }
- else {
- if (type == 'home') {
- if (parseInt(tapIndex + '') == 2) {
- var obj = array[1]
- var timestamp = getTimestamp(obj)
- list.push({
- schedule_id: obj.schedule_id,
- event_id: obj.event_id ? obj.event_id : null,
- extra: obj.extra,
- date: dayjs(timestamp).format('YYYYMMDD'),
- timestamp: timestamp,
- })
- var obj2 = array[2]
- var timestamp2 = getTimestamp(obj2)
- list.push({
- schedule_id: obj2.schedule_id,
- event_id: obj.event_id ? obj.event_id : null,
- extra: obj2.extra,
- date: dayjs(timestamp2).format('YYYYMMDD'),
- timestamp: timestamp2,
- })
- }
- if (parseInt(tapIndex + '') == 3) {
- var obj = array[0]
- var timestamp = getTimestamp(obj)
- list.push({
- schedule_id: obj.schedule_id,
- event_id: obj.event_id ? obj.event_id : null,
- extra: obj.extra,
- date: dayjs(timestamp).format('YYYYMMDD'),
- timestamp: timestamp,
- })
- var obj2 = array[3]
- var timestamp2 = getTimestamp(obj2)
- list.push({
- schedule_id: obj2.schedule_id,
- event_id: obj.event_id ? obj.event_id : null,
- extra: obj2.extra,
- date: dayjs(timestamp2).format('YYYYMMDD'),
- timestamp: timestamp2,
- })
- }
- }
- else {
- // const { status } = data
- for (var i = 0; i <= tapIndex; i++) {
- if (firstCheck && i > 1) {
- }
- else {
- var obj = array[i]
- var timestamp = getTimestamp(obj)
- list.push({
- schedule_id: obj.schedule_id,
- event_id: obj.event_id ? obj.event_id : null,
- extra: obj.extra,
- date: dayjs(timestamp).format('YYYYMMDD'),
- timestamp: timestamp,
- })
- }
- }
- }
- }
- if (firstCheck && parseInt(router.params.initIndex) == 1) {
- var temps = [list[1], list[0]]
- commit(temps, onlyCheck, save_confirm)
- }
- else {
- commit(list, onlyCheck, save_confirm)
- }
- }
- function getLogMethod(list) {
- // if (isSingle) return 'LOG_ONCE'
- if (list.length == 1) return 'LOG_ONCE'
- const { status } = data
- if (status == 'OG2_MISALIGNED') return 'LOG_MULTIPLE'
- if (status == 'OG2_NO1') return 'LOG_MULTIPLE'
- if (status == 'WFS' && list.length > 1) return 'LOG_MULTIPLE'
- if (status == 'OG1' && list.length > 2) return 'LOG_MULTIPLE'
- if (status == 'OG2' && list.length > 3) return 'LOG_MULTIPLE'
- return 'LOG_ONCE'
- }
- function commit(list, onlyCheck, save_confirm) {
- const sortedData = list.sort((a, b) => {
- return b.extra.confirm_time - a.extra.confirm_time; // 降序
- });
- var params: any = {
- check_items: sortedData,
- op_page: isFastWithSleep ? 'FAST_WITH_SLEEP' : router.params.op_page ? router.params.op_page : null,
- only_check: onlyCheck,
- extra: {
- log_method: getLogMethod(list),
- set_time: enterTime,
- confirm_time: new Date().getTime()
- }
- }
- if (onlyCheck) {
- var dt = new Date().getTime()
- params.requestId = dt
- setRequestId(dt)
- }
- else {
- params.requestId = requestId
- }
- if (save_confirm) {
- params.save_confirm = save_confirm
- params.requestId = new Date().getTime()
- }
- console.log(requestId)
- if (posting) return
- setPosting(true)
- const url = onlyCheck ? '?only_check=true' : ''
- clockTimes(url, params).then(res => {
- if (onlyCheck) {
- setRequestId(new Date().getTime())
- }
- if ((res as any).result) {
- setErrors([])
- setConflicts([])
- if (!onlyCheck) {
- Taro.redirectTo({
- url: './post_result?data=' + JSON.stringify(res)
- })
- global.refreshWindow()
- if (global.refreshHistory)
- global.refreshHistory()
- if (global.refreshOtherHistory)
- global.refreshOtherHistory()
- if (global.refreshFastSleep)
- global.refreshFastSleep()
- if (global.refreshLongFast) {
- global.refreshLongFast()
- }
- }
- else {
- setPosting(false)
- }
- console.log('页面正常跳转到下一级')
- }
- else {
- setPosting(false)
- if ((res as any).warn_code == 'SAVE_AS_LF') {
- setRequestId(new Date().getTime())
- console.log(new Date().getTime())
- showAlert({
- title: t('health.save_as_lf_title'),
- content: t('health.save_as_lf_desc'),
- showCancel: true,
- cancelText: t('health.cancel'),
- confirmText: t('health.save'),
- cancel: () => {
- },
- confirm: () => {
- tapCommit(false, false, (res as any).warn_code)
- // Taro.showToast({
- // title: t('health.save_as_lf_toast'),
- // icon: 'success'
- // })
- }
- })
- return
- }
- if ((res as any).warn_code == 'SAVE_AS_IF') {
- setRequestId(new Date().getTime())
- showAlert({
- title: t('health.save_as_if_title'),
- content: t('health.save_as_if_desc'),
- showCancel: true,
- cancelText: t('health.cancel'),
- confirmText: t('health.save'),
- cancel: () => {
- },
- confirm: () => {
- tapCommit(false, false, (res as any).warn_code)
- // Taro.showToast({
- // title: t('health.save_as_if_toast'),
- // icon: 'success'
- // })
- }
- })
- return
- }
- setErrors((res as any).error_messages)
- setConflicts((res as any).conflict_schedule_ids)
- setShowSuccess(true)
- setAutoCheck(true)
- var tempList = JSON.parse(JSON.stringify(array))
- tempList.map(item => {
- if ((res as any).conflict_schedule_ids.includes(item.schedule_id)) {
- item.disable = false
- }
- })
- setIgnoreCheck(true)
- setArray(tempList)
- }
- // Taro.navigateBack({
- // delta: 1
- // })
- }).catch(e => {
- setPosting(false)
- })
- }
- function changeToScheduleTime(schedule_time, index) {
- var nowTime = parseInt(dayjs().format('HHmm'))
- var scheduleTime = parseInt(schedule_time.replace(':', ''))
- var date = dayjs().format('YYYY-MM-DD')
- if (scheduleTime > nowTime) {
- date = dayjs(new Date().getTime() - 24 * 3600 * 1000).format('YYYY-MM-DD')
- }
- var list = JSON.parse(JSON.stringify(array))
- list[index].date = date
- list[index].time = schedule_time
- list[index].extra.confirm_time = new Date().getTime()
- setArray(list)
- }
- function durationTime() {
- const { fast, sleep } = data
- var seconds = isFast ? fast.target.duration / 1000 : sleep.target.duration / 1000
- var hour = Math.floor(seconds / 3600)
- var minutes = Math.floor((seconds % 3600) / 60)
- var str = ''
- if (hour > 0) {
- str = hour + TimeFormatter.getHoursUnit(hour)
- }
- if (minutes > 0) {
- str += minutes + TimeFormatter.getMinutesUnit(minutes)
- }
- if (hour == 0 && minutes == 0) {
- str = 0 + TimeFormatter.getMinutesUnit(0)
- }
- return str
- }
- // function changeTimeText(schedule_time, time) {
- // if (time == schedule_time) {
- // return `Check in:Today ${dayjs(enterTime).format('HH:mm')}`
- // }
- // return `Scheduled for ${schedule_time}`
- // }
- // function changeTimeText(timestamp,date, time) {
- // // if (new Date().getTime()>timestamp){
- // return t('health.schedule_for',{time:dayjs(timestamp).format('HH:mm')})
- // // }
- // // if (time == schedule_time) {
- // // return `Check in:Today ${dayjs(enterTime).format('HH:mm')}`
- // // }
- // // return `Scheduled for ${schedule_time}`
- // }
- function tapChangeTime(schedule_time, time, index) {
- if (time == schedule_time) {
- changeToScheduleTime(dayjs(enterTime).format('HH:mm'), index)
- }
- else {
- changeToScheduleTime(schedule_time, index)
- }
- }
- function expectText() {
- var obj = array[0]
- var timestamp = getTimestamp(obj)
- var duration = isFast ? data.fast.target.duration : data.sleep.target.duration
- if (router.params.longfast) {
- duration = longDuration * 1000 * 3600
- }
- var time2 = timestamp + duration
- if (router.params.longfast) {
- if (TimeFormatter.isToday(time2)) {
- return t('health.end_fast_date_time', { date: global.language == 'en' ? 'today' : '今天', time: dayjs(time2).format('HH:mm') })
- }
- else if (TimeFormatter.isTomorrowday(time2)) {
- return t('health.end_fast_date_time', { date: global.language == 'en' ? 'tomorrow' : '明天', time: dayjs(time2).format('HH:mm') })
- }
- return t('health.end_fast_date_time', { date: global.language == 'en' ? dayjs(time2).format('on MMM D') : dayjs(time2).format('MMMD日'), time: dayjs(time2).format('HH:mm') })//'Expect to end fast at ' + dayjs(time2).format('MM-DD HH:mm')
- }
- var strTime = TimeFormatter.dayjsFormat(time2, false)
- if (isFast) {
- return 'Expect to end fast at ' + strTime
- }
- else {
- return 'Expect to wake up at ' + strTime
- }
- }
- function logItem(index: number, iFast: boolean, iStart: boolean, showLine: boolean) {
- const { fast, sleep, status } = data
- var schedule_time = ''
- var title = ''
- var date = array[index].date//array[index].today ? "Today" : "Yesterday"
- var time = array[index].time
- var min = enterTime
- var max = new Date().getTime()
- if (iFast) {
- var timeline = index == 0 ? fast.timeline[0] : fast.timeline[1]
- if (!isFastWithSleep) {
- //首页单场景进入时时间限制处理逻辑
- var fastScenario = getScenario(health.windows, 'FAST')
- timeline = iStart ? fastScenario.timeline[0] : fastScenario.timeline[1]
- if (fastScenario.status == 'WFS') {
- min = enterTime - 24 * 3600 * 1000
- max = new Date().getTime()
- }
- else {
- min = timeline.picker.min_timestamp
- max = timeline.picker.max_timestamp ?? new Date().getTime()
- }
- }
- else {
- //fast_sleep 进入时,除了wfs情况前端处理,其他case后端处理
- if (status == 'WFS') {
- min = enterTime - 24 * 3600 * 1000
- max = new Date().getTime()
- }
- else if (status == 'OG2_MISALIGNED') {
- if (index == 0) {
- min = sleep.real.start_timestamp - 24 * 3600 * 1000
- max = sleep.real.start_timestamp
- }
- else {
- min = fast.real.start_timestamp
- max = new Date().getTime()
- }
- }
- else {
- min = timeline.picker.min_timestamp
- max = timeline.picker.max_timestamp ?? new Date().getTime()
- }
- }
- }
- else {
- var timeline = index == 1 ? sleep.timeline[0] : sleep.timeline[1]
- if (isSingle){
- timeline = sleep.timeline[0]
- }
- if (status == 'WFS') {
- min = enterTime - 24 * 3600 * 1000
- max = new Date().getTime()
- }
- else if (status == 'OG2_MISALIGNED') {
- if (index == 1) {
- min = fast.real.start_timestamp
- }
- else {
- min = sleep.real.start_timestamp
- }
- max = new Date().getTime()
- }
- else {
- min = timeline.picker.min_timestamp
- max = timeline.picker.max_timestamp ?? new Date().getTime()
- }
- }
- const today = dayjs();
- var schedule_id = ''
- if (iFast) {
- schedule_time = iStart ? fast.period.start_time : fast.period.end_time
- schedule_id = iStart ? fast.timeline[0].schedule_id : fast.timeline[1].schedule_id
- if (!iStart && fast.status == 'OG') {
- schedule_time = dayjs(fast.target.end_timestamp).format('HH:mm')
- }
- title = iStart ? fast.timeline[0].title : fast.timeline[1].title
- if (array[index].disable) {
- var timestamp = iStart ? fast.real.start_timestamp : fast.real.end_timestamp
- date = dayjs(timestamp).format('YYYY-MM-DD')
- // const dt = dayjs(timestamp);
- // const yesterday = today.subtract(1, 'day');
- // time = dayjs(timestamp).format('HH:mm')
- // if (dt.isSame(today, 'day')) {
- // date = global.language == 'en' ? 'Today' : '今天';
- // } else if (dt.isSame(yesterday, 'day')) {
- // date = global.language == 'en' ? 'Yesterday' : '昨天';
- // } else {
- // date = global.language == 'en' ? dt.format('MMM D') : dt.format('MMMD日');
- // }
- }
- }
- else {
- schedule_time = iStart ? sleep.period.start_time : sleep.period.end_time
- schedule_id = iStart ? sleep.timeline[0].schedule_id : sleep.timeline[1].schedule_id
- if (!iStart && sleep.status == 'OG') {
- schedule_time = dayjs(sleep.target.end_timestamp).format('HH:mm')
- }
- title = iStart ? sleep.timeline[0].title : sleep.timeline[1].title
- if (array[index].disable) {
- var timestamp = iStart ? sleep.real.start_timestamp : sleep.real.end_timestamp
- date = dayjs(timestamp).format('YYYY-MM-DD')
- // const dt = dayjs(timestamp);
- // const yesterday = today.subtract(1, 'day');
- // time = dayjs(timestamp).format('HH:mm')
- // if (dt.isSame(today, 'day')) {
- // date = global.language == 'en' ? 'Today' : '今天';
- // } else if (dt.isSame(yesterday, 'day')) {
- // date = global.language == 'en' ? 'Yesterday' : '昨天';
- // } else {
- // date = global.language == 'en' ? dt.format('MMM D') : dt.format('MMMD日');
- // }
- }
- }
- var showError = false;
- if (conflicts.includes(schedule_id)) {
- showError = true
- }
- function getTimestamp() {
- if (router.params.longfast) {
- if (health.long_fast.timeline && health.long_fast.timeline.length > 0 && health.long_fast.timeline[1].target) {
- return health.long_fast.timeline[1].target.timestamp
- }
- return null
- }
- return timeline.target.timestamp
- }
- return <ChooseDateTime
- title={isSingle ? null : <StatusIndicator
- type={showError ? StatusType.img : StatusType.normal}
- color={showError ? MainColorType.error : iFast ? MainColorType.fast : MainColorType.sleep}
- fontColor="#000"
- fontSize={rpxToPx(34)}
- text={title}
- defaultSpace
- >
- {
- showError && <IconError color="#fff" width={rpxToPx(26)} />
- }
- </StatusIndicator>}
- disable={array[index].disable}
- showError={showError}
- showLine={showLine}
- targetTimestamp={getTimestamp()}
- enterTimestamp={enterTime}
- color={iFast ? MainColorType.fast : MainColorType.sleep}
- minTimestamp={min}
- maxTimestamp={max}
- date={date}
- time={time}
- count={count}
- expand={expandIndex == index}
- choose={() => {
- setExpandIndex(index)
- }}
- change={(e) => {
- var list = JSON.parse(JSON.stringify(array))
- list[index].date = e[0]
- list[index].time = e[1]
- list[index].extra.confirm_time = new Date().getTime()
- setArray(list)
- }}
- />
- }
- function multiContent() {
- if (type == 'home') {
- switch (parseInt(tapIndex + '')) {
- case 2:
- return <View style={{ position: 'relative' }}>
- {
- logItem(1, false, true, false)
- }
- {
- logItem(2, false, false, false)
- }
- </View>
- case 3:
- return <View style={{ position: 'relative' }}>
- {
- logItem(0, true, true, false)
- }
- {
- logItem(3, true, false, false)
- }
- </View>
- }
- }
- const { status } = data
- switch (parseInt(tapIndex + '')) {
- case 1:
- return <View style={{ position: 'relative' }}>
- {
- logItem(0, true, true, true)
- }
- {
- logItem(1, false, true, false)
- }
- </View>
- case 2:
- return <View style={{ position: 'relative' }}>
- {
- logItem(0, true, true, true)
- }
- {
- logItem(1, false, true, true)
- }
- {
- logItem(2, false, false, false)
- }
- </View>
- case 3:
- return <View style={{ position: 'relative' }}>
- {
- logItem(0, true, true, true)
- }
- {
- logItem(1, false, true, true)
- }
- {
- logItem(2, false, false, true)
- }
- {
- logItem(3, true, false, false)
- }
- </View>
- }
- return <View style={{ position: 'relative' }}></View>
- }
- function goalCard() {
- if (isSingle && isStart && showGoal) {
- if (router.params.longfast) {
- return <Card>
- <View style={{ position: 'relative' }}>
- <View className="card_header" style={{ justifyContent: 'space-between' }}>
- <View className="h34" style={{ marginLeft: rpxToPx(12) }}>{t('health.goal')}</View>
- <NewButton
- type={expandIndex == -1 ? NewButtonType.alpha : NewButtonType.gray}
- color={isFast ? MainColorType.fast : MainColorType.sleep}
- height={rpxToPx(84)}
- title={getLongDuration()}
- fontNormal
- onClick={() => { setExpandIndex(-1) }}
- />
- {
- expandIndex == -1 && <View className="border_footer_line" style={{ left: rpxToPx(40), right: rpxToPx(40) }} />
- }
- {/* </View> */}
- </View>
- {
- expandIndex == -1 && <View style={{
- display: 'flex',
- flexDirection: 'column',
- alignItems: 'center'
- }}>
- <NewDurationPicker color={MainColorType.fast}
- type={DurationPickerType.long}
- value={longDuration}
- onChange={(e) => {
- setLongDuration(e)
- }}
- />
- </View>
- }
- </View>
- </Card>
- }
- else {
- return <Card>
- <View style={{ position: 'relative' }}>
- <View className="card_header" style={{ justifyContent: 'space-between' }}>
- <View className="h34" style={{ marginLeft: rpxToPx(12) }}>{t('health.goal')}</View>
- {/* <View className="h34" style={{ marginLeft: rpxToPx(12) }}>{isFast ? 'Fast Goal' : 'Sleep Goal'}</View> */}
- {/* <View style={{
- borderColor: expandIndex == -1 ? isFast ? MainColorType.fast : MainColorType.sleep : 'transparent',
- borderWidth: rpxToPx(2),
- borderRadius: rpxToPx(88 / 4),
- borderStyle: 'solid'
- }}> */}
- <NewButton
- type={expandIndex == -1 ? NewButtonType.alpha : NewButtonType.gray}
- color={isFast ? MainColorType.fast : MainColorType.sleep}
- height={rpxToPx(84)}
- width={rpxToPx(238)}
- title={durationTime()}
- fontNormal
- onClick={() => { setExpandIndex(-1) }}
- />
- {/* </View> */}
- </View>
- {
- expandIndex == -1 && <View style={{
- display: 'flex',
- flexDirection: 'column',
- alignItems: 'center'
- }}>
- <NewDurationPicker
- type={DurationPickerType.normal}
- value={isFast ? data.fast.target.duration : data.sleep.target.duration}
- onChange={e => {
- var temp = JSON.parse(JSON.stringify(data))
- if (isFast) {
- temp.fast.target.duration = e
- }
- else {
- temp.sleep.target.duration = e
- }
- setData(temp)
- }}
- color={isFast ? MainColorType.fast : MainColorType.sleep} />
- </View>
- }
- </View>
- </Card>
- }
- }
- return <View />
- }
- if (!loaded) return <View />
- return <View className="page_container" style={{ minHeight: '101vh' }}>
- <View style={{ height: rpxToPx(36) }} />
- <Card>
- <View style={{ display: 'flex', flexDirection: 'column' }}>
- {
- errors.map((item, index) => {
- return <View className="error_bg" key={index}>
- <View className="error_icon_bg">
- <IconError color="#fff" width={rpxToPx(26)} />
- </View>
- <Text className="h24" style={{ lineHeight: rpxToPx(36) + 'px' }}>{item}</Text>
- </View>
- })
- }
- {
- autoCheck && showSuccess && errors.length == 0 && <View className="error_bg" style={{ backgroundColor: MainColorType.success + '1A' }}>
- <View className="error_icon_bg" style={{ backgroundColor: MainColorType.success }}>
- <Image src={require('@assets/_health/tip_check.png')} style={{ width: rpxToPx(26), height: rpxToPx(26) }} />
- </View>
- <Text className="h24" style={{ lineHeight: rpxToPx(36) + 'px' }}>{t('health.conflict_resolve')}</Text>
- </View>
- }
- {
- isSingle ? <View style={{ position: 'relative' }}>
- {
- logItem(0, isFast, isStart, false)
- }
- </View> : multiContent()
- }
- </View>
- </Card>
- <View style={{ height: rpxToPx(36) }} />
- {/* {
- isSingle && isStart && !showGoal && <View style={{ height: rpxToPx(72), marginTop: rpxToPx(18) }}>
- <NewButton
- type={NewButtonType.link}
- title="Show More"
- onClick={() => {
- setShowGoal(true)
- }}
- />
- </View>
- } */}
- {
- goalCard()
- }
- {
- isSingle && isStart && showGoal && <View className="h24 g02" style={{
- width: rpxToPx(750), height: rpxToPx(84),
- display: 'flex',
- alignItems: 'center',
- justifyContent: 'center'
- }}>{expectText()}</View>
- }
- <View style={{ flex: 1 }} />
- {/* <View style={{ marginBottom: rpxToPx(128), display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
- <NewButton
- color={footerBtnColor()}
- type={NewButtonType.fill}
- title={'Log'}
- width={rpxToPx(646)}
- height={rpxToPx(96)}
- disable={errors.length > 0}
- bold={true}
- onClick={() => tapCommit(false)} />
- </View> */}
- <View className="main_footer">
- <NewButton
- color={footerBtnColor()}
- type={NewButtonType.fill}
- title={isSingle ? t('health.log_moment') : t('health.log_moments')}
- width={rpxToPx(646)}
- height={rpxToPx(96)}
- disable={errors.length > 0}
- bold={true}
- onClick={() => tapDone()} />
- </View>
- </View>
- }
|