DayNightRing.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import { View } from "@tarojs/components"
  2. import { bigRingRadius, getBgRing, getCommon, getDot, ringWidth, thirdRingRadius } from "../trackTimeDuration/hooks/RingData";
  3. import Rings from "@/features/trackTimeDuration/components/Rings";
  4. import { RealRing } from '@/features/trackTimeDuration/components/Rings'
  5. import { ColorType } from "@/context/themes/color";
  6. import { useEffect, useState } from "react";
  7. import { TimeFormatter } from "@/utils/time_format";
  8. import dayjs from 'dayjs'
  9. const utc = require('dayjs/plugin/utc')
  10. const timezone = require('dayjs/plugin/timezone')
  11. dayjs.extend(utc)
  12. dayjs.extend(timezone)
  13. export default function DayNightRing(props: {
  14. isNight: boolean,
  15. isThirdRing: boolean,
  16. canvasId: any,
  17. authInfo: any
  18. }) {
  19. const [authInfo, setAuthInfo] = useState(props.authInfo)
  20. useEffect(() => {
  21. setAuthInfo(props.authInfo)
  22. }, [props.authInfo])
  23. function dayRing() {
  24. var common = getCommon(null, true)
  25. common.radius = props.isThirdRing ? thirdRingRadius : bigRingRadius;
  26. common.lineWidth = ringWidth;
  27. var bgRing = getBgRing()
  28. let realRingBig: RealRing = {
  29. color: ColorType.day + '66',
  30. startArc: 0,
  31. durationArc: 2
  32. }
  33. var now = new Date().getTime()
  34. if (authInfo && authInfo.day_completed && now > authInfo.day_completed.sunset_ts) {
  35. realRingBig.color = ColorType.day
  36. var duration = (authInfo.day_completed.sunset_ts - authInfo.day_completed.sunrise_ts) / 1000
  37. var time = authInfo.day_completed.sunrise
  38. time = time.substring(time.length - 8, time.length)
  39. var start = new Date('2024-01-01T' + time)
  40. realRingBig.startArc = (start.getHours() * 3600 + start.getMinutes() * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  41. realRingBig.durationArc = (duration) / (24 * 3600) * 2 * Math.PI;
  42. return <Rings common={common} bgRing={bgRing} targetRing={realRingBig} canvasId={props.canvasId} />
  43. }
  44. var sunRise = 6 * 60
  45. var sunSet = 18 * 60
  46. if (global.dayObj) {
  47. sunRise = parseInt(global.dayObj.sunrise.time.split(':')[0]) * 60 + parseInt(global.dayObj.sunrise.time.split(':')[1])
  48. sunSet = parseInt(global.dayObj.sunset.time.split(':')[0]) * 60 + parseInt(global.dayObj.sunset.time.split(':')[1])
  49. }
  50. var duration = sunSet - sunRise
  51. if (global.dayObj) {
  52. duration = (global.dayObj.sunset.timestamp - global.dayObj.sunrise.timestamp) / 60000
  53. }
  54. realRingBig.startArc = (sunRise * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  55. realRingBig.durationArc = (duration * 60) / (24 * 3600) * 2 * Math.PI;
  56. var currentDot = getDot(null, false)
  57. if (authInfo && authInfo.timezone && authInfo.timezone.id) {
  58. var current1 = dayjs()
  59. var current = TimeFormatter.tzLocalTime(new Date().getTime(),authInfo.timezone.id)//dayjs().tz(authInfo.timezone.id)
  60. var offset = current.day() * 24 * 60 + current.hour() * 60 + current.minute() - current1.day() * 24 * 60 - current1.hour() * 60 - current1.minute()
  61. currentDot.offset = offset
  62. }
  63. var date = new Date(now)
  64. var minutes = date.getHours() * 60 + date.getMinutes()
  65. if (minutes < sunRise) {
  66. minutes += 1440
  67. }
  68. currentDot.color = ColorType.day
  69. var t = date.getHours() * 60 + date.getMinutes()
  70. var duration2 = t - sunRise
  71. if (duration2 < 0) {
  72. duration2 += 24 * 60
  73. }
  74. if (global.dayObj) {
  75. if (global.dayObj.sunrise.timestamp < now && global.dayObj.sunset.timestamp > now) {
  76. duration2 = (now - global.dayObj.sunrise.timestamp) / 60000
  77. }
  78. else {
  79. duration2 = -1
  80. }
  81. }
  82. var realRing: RealRing = {
  83. color: ColorType.day,
  84. startArc: (sunRise * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0,
  85. durationArc: (duration2 * 60) / (24 * 3600) * 2 * Math.PI
  86. }
  87. if (duration2 == -1) {
  88. realRing = null
  89. }
  90. return <Rings common={common} bgRing={bgRing} targetRing={realRingBig} realRing={realRing} currentDot={currentDot} canvasId={props.canvasId} />
  91. }
  92. function nightRing() {
  93. var common = getCommon(null, true)
  94. common.radius = props.isThirdRing ? thirdRingRadius : bigRingRadius;
  95. common.lineWidth = ringWidth;
  96. var bgRing = getBgRing()
  97. let realRingBig: RealRing = {
  98. color: ColorType.night + '66',
  99. startArc: 0,
  100. durationArc: 2
  101. }
  102. var currentDot = getDot(null, false)
  103. currentDot.color = ColorType.night
  104. if (authInfo && authInfo.timezone && authInfo.timezone.id) {
  105. var current1 = dayjs()
  106. var current = TimeFormatter.tzLocalTime(new Date().getTime(),authInfo.timezone.id)//dayjs().tz(authInfo.timezone.id)
  107. var offset = current.date() * 24 * 60 + current.hour() * 60 + current.minute() - current1.date() * 24 * 60 - current1.hour() * 60 - current1.minute()
  108. currentDot.offset = offset
  109. }
  110. var now1 = new Date().getTime()
  111. if (authInfo && authInfo.timezone) {
  112. now1 = TimeFormatter.transferTimestamp(now1, authInfo.timezone.gmt)
  113. }
  114. if (authInfo && authInfo.night_completed && new Date().getTime() > authInfo.night_completed.sunrise_ts) {
  115. var isCompleted = false;
  116. if (authInfo.day_completed && new Date().getTime() > authInfo.day_completed.sunset_ts) {
  117. isCompleted = true
  118. }
  119. realRingBig.color = ColorType.night
  120. var duration = (authInfo.night_completed.sunrise_ts - authInfo.night_completed.sunset_ts) / 1000
  121. var time = authInfo.night_completed.sunset
  122. time = time.substring(time.length - 8, time.length)
  123. var start = new Date('2024-01-01T' + time)
  124. realRingBig.startArc = (start.getHours() * 3600 + start.getMinutes() * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  125. realRingBig.durationArc = (duration) / (24 * 3600) * 2 * Math.PI;
  126. //已结束未更新暂停时,显示current_dot
  127. if (isCompleted) {
  128. currentDot = null
  129. }
  130. // if (!realRingBig.startArc){
  131. // realRingBig = null
  132. // }
  133. return <Rings common={common} bgRing={bgRing} targetRing={realRingBig} currentDot={currentDot} canvasId={props.canvasId} />
  134. }
  135. var sunRise = 24 * 60 + 6 * 60
  136. var sunSet = 18 * 60
  137. if (global.nightObj) {
  138. sunRise = 24 * 60 + parseInt(global.nightObj.sunrise.time.split(':')[0]) * 60 + parseInt(global.nightObj.sunrise.time.split(':')[1])
  139. sunSet = parseInt(global.nightObj.sunset.time.split(':')[0]) * 60 + parseInt(global.nightObj.sunset.time.split(':')[1])
  140. }
  141. var duration = sunRise - sunSet
  142. if (global.nightObj) {
  143. duration = (global.nightObj.sunrise.timestamp - global.nightObj.sunset.timestamp) / 60000
  144. // if (global.nightObj.sunrise.timestamp-new Date().getTime()>48*3600*1000){
  145. // duration = 24*60
  146. // }
  147. }
  148. realRingBig.startArc = (sunSet * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  149. realRingBig.durationArc = (duration * 60) / (24 * 3600) * 2 * Math.PI;
  150. var now = new Date()
  151. var t1 = now.getHours() * 60 + now.getMinutes()
  152. var duration2 = t1 - sunSet
  153. if (duration2 < 0) {
  154. duration2 += 24 * 60
  155. }
  156. if (global.nightObj) {
  157. if (global.nightObj.sunset.timestamp < now.getTime() && global.nightObj.sunrise.timestamp > now.getTime()) {
  158. duration2 = (now.getTime() - global.nightObj.sunset.timestamp) / 60000
  159. }
  160. else {
  161. duration2 = -1
  162. }
  163. }
  164. let realRing: RealRing = {
  165. color: ColorType.night,
  166. startArc: (sunSet * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0,
  167. durationArc: (duration2 * 60) / (24 * 3600) * 2 * Math.PI
  168. }
  169. if (duration2 == -1) {
  170. realRing = null;
  171. }
  172. // if (night.nightRingDate) {
  173. // if (new Date(night.nightRingDate).getDate() == now.getDate() && now.getHours() < 12) {
  174. // realRing = null;
  175. // }
  176. // }
  177. // if (!user.isLogin) {
  178. // currentDot = null;
  179. // realRing = null;
  180. // }
  181. return <Rings common={common} bgRing={bgRing} targetRing={realRingBig} realRing={duration2 > duration ? null : realRing} currentDot={currentDot} canvasId={props.canvasId} />
  182. }
  183. return props.isNight ? nightRing() : dayRing()
  184. }