WeekCalendarItem.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { View, Text } from "@tarojs/components";
  2. import './WeekCalendarItem.scss'
  3. import { rpxToPx } from "@/utils/tools";
  4. import { useEffect, useState } from "react";
  5. import { ColorType } from "@/context/themes/color";
  6. let timer
  7. export default function WeekCalendarItem(props: { data: any, isCurrentWeek: boolean }) {
  8. const [charts, setCharts] = useState([])
  9. const [showCurrentTime, setShowCurrentTime] = useState(false)
  10. const [position, setPosition] = useState({ left: 0, top: 0 })
  11. useEffect(() => {
  12. var array: any = []
  13. for (var i = 0; i < 7; i++) {
  14. var start = props.data.start + 24 * 3600 * 1000 * i
  15. var end = props.data.start + (24 * 3600 * 1000) * (i + 1)
  16. var fasts: any = []
  17. var sleeps: any = []
  18. array.push({
  19. start,
  20. end,
  21. fasts,
  22. sleeps
  23. })
  24. props.data.list.map((item) => {
  25. var isFast = item.scenario == 'FAST'
  26. var range = getIntersection(start, end, item.real_start_time, item.real_end_time)
  27. if (range) {
  28. var begin = (range[0] - start) / (24 * 3600 * 1000)
  29. var height = (range[1] - range[0]) / (24 * 3600 * 1000)
  30. if (isFast) {
  31. fasts.push({
  32. begin,
  33. height
  34. })
  35. }
  36. else {
  37. sleeps.push({
  38. begin, height
  39. })
  40. }
  41. }
  42. })
  43. }
  44. setCharts(array)
  45. return ()=>{
  46. timer && clearInterval(timer)
  47. }
  48. }, [])
  49. useEffect(() => {
  50. if (timer)
  51. clearInterval(timer)
  52. if (props.isCurrentWeek) {
  53. timer = setInterval(()=>{
  54. getPosition()
  55. },60000)
  56. getPosition()
  57. }
  58. setShowCurrentTime(props.isCurrentWeek)
  59. }, [props.isCurrentWeek])
  60. function getPosition() {
  61. var now = new Date().getTime()
  62. var start = props.data.start
  63. var index = Math.floor((now - start) / (3600 * 24 * 1000))
  64. var left = (now - start) % (3600 * 24 * 1000)
  65. var top = left / (3600 * 24 * 1000) * 400
  66. setPosition({
  67. left: rpxToPx(94 * index),
  68. top: rpxToPx(top)
  69. })
  70. }
  71. function getIntersection(start1, end1, start2, end2) {
  72. // 确保 start1 小于等于 end1,start2 小于等于 end2
  73. if (start1 > end1) {
  74. [start1, end1] = [end1, start1];
  75. }
  76. if (start2 > end2) {
  77. [start2, end2] = [end2, start2];
  78. }
  79. // 计算相交的时间戳
  80. var intersectionStart = Math.max(start1, start2);
  81. var intersectionEnd = Math.min(end1, end2);
  82. // 检查是否存在相交时间戳
  83. if (intersectionStart <= intersectionEnd) {
  84. // 返回相交的时间戳
  85. return [intersectionStart, intersectionEnd];
  86. } else {
  87. // 不存在相交时间戳
  88. return null;
  89. }
  90. }
  91. return <View className="chart_content" style={{ width: parseInt(rpxToPx(658) + '') }}>
  92. <View style={{ height: parseInt(rpxToPx(60) + '') }}>
  93. <View className="chart_top_week">
  94. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(0) + '') }}>周日</Text>
  95. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 1) + '') }}>周一</Text>
  96. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 2) + '') }}>周二</Text>
  97. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 3) + '') }}>周三</Text>
  98. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 4) + '') }}>周四</Text>
  99. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 5) + '') }}>周五</Text>
  100. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 6) + '') }}>周六</Text>
  101. </View>
  102. </View>
  103. <View className="chart_detail">
  104. {
  105. showCurrentTime && <View style={{
  106. position: 'absolute',
  107. left: position.left,
  108. top: position.top,
  109. width: rpxToPx(94),
  110. height: 1,
  111. backgroundColor: 'red'
  112. }} />
  113. }
  114. <View className="verticalLine" style={{ left: 0 }} />
  115. <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 1) + '') }} />
  116. <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 2) + '') }} />
  117. <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 3) + '') }} />
  118. <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 4) + '') }} />
  119. <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 5) + '') }} />
  120. <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 6) + '') }} />
  121. <View className="horizontalLine" style={{ top: rpxToPx(100), backgroundColor: '#262626' }} />
  122. <View className="horizontalLine" style={{ top: rpxToPx(200), backgroundColor: '#383838' }} />
  123. <View className="horizontalLine" style={{ top: rpxToPx(300), backgroundColor: '#262626' }} />
  124. {
  125. charts.map((item, index) => {
  126. return <View className="lineContent" style={{ left: rpxToPx(94 * index) }}>
  127. {
  128. (item as any).fasts.length > 0 &&
  129. <View className="lineBgView">
  130. {
  131. (item as any).fasts.map(obj => {
  132. return <View className="detailLine" style={{
  133. backgroundColor: ColorType.fast,
  134. top: rpxToPx(obj.begin * 400),
  135. height: rpxToPx(obj.height * 400)
  136. }} />
  137. })
  138. }
  139. </View>
  140. }
  141. {
  142. (item as any).sleeps.length > 0 &&
  143. <View className="lineBgView">
  144. {
  145. (item as any).sleeps.map(obj => {
  146. return <View className="detailLine" style={{
  147. backgroundColor: ColorType.sleep,
  148. top: rpxToPx(obj.begin * 400),
  149. height: rpxToPx(obj.height * 400)
  150. }} />
  151. })
  152. }
  153. </View>
  154. }
  155. </View>
  156. })
  157. }
  158. </View>
  159. <View style={{ height: parseInt(rpxToPx(60) + '') }}>
  160. <View className="chart_bottom_week">
  161. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(0) + '') }}>周一</Text>
  162. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 1) + '') }}>周二</Text>
  163. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 2) + '') }}>周三</Text>
  164. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 3) + '') }}>周四</Text>
  165. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 4) + '') }}>周五</Text>
  166. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 5) + '') }}>周六</Text>
  167. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 6) + '') }}>周日</Text>
  168. </View>
  169. </View>
  170. </View>
  171. }