import { Swiper, SwiperItem, View, Text } from "@tarojs/components"; import DayNightCard from "./DayNightCard"; import { useEffect, useState } from "react"; import { TimeFormatter } from "@/utils/time_format"; import { clearLocation, latestLocation } from "@/services/user"; import { useDispatch, useSelector } from "react-redux"; import '@/pages/clock/Clock.scss'; import AllRings from "./AllRings"; import AllDayRings from "./AllDayRings"; import DayNightSwiperPopup from "./DayNightSwiperPopup"; import { kIsIOS, rpxToPx } from "@/utils/tools"; import dayjs from "dayjs"; let locationObj let useNavigation; let Linking; if (process.env.TARO_ENV == 'rn') { useNavigation = require("@react-navigation/native").useNavigation Linking = require('react-native').Linking; } export default function DayNightSwiper(props: { count: number, schedule: any, homeData: any }) { const user = useSelector((state: any) => state.user); const [loaded, setLoaded] = useState(false) const [firstNight, setFirstNight] = useState(true) const [current, setCurrent] = useState(0) const [authInfo, setAuthInfo] = useState(null) useEffect(() => { setLoaded(false) getLatestLocation() }, [user.isLogin]) global.swiperDayNightRefresh = () => { debugger getLatestLocation() } global.updateSwiper = ()=>{ getLatestLocation() } useEffect(() => { if (loaded) { updateDate(user.isLogin ? locationObj : null) } }, [props.count]) function getLatestLocation() { if (!user.isLogin) { setLoaded(true) updateDate(null) return } var today = new Date() var yesterday = new Date(today.getTime() - 24 * 3600 * 1000) var tomorrow = new Date(today.getTime() + 24 * 3600 * 1000 * 50) var strYesterday = `${yesterday.getFullYear()}-${TimeFormatter.padZero(yesterday.getMonth() + 1)}-${TimeFormatter.padZero(yesterday.getDate())}` var strTomorrow = `${tomorrow.getFullYear()}-${TimeFormatter.padZero(tomorrow.getMonth() + 1)}-${TimeFormatter.padZero(tomorrow.getDate())}` latestLocation({ date_start: strYesterday, date_end: strTomorrow }).then(data => { // (data as any).timezone = 'GMT-1000' if (data && (data as any).daylights) { (data as any).daylights.map(item => { if (!item.sunrise_ts) { item.sunrise_ts = new Date(item.date + 'T' + item.sunrise + ':00').getTime() item.sunset_ts = new Date(item.date + 'T' + item.sunset + ':00').getTime() } }) } if (process.env.TARO_ENV == 'rn') { setLocalPush(data) } setAuthInfo(data) updateDate(data) locationObj = data setLoaded(true) }).catch(e => { setLoaded(true) updateDate(null) }) } function setLocalPush(data) { const { sunrise, sunset, solar_noon } = data.local_push; var list: any = []; if (!data.has_daylight_calc) { return; } if (data && (data as any).daylights) { (data as any).daylights.map((item, index) => { var dayDuration = '' var nightDuration = '' if (item.sunrise_ts && item.sunset_ts) { dayDuration = TimeFormatter.durationFormate(item.sunset_ts, item.sunrise_ts, true) } if (index + 1 < (data as any).daylights.length - 1) { var nextObj = (data as any).daylights[index + 1]; if (nextObj.sunrise_ts && item.sunset_ts) { nightDuration = TimeFormatter.durationFormate(nextObj.sunrise_ts, item.sunset_ts, true) } } if (sunrise && item.sunrise_ts) { list.push({ channel_id: 'REMINDER_SUN', category_id: 'REMINDER_SUN_RISE', title: `Sunrise at ${dayjs(item.sunrise_ts).format('HH:mm')}`, body: `Daylight lasts ${dayDuration}.`, timestamp: item.sunrise_ts }) } if (sunset && item.sunset_ts) { list.push({ channel_id: 'REMINDER_SUN', category_id: 'REMINDER_SUN_SET', title: `Sunset at ${dayjs(item.sunset_ts).format('HH:mm')}`, body: `The night lasts ${nightDuration}.`, timestamp: item.sunset_ts }) } if (solar_noon && item.solar_noon_ts) { list.push({ channel_id: 'REMINDER_SUN', category_id: 'REMINDER_SUN_SOLAR_NOON', title: `Solar Noon at ${dayjs(item.solar_noon_ts).format('HH:mm')}`, body: 'The Sun is currently at its highest point in the sky for the day.', timestamp: item.solar_noon_ts }) } }) } if (process.env.TARO_ENV == 'rn') { var Jto = require('react-native').NativeModules.NativeBridge; if (list.length==0){ return } if (kIsIOS) { Jto.addSunPush(list) } else { Jto.addSunPush(JSON.stringify(list)) } } } function updateDate(data) { var today = new Date() if (data && data.daylights && data.daylights.length > 0) { if (data.night_completed) { if (today.getTime() > data.night_completed.sunrise_ts) { setFirstNight(true) setCurrent(1) return; } } setCurrent(0) // var sunriseObj,sunsetObj; var list = data.daylights for (var i = 0; i < (list.length - 1); i++) { //夜间 if (list[i].sunset_ts < today.getTime() && list[i + 1].sunrise_ts > today.getTime()) { setFirstNight(true) } //白天 else if (list[i].sunrise_ts < today.getTime() && list[i].sunset_ts > today.getTime()) { setFirstNight(false) } } } else { setCurrent(0) if (today.getHours() < 6 || today.getHours() > 18) { setFirstNight(true) } else { setFirstNight(false) } } } if (!loaded) return var timestamp = new Date().getTime() return {/* */} }