WeekCalendarItem.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. export default function WeekCalendarItem(props: { data: any }) {
  7. const [charts, setCharts] = useState([])
  8. useEffect(() => {
  9. var array: any = []
  10. for (var i = 0; i < 7; i++) {
  11. var start = props.data.start + 24 * 3600 * 1000 * i
  12. var end = props.data.start + (24 * 3600 * 1000) * (i + 1)
  13. var fasts: any = []
  14. var sleeps: any = []
  15. array.push({
  16. start,
  17. end,
  18. fasts,
  19. sleeps
  20. })
  21. props.data.list.map((item) => {
  22. var isFast = item.scenario == 'FAST'
  23. var range = getIntersection(start, end, item.real_start_time, item.real_end_time)
  24. if (range) {
  25. var begin = (range[0] - start) / (24 * 3600 * 1000)
  26. var height = (range[1] - range[0]) / (24 * 3600 * 1000)
  27. if (isFast) {
  28. fasts.push({
  29. begin,
  30. height
  31. })
  32. }
  33. else {
  34. sleeps.push({
  35. begin, height
  36. })
  37. }
  38. }
  39. })
  40. }
  41. setCharts(array)
  42. }, [])
  43. function getIntersection(start1, end1, start2, end2) {
  44. // 确保 start1 小于等于 end1,start2 小于等于 end2
  45. if (start1 > end1) {
  46. [start1, end1] = [end1, start1];
  47. }
  48. if (start2 > end2) {
  49. [start2, end2] = [end2, start2];
  50. }
  51. // 计算相交的时间戳
  52. var intersectionStart = Math.max(start1, start2);
  53. var intersectionEnd = Math.min(end1, end2);
  54. // 检查是否存在相交时间戳
  55. if (intersectionStart <= intersectionEnd) {
  56. // 返回相交的时间戳
  57. return [intersectionStart, intersectionEnd];
  58. } else {
  59. // 不存在相交时间戳
  60. return null;
  61. }
  62. }
  63. return <View className="chart_content" style={{width:parseInt(rpxToPx(658)+'')}}>
  64. <View className="chart_top_week">
  65. <Text className="chart_week_text">周日</Text>
  66. <Text className="chart_week_text">周一</Text>
  67. <Text className="chart_week_text">周二</Text>
  68. <Text className="chart_week_text">周三</Text>
  69. <Text className="chart_week_text">周四</Text>
  70. <Text className="chart_week_text">周五</Text>
  71. <Text className="chart_week_text">周六</Text>
  72. </View>
  73. <View className="chart_detail">
  74. <View className="verticalLine" style={{ left: 0 }} />
  75. <View className="verticalLine" style={{ left: rpxToPx(94) }} />
  76. <View className="verticalLine" style={{ left: rpxToPx(94 * 2) }} />
  77. <View className="verticalLine" style={{ left: rpxToPx(94 * 3) }} />
  78. <View className="verticalLine" style={{ left: rpxToPx(94 * 4) }} />
  79. <View className="verticalLine" style={{ left: rpxToPx(94 * 5) }} />
  80. <View className="verticalLine" style={{ left: rpxToPx(94 * 6) }} />
  81. <View className="horizontalLine" style={{ top: rpxToPx(100), backgroundColor: '#262626' }} />
  82. <View className="horizontalLine" style={{ top: rpxToPx(200), backgroundColor: '#383838' }} />
  83. <View className="horizontalLine" style={{ top: rpxToPx(300), backgroundColor: '#262626' }} />
  84. {
  85. charts.map((item, index) => {
  86. return <View className="lineContent" style={{ left: rpxToPx(94 * index) }}>
  87. {
  88. (item as any).fasts.length > 0 &&
  89. <View className="lineBgView">
  90. {
  91. (item as any).fasts.map(obj => {
  92. return <View className="detailLine" style={{
  93. backgroundColor: ColorType.fast,
  94. top: rpxToPx(obj.begin * 400),
  95. height: rpxToPx(obj.height * 400)
  96. }} />
  97. })
  98. }
  99. </View>
  100. }
  101. {
  102. (item as any).sleeps.length > 0 &&
  103. <View className="lineBgView">
  104. {
  105. (item as any).sleeps.map(obj => {
  106. return <View className="detailLine" style={{
  107. backgroundColor: ColorType.sleep,
  108. top: rpxToPx(obj.begin * 400),
  109. height: rpxToPx(obj.height * 400)
  110. }} />
  111. })
  112. }
  113. </View>
  114. }
  115. </View>
  116. })
  117. }
  118. </View>
  119. <View className="chart_bottom_week">
  120. <Text className="chart_week_text">周一</Text>
  121. <Text className="chart_week_text">周二</Text>
  122. <Text className="chart_week_text">周三</Text>
  123. <Text className="chart_week_text">周四</Text>
  124. <Text className="chart_week_text">周五</Text>
  125. <Text className="chart_week_text">周六</Text>
  126. <Text className="chart_week_text">周日</Text>
  127. </View>
  128. </View>
  129. }