DayNightSwiper.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { Swiper, SwiperItem, View, Text } from "@tarojs/components";
  2. import DayNightCard from "./DayNightCard";
  3. import { useEffect, useState } from "react";
  4. import { TimeFormatter } from "@/utils/time_format";
  5. import { clearLocation, latestLocation } from "@/services/user";
  6. import { useDispatch, useSelector } from "react-redux";
  7. import '@/pages/clock/Clock.scss';
  8. import AllRings from "./AllRings";
  9. import AllDayRings from "./AllDayRings";
  10. import DayNightSwiperPopup from "./DayNightSwiperPopup";
  11. import { rpxToPx } from "@/utils/tools";
  12. let locationObj
  13. let useNavigation;
  14. let Linking;
  15. if (process.env.TARO_ENV == 'rn') {
  16. useNavigation = require("@react-navigation/native").useNavigation
  17. Linking = require('react-native').Linking;
  18. }
  19. export default function DayNightSwiper(props: { count: number, schedule: any, homeData: any }) {
  20. const user = useSelector((state: any) => state.user);
  21. const [loaded, setLoaded] = useState(false)
  22. const [firstNight, setFirstNight] = useState(true)
  23. const [current, setCurrent] = useState(0)
  24. const [authInfo, setAuthInfo] = useState<any>(null)
  25. useEffect(() => {
  26. setLoaded(false)
  27. getLatestLocation()
  28. }, [user.isLogin])
  29. global.swiperDayNightRefresh = () => {
  30. getLatestLocation()
  31. }
  32. useEffect(() => {
  33. if (loaded) {
  34. updateDate(user.isLogin ? locationObj : null)
  35. }
  36. }, [props.count])
  37. function getLatestLocation() {
  38. if (!user.isLogin) {
  39. setLoaded(true)
  40. updateDate(null)
  41. return
  42. }
  43. var today = new Date()
  44. var yesterday = new Date(today.getTime() - 24 * 3600 * 1000)
  45. var tomorrow = new Date(today.getTime() + 24 * 3600 * 1000 * 50)
  46. var strYesterday = `${yesterday.getFullYear()}-${TimeFormatter.padZero(yesterday.getMonth() + 1)}-${TimeFormatter.padZero(yesterday.getDate())}`
  47. var strTomorrow = `${tomorrow.getFullYear()}-${TimeFormatter.padZero(tomorrow.getMonth() + 1)}-${TimeFormatter.padZero(tomorrow.getDate())}`
  48. latestLocation({
  49. date_start: strYesterday, date_end: strTomorrow
  50. }).then(data => {
  51. // (data as any).timezone = 'GMT-1000'
  52. if (data && (data as any).daylights) {
  53. (data as any).daylights.map(item => {
  54. if (!item.sunrise_ts) {
  55. item.sunrise_ts = new Date(item.date + 'T' + item.sunrise + ':00').getTime()
  56. item.sunset_ts = new Date(item.date + 'T' + item.sunset + ':00').getTime()
  57. }
  58. })
  59. }
  60. setAuthInfo(data)
  61. updateDate(data)
  62. locationObj = data
  63. setLoaded(true)
  64. }).catch(e => {
  65. setLoaded(true)
  66. updateDate(null)
  67. })
  68. }
  69. function updateDate(data) {
  70. var today = new Date()
  71. if (data && data.daylights && data.daylights.length > 0) {
  72. if (data.night_completed) {
  73. if (today.getTime() > data.night_completed.sunrise_ts) {
  74. setFirstNight(true)
  75. setCurrent(1)
  76. return;
  77. }
  78. }
  79. setCurrent(0)
  80. // var sunriseObj,sunsetObj;
  81. var list = data.daylights
  82. for (var i = 0; i < (list.length - 1); i++) {
  83. //夜间
  84. if (list[i].sunset_ts < today.getTime() && list[i + 1].sunrise_ts > today.getTime()) {
  85. setFirstNight(true)
  86. }
  87. //白天
  88. else if (list[i].sunrise_ts < today.getTime() && list[i].sunset_ts > today.getTime()) {
  89. setFirstNight(false)
  90. }
  91. }
  92. }
  93. else {
  94. setCurrent(0)
  95. if (today.getHours() < 6 || today.getHours() > 18) {
  96. setFirstNight(true)
  97. } else {
  98. setFirstNight(false)
  99. }
  100. }
  101. }
  102. if (!loaded)
  103. return <View className="rn_swiper" />
  104. var timestamp = new Date().getTime()
  105. return <View><Swiper
  106. className="rn_swiper"
  107. indicatorDots
  108. indicatorColor='#ffffff66'
  109. indicatorActiveColor='#ffffff99'
  110. current={current}
  111. style={{height:process.env.TARO_ENV=='weapp'?rpxToPx(204)+96+4:rpxToPx(204)+96+12}}
  112. >
  113. <SwiperItem>
  114. <DayNightCard isNight={firstNight} count={props.count} />
  115. </SwiperItem>
  116. <SwiperItem>
  117. <DayNightCard isNight={!firstNight} count={props.count} />
  118. </SwiperItem>
  119. </Swiper>
  120. <AllRings data={props.homeData} time={timestamp} />
  121. <AllDayRings schedule={props.schedule} />
  122. <DayNightSwiperPopup authInfo={authInfo} />
  123. </View>
  124. }