DayNightSwiper.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 { kIsIOS, rpxToPx } from "@/utils/tools";
  12. import dayjs from "dayjs";
  13. let locationObj
  14. let useNavigation;
  15. let Linking;
  16. if (process.env.TARO_ENV == 'rn') {
  17. useNavigation = require("@react-navigation/native").useNavigation
  18. Linking = require('react-native').Linking;
  19. }
  20. export default function DayNightSwiper(props: { count: number, schedule: any, homeData: any }) {
  21. const user = useSelector((state: any) => state.user);
  22. const [loaded, setLoaded] = useState(false)
  23. const [firstNight, setFirstNight] = useState(true)
  24. const [current, setCurrent] = useState(0)
  25. const [authInfo, setAuthInfo] = useState<any>(null)
  26. useEffect(() => {
  27. setLoaded(false)
  28. getLatestLocation()
  29. }, [user.isLogin])
  30. global.swiperDayNightRefresh = () => {
  31. debugger
  32. getLatestLocation()
  33. }
  34. global.updateSwiper = ()=>{
  35. getLatestLocation()
  36. }
  37. useEffect(() => {
  38. if (loaded) {
  39. updateDate(user.isLogin ? locationObj : null)
  40. }
  41. }, [props.count])
  42. function getLatestLocation() {
  43. if (!user.isLogin) {
  44. setLoaded(true)
  45. updateDate(null)
  46. return
  47. }
  48. var today = new Date()
  49. var yesterday = new Date(today.getTime() - 24 * 3600 * 1000)
  50. var tomorrow = new Date(today.getTime() + 24 * 3600 * 1000 * 50)
  51. var strYesterday = `${yesterday.getFullYear()}-${TimeFormatter.padZero(yesterday.getMonth() + 1)}-${TimeFormatter.padZero(yesterday.getDate())}`
  52. var strTomorrow = `${tomorrow.getFullYear()}-${TimeFormatter.padZero(tomorrow.getMonth() + 1)}-${TimeFormatter.padZero(tomorrow.getDate())}`
  53. latestLocation({
  54. date_start: strYesterday, date_end: strTomorrow
  55. }).then(data => {
  56. // (data as any).timezone = 'GMT-1000'
  57. if (data && (data as any).daylights) {
  58. (data as any).daylights.map(item => {
  59. if (!item.sunrise_ts) {
  60. item.sunrise_ts = new Date(item.date + 'T' + item.sunrise + ':00').getTime()
  61. item.sunset_ts = new Date(item.date + 'T' + item.sunset + ':00').getTime()
  62. }
  63. })
  64. }
  65. if (process.env.TARO_ENV == 'rn') {
  66. setLocalPush(data)
  67. }
  68. setAuthInfo(data)
  69. updateDate(data)
  70. locationObj = data
  71. setLoaded(true)
  72. }).catch(e => {
  73. setLoaded(true)
  74. updateDate(null)
  75. })
  76. }
  77. function setLocalPush(data) {
  78. const { sunrise, sunset, solar_noon } = data.local_push;
  79. var list: any = [];
  80. if (!data.has_daylight_calc) {
  81. return;
  82. }
  83. if (data && (data as any).daylights) {
  84. (data as any).daylights.map((item, index) => {
  85. var dayDuration = ''
  86. var nightDuration = ''
  87. if (item.sunrise_ts && item.sunset_ts) {
  88. dayDuration = TimeFormatter.durationFormate(item.sunset_ts, item.sunrise_ts, true)
  89. }
  90. if (index + 1 < (data as any).daylights.length - 1) {
  91. var nextObj = (data as any).daylights[index + 1];
  92. if (nextObj.sunrise_ts && item.sunset_ts) {
  93. nightDuration = TimeFormatter.durationFormate(nextObj.sunrise_ts, item.sunset_ts, true)
  94. }
  95. }
  96. if (sunrise && item.sunrise_ts) {
  97. list.push({
  98. channel_id: 'REMINDER_SUN',
  99. category_id: 'REMINDER_SUN_RISE',
  100. title: `Sunrise at ${dayjs(item.sunrise_ts).format('HH:mm')}`,
  101. body: `Daylight lasts ${dayDuration}.`,
  102. timestamp: item.sunrise_ts
  103. })
  104. }
  105. if (sunset && item.sunset_ts) {
  106. list.push({
  107. channel_id: 'REMINDER_SUN',
  108. category_id: 'REMINDER_SUN_SET',
  109. title: `Sunset at ${dayjs(item.sunset_ts).format('HH:mm')}`,
  110. body: `The night lasts ${nightDuration}.`,
  111. timestamp: item.sunset_ts
  112. })
  113. }
  114. if (solar_noon && item.solar_noon_ts) {
  115. list.push({
  116. channel_id: 'REMINDER_SUN',
  117. category_id: 'REMINDER_SUN_SOLAR_NOON',
  118. title: `Solar Noon at ${dayjs(item.solar_noon_ts).format('HH:mm')}`,
  119. body: 'The Sun is currently at its highest point in the sky for the day.',
  120. timestamp: item.solar_noon_ts
  121. })
  122. }
  123. })
  124. }
  125. if (process.env.TARO_ENV == 'rn') {
  126. var Jto = require('react-native').NativeModules.NativeBridge;
  127. if (list.length==0){
  128. return
  129. }
  130. if (kIsIOS) {
  131. Jto.addSunPush(list)
  132. }
  133. else {
  134. Jto.addSunPush(JSON.stringify(list))
  135. }
  136. }
  137. }
  138. function updateDate(data) {
  139. var today = new Date()
  140. if (data && data.daylights && data.daylights.length > 0) {
  141. if (data.night_completed) {
  142. if (today.getTime() > data.night_completed.sunrise_ts) {
  143. setFirstNight(true)
  144. setCurrent(1)
  145. return;
  146. }
  147. }
  148. setCurrent(0)
  149. // var sunriseObj,sunsetObj;
  150. var list = data.daylights
  151. for (var i = 0; i < (list.length - 1); i++) {
  152. //夜间
  153. if (list[i].sunset_ts < today.getTime() && list[i + 1].sunrise_ts > today.getTime()) {
  154. setFirstNight(true)
  155. }
  156. //白天
  157. else if (list[i].sunrise_ts < today.getTime() && list[i].sunset_ts > today.getTime()) {
  158. setFirstNight(false)
  159. }
  160. }
  161. }
  162. else {
  163. setCurrent(0)
  164. if (today.getHours() < 6 || today.getHours() > 18) {
  165. setFirstNight(true)
  166. } else {
  167. setFirstNight(false)
  168. }
  169. }
  170. }
  171. if (!loaded)
  172. return <View className="rn_swiper" />
  173. var timestamp = new Date().getTime()
  174. return <View><Swiper
  175. className="rn_swiper"
  176. indicatorDots
  177. indicatorColor='#ffffff66'
  178. indicatorActiveColor='#ffffff99'
  179. current={current}
  180. style={{ height: process.env.TARO_ENV == 'weapp' ? rpxToPx(204) + 96 + 4 : rpxToPx(204) + 96 + 12 + 10 }}
  181. >
  182. <SwiperItem>
  183. <DayNightCard isNight={firstNight} count={props.count} />
  184. </SwiperItem>
  185. <SwiperItem>
  186. <DayNightCard isNight={!firstNight} count={props.count} />
  187. </SwiperItem>
  188. </Swiper>
  189. <AllRings data={props.homeData} time={timestamp} />
  190. {/* <AllDayRings schedule={props.schedule} /> */}
  191. <DayNightSwiperPopup authInfo={authInfo} />
  192. </View>
  193. }