MainSleepActiveCard.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { View, Text } from "@tarojs/components";
  2. import './MainCard.scss'
  3. import { useState } from "react";
  4. import { rpxToPx } from "@/utils/tools";
  5. import Rings, { RingCommon, BgRing, TargetRing, CurrentDot } from "@/features/trackTimeDuration/components/Rings";
  6. import dayjs from "dayjs";
  7. import moment from 'moment-timezone'
  8. import { MainColorType } from "@/context/themes/color";
  9. export default function MainSleepActiveCard() {
  10. const [isSleep, setIsSleep] = useState(true)
  11. const [isSleepMode, setIsSleepMode] = useState(true)
  12. const startScheduleTime = '19:00'
  13. const endScheduleTime = '06:00'
  14. function formatTime(format: string, timestamp?: number) {
  15. return dayjs().format(format)
  16. }
  17. function switchMode() {
  18. setIsSleepMode(!isSleepMode)
  19. }
  20. const common: RingCommon = {
  21. useCase: 'ChooseScenario',
  22. radius: 115,
  23. lineWidth: 26,
  24. isFast: true,
  25. status: 'WAIT_FOR_START'
  26. }
  27. const bgRing: BgRing = {
  28. color: '#EAE9E9'
  29. }
  30. function targetRing() {
  31. var starts: any = startScheduleTime.split(':')
  32. var ends: any = endScheduleTime.split(':')
  33. const startSeconds: any = parseInt(starts[0] + '') * 60 + parseInt(starts[1] + '')
  34. const endSeconds: any = parseInt(ends[0] + '') * 60 + parseInt(ends[1] + '')
  35. const color = isSleepMode ? '#D0C0FB' : '#FF498366'
  36. const startArc = isSleepMode ? startSeconds / 1440 * 2 * Math.PI - Math.PI / 2 : endSeconds / 1440 * 2 * Math.PI - Math.PI / 2
  37. const fastCount = endSeconds - startSeconds > 0 ? endSeconds - startSeconds : endSeconds - startSeconds + 1440
  38. const eatCount = 1440 - fastCount
  39. const durationArc = isSleepMode ? fastCount / 1440 * 2 * Math.PI : eatCount / 1440 * 2 * Math.PI
  40. return {
  41. color,
  42. startArc,
  43. durationArc
  44. }
  45. }
  46. function realRing() {
  47. if (isSleepMode) {
  48. if (isCurrentTimeInRange( startScheduleTime,endScheduleTime)) {
  49. var starts: any = startScheduleTime.split(':')
  50. const startSeconds = parseInt(starts[0] + '') * 60 + parseInt(starts[1] + '')
  51. const color = MainColorType.sleep
  52. const startArc = startSeconds / 1440 * 2 * Math.PI - Math.PI / 2
  53. var endSeconds = new Date().getHours() * 60 + new Date().getMinutes()
  54. const fastCount = endSeconds - startSeconds > 0 ? endSeconds - startSeconds : endSeconds - startSeconds + 1440
  55. const durationArc = fastCount / 1440 * 2 * Math.PI
  56. return {
  57. color,
  58. startArc,
  59. durationArc
  60. }
  61. }
  62. }
  63. else {
  64. if (isCurrentTimeInRange(endScheduleTime, startScheduleTime)) {
  65. var starts: any = endScheduleTime.split(':')
  66. const startSeconds = parseInt(starts[0] + '') * 60 + parseInt(starts[1] + '')
  67. const color = MainColorType.active
  68. const startArc = startSeconds / 1440 * 2 * Math.PI - Math.PI / 2
  69. var endSeconds = new Date().getHours() * 60 + new Date().getMinutes()
  70. const fastCount = endSeconds - startSeconds > 0 ? endSeconds - startSeconds : endSeconds - startSeconds + 1440
  71. const durationArc = fastCount / 1440 * 2 * Math.PI
  72. return {
  73. color,
  74. startArc,
  75. durationArc
  76. }
  77. }
  78. }
  79. }
  80. function ring() {
  81. var offset = 0
  82. var hour = new Date().getHours()
  83. var minute = new Date().getMinutes()
  84. if (hour != new Date().getHours() || minute != new Date().getMinutes()) {
  85. offset = hour * 60 + minute - new Date().getHours() * 60 - new Date().getMinutes()
  86. }
  87. const currentDot: CurrentDot = {
  88. color: isSleepMode ? MainColorType.sleep : MainColorType.active,
  89. lineWidth: 10,
  90. borderColor: '#fff',
  91. offset: offset,
  92. whiteIcon: true
  93. }
  94. return <Rings common={common} bgRing={bgRing} targetRing={targetRing()} realRing={realRing()} currentDot={currentDot} canvasId={'smal11lee'} />
  95. }
  96. function isCurrentTimeInRange(start, end) {
  97. // 获取当前时间
  98. const now = new Date();
  99. const currentTime = now.getHours() * 60 + now.getMinutes(); // 当前时间转换为分钟
  100. // 将时间点转换为分钟
  101. const [startHour, startMinute] = start.split(':').map(Number);
  102. const [endHour, endMinute] = end.split(':').map(Number);
  103. const startTime = startHour * 60 + startMinute; // 开始时间转换为分钟
  104. const endTime = endHour * 60 + endMinute; // 结束时间转换为分钟
  105. // 处理跨日的情况
  106. if (endTime < startTime) {
  107. return currentTime >= startTime || currentTime <= endTime;
  108. }
  109. // 判断当前时间是否在范围内
  110. return currentTime >= startTime && currentTime <= endTime;
  111. }
  112. return <View style={{ width: rpxToPx(750), display: 'flex', flexShrink: 0, flexDirection: 'column', alignItems: 'center' }}>
  113. <View>Sleep Active</View>
  114. <View style={{ position: 'relative', }}>
  115. {
  116. ring()
  117. }
  118. <View className="ring_center">
  119. <Text className="time1">{formatTime('HH:mm:ss')}</Text>
  120. <Text className="date1">{global.language == 'en' ? formatTime('dddd, MMM D') : formatTime('MMMD日 dddd')}</Text>
  121. </View>
  122. </View>
  123. <Text onClick={switchMode}>Switch</Text>
  124. </View>
  125. }