DayNightRing.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. // if (!realRingBig.startArc){
  43. // realRingBig = null
  44. // }
  45. debugger
  46. return <Rings common={common} bgRing={bgRing} targetRing={realRingBig} canvasId={props.canvasId} />
  47. }
  48. var sunRise = 6 * 60
  49. var sunSet = 18 * 60
  50. if (global.dayObj) {
  51. sunRise = parseInt(global.dayObj.sunrise.time.split(':')[0]) * 60 + parseInt(global.dayObj.sunrise.time.split(':')[1])
  52. sunSet = parseInt(global.dayObj.sunset.time.split(':')[0]) * 60 + parseInt(global.dayObj.sunset.time.split(':')[1])
  53. }
  54. var duration = sunSet - sunRise
  55. if (global.dayObj) {
  56. duration = (global.dayObj.sunset.timestamp - global.dayObj.sunrise.timestamp) / 60000
  57. }
  58. realRingBig.startArc = (sunRise * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  59. realRingBig.durationArc = (duration * 60) / (24 * 3600) * 2 * Math.PI;
  60. var currentDot = getDot(null, false)
  61. if (authInfo && authInfo.timezone && authInfo.timezone.id) {
  62. var current1 = dayjs()
  63. var current = TimeFormatter.tzLocalTime(new Date().getTime(),authInfo.timezone.id)//dayjs().tz(authInfo.timezone.id)
  64. var offset = current.day() * 24 * 60 + current.hour() * 60 + current.minute() - current1.day() * 24 * 60 - current1.hour() * 60 - current1.minute()
  65. currentDot.offset = offset
  66. }
  67. var date = new Date(now)
  68. var minutes = date.getHours() * 60 + date.getMinutes()
  69. if (minutes < sunRise) {
  70. minutes += 1440
  71. }
  72. currentDot.color = ColorType.day
  73. var t = date.getHours() * 60 + date.getMinutes()
  74. var duration2 = t - sunRise
  75. if (duration2 < 0) {
  76. duration2 += 24 * 60
  77. }
  78. if (global.dayObj) {
  79. if (global.dayObj.sunrise.timestamp < now && global.dayObj.sunset.timestamp > now) {
  80. duration2 = (now - global.dayObj.sunrise.timestamp) / 60000
  81. }
  82. else {
  83. duration2 = -1
  84. }
  85. }
  86. var realRing: RealRing = {
  87. color: ColorType.day,
  88. startArc: (sunRise * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0,
  89. durationArc: (duration2 * 60) / (24 * 3600) * 2 * Math.PI
  90. }
  91. if (duration2 == -1) {
  92. realRing = null
  93. }
  94. return <Rings common={common} bgRing={bgRing} targetRing={realRingBig} realRing={realRing} currentDot={currentDot} canvasId={props.canvasId} />
  95. }
  96. function nightRing() {
  97. var common = getCommon(null, true)
  98. common.radius = props.isThirdRing ? thirdRingRadius : bigRingRadius;
  99. common.lineWidth = ringWidth;
  100. var bgRing = getBgRing()
  101. let realRingBig: RealRing = {
  102. color: ColorType.night + '66',
  103. startArc: 0,
  104. durationArc: 2
  105. }
  106. var currentDot = getDot(null, false)
  107. currentDot.color = ColorType.night
  108. if (authInfo && authInfo.timezone && authInfo.timezone.id) {
  109. var current1 = dayjs()
  110. var current = TimeFormatter.tzLocalTime(new Date().getTime(),authInfo.timezone.id)//dayjs().tz(authInfo.timezone.id)
  111. var offset = current.date() * 24 * 60 + current.hour() * 60 + current.minute() - current1.date() * 24 * 60 - current1.hour() * 60 - current1.minute()
  112. currentDot.offset = offset
  113. }
  114. var now1 = new Date().getTime()
  115. if (authInfo && authInfo.timezone) {
  116. now1 = TimeFormatter.transferTimestamp(now1, authInfo.timezone.gmt)
  117. }
  118. if (authInfo && authInfo.night_completed && new Date().getTime() > authInfo.night_completed.sunrise_ts) {
  119. var isCompleted = false;
  120. if (authInfo.day_completed && new Date().getTime() > authInfo.day_completed.sunset_ts) {
  121. isCompleted = true
  122. }
  123. realRingBig.color = ColorType.night
  124. var duration = (authInfo.night_completed.sunrise_ts - authInfo.night_completed.sunset_ts) / 1000
  125. var time = authInfo.night_completed.sunset
  126. time = time.substring(time.length - 8, time.length)
  127. var start = new Date('2024-01-01T' + time)
  128. realRingBig.startArc = (start.getHours() * 3600 + start.getMinutes() * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  129. realRingBig.durationArc = (duration) / (24 * 3600) * 2 * Math.PI;
  130. //已结束未更新暂停时,显示current_dot
  131. if (isCompleted) {
  132. currentDot = null
  133. }
  134. // if (!realRingBig.startArc){
  135. // realRingBig = null
  136. // }
  137. return <Rings common={common} bgRing={bgRing} targetRing={realRingBig} currentDot={currentDot} canvasId={props.canvasId} />
  138. }
  139. var sunRise = 24 * 60 + 6 * 60
  140. var sunSet = 18 * 60
  141. if (global.nightObj) {
  142. sunRise = 24 * 60 + parseInt(global.nightObj.sunrise.time.split(':')[0]) * 60 + parseInt(global.nightObj.sunrise.time.split(':')[1])
  143. sunSet = parseInt(global.nightObj.sunset.time.split(':')[0]) * 60 + parseInt(global.nightObj.sunset.time.split(':')[1])
  144. }
  145. var duration = sunRise - sunSet
  146. if (global.nightObj) {
  147. duration = (global.nightObj.sunrise.timestamp - global.nightObj.sunset.timestamp) / 60000
  148. // if (global.nightObj.sunrise.timestamp-new Date().getTime()>48*3600*1000){
  149. // duration = 24*60
  150. // }
  151. }
  152. realRingBig.startArc = (sunSet * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0;
  153. realRingBig.durationArc = (duration * 60) / (24 * 3600) * 2 * Math.PI;
  154. var now = new Date()
  155. var t1 = now.getHours() * 60 + now.getMinutes()
  156. var duration2 = t1 - sunSet
  157. if (duration2 < 0) {
  158. duration2 += 24 * 60
  159. }
  160. if (global.nightObj) {
  161. if (global.nightObj.sunset.timestamp < now.getTime() && global.nightObj.sunrise.timestamp > now.getTime()) {
  162. duration2 = (now.getTime() - global.nightObj.sunset.timestamp) / 60000
  163. }
  164. else {
  165. duration2 = -1
  166. }
  167. }
  168. let realRing: RealRing = {
  169. color: ColorType.night,
  170. startArc: (sunSet * 60) / (24 * 3600) * 2 * Math.PI - Math.PI / 2.0,
  171. durationArc: (duration2 * 60) / (24 * 3600) * 2 * Math.PI
  172. }
  173. if (duration2 == -1) {
  174. realRing = null;
  175. }
  176. // if (night.nightRingDate) {
  177. // if (new Date(night.nightRingDate).getDate() == now.getDate() && now.getHours() < 12) {
  178. // realRing = null;
  179. // }
  180. // }
  181. // if (!user.isLogin) {
  182. // currentDot = null;
  183. // realRing = null;
  184. // }
  185. return <Rings common={common} bgRing={bgRing} targetRing={realRingBig} realRing={duration2 > duration ? null : realRing} currentDot={currentDot} canvasId={props.canvasId} />
  186. }
  187. return props.isNight ? nightRing() : dayRing()
  188. }