| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- 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<any>(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 <View className="rn_swiper" />
- var timestamp = new Date().getTime()
- return <View><Swiper
- className="rn_swiper"
- indicatorDots
- indicatorColor='#ffffff66'
- indicatorActiveColor='#ffffff99'
- current={current}
- style={{ height: process.env.TARO_ENV == 'weapp' ? rpxToPx(204) + 96 + 4 : rpxToPx(204) + 96 + 12 + 10 }}
- >
- <SwiperItem>
- <DayNightCard isNight={firstNight} count={props.count} />
- </SwiperItem>
- <SwiperItem>
- <DayNightCard isNight={!firstNight} count={props.count} />
- </SwiperItem>
- </Swiper>
- <AllRings data={props.homeData} time={timestamp} />
- {/* <AllDayRings schedule={props.schedule} /> */}
- <DayNightSwiperPopup authInfo={authInfo} />
- </View>
- }
|