WeekCalendarItem.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. import { TimeFormatter } from "@/utils/time_format";
  7. import dayjs from 'dayjs'
  8. import showAlert from "@/components/basic/Alert";
  9. import { title } from "process";
  10. const utc = require('dayjs/plugin/utc')
  11. const timezone = require('dayjs/plugin/timezone')
  12. dayjs.extend(utc)
  13. dayjs.extend(timezone)
  14. let timer
  15. export default function WeekCalendarItem(props: { data: any, isCurrentWeek: boolean }) {
  16. const [charts, setCharts] = useState([])
  17. const [showCurrentTime, setShowCurrentTime] = useState(false)
  18. const [position, setPosition] = useState({ left: 0, top: 0 })
  19. useEffect(() => {
  20. if (props.isCurrentWeek) {
  21. if (timer)
  22. clearInterval(timer)
  23. if (props.isCurrentWeek) {
  24. timer = setInterval(() => {
  25. getPosition()
  26. }, 6000)
  27. getPosition()
  28. }
  29. setShowCurrentTime(props.isCurrentWeek)
  30. }
  31. else {
  32. if (timer)
  33. clearInterval(timer)
  34. }
  35. var array: any = []
  36. for (var i = 0; i < 7; i++) {
  37. var start = props.data.start + 24 * 3600 * 1000 * i
  38. var end = props.data.start + (24 * 3600 * 1000) * (i + 1)
  39. var fasts: any = []
  40. var sleeps: any = []
  41. array.push({
  42. start,
  43. end,
  44. fasts,
  45. sleeps
  46. })
  47. props.data.list.map((item, index) => {
  48. var isFast = item.scenario == 'FAST'
  49. var real_start = item.real_start_time
  50. var real_end = item.real_end_time
  51. if (item.real_end_timezone && item.real_end_timezone.id) {
  52. // var strEnd = dayjs(real_end).tz(item.real_end_timezone.id).format('YYYY-MM-DDTHH:mm:ss')
  53. var strEnd = TimeFormatter.tzTimeFormateLocalTime(real_end, item.real_end_timezone.id,'YYYY-MM-DDTHH:mm:ss')
  54. var duration = real_end - real_start
  55. // if (index = 1) {
  56. // if (new Date(strEnd)) {
  57. // // 示例字符串
  58. // var isoString = '2021-04-01T15:20:00';
  59. // // 将字符串转换为Date对象
  60. // var date = new Date(strEnd);
  61. // showAlert({
  62. // title: strEnd,
  63. // content: date.getTime()+''
  64. // })
  65. // }
  66. // else {
  67. // showAlert({
  68. // title: strEnd,
  69. // content: '转换失败'
  70. // })
  71. // }
  72. // }
  73. real_end = new Date(strEnd).getTime()
  74. real_start = real_end - duration
  75. }
  76. var range = getIntersection(start, end, real_start, real_end)
  77. if (range) {
  78. var begin = (range[0] - start) / (24 * 3600 * 1000)
  79. var height = (range[1] - range[0]) / (24 * 3600 * 1000)
  80. if (isFast) {
  81. fasts.push({
  82. begin,
  83. height
  84. })
  85. }
  86. else {
  87. sleeps.push({
  88. begin, height
  89. })
  90. }
  91. }
  92. })
  93. }
  94. setCharts(array)
  95. // if (array.length>0 && props.isCurrentWeek){
  96. // showAlert({
  97. // title:'111',
  98. // content:JSON.stringify(props.data)
  99. // })
  100. // }
  101. return () => {
  102. timer && clearInterval(timer)
  103. }
  104. }, [props.data])
  105. useEffect(() => {
  106. if (timer)
  107. clearInterval(timer)
  108. if (props.isCurrentWeek) {
  109. timer = setInterval(() => {
  110. getPosition()
  111. }, 60000)
  112. getPosition()
  113. }
  114. setShowCurrentTime(props.isCurrentWeek)
  115. }, [props.isCurrentWeek])
  116. function getPosition() {
  117. var now = new Date().getTime()
  118. var start = props.data.start
  119. var index = Math.floor((now - start) / (3600 * 24 * 1000))
  120. var left = (now - start) % (3600 * 24 * 1000)
  121. var top = left / (3600 * 24 * 1000) * 400
  122. if (top >= 398) {
  123. top = 398
  124. }
  125. setPosition({
  126. left: rpxToPx(94 * index),
  127. top: rpxToPx(top)
  128. })
  129. }
  130. function getIntersection(start1, end1, start2, end2) {
  131. // 确保 start1 小于等于 end1,start2 小于等于 end2
  132. if (start1 > end1) {
  133. [start1, end1] = [end1, start1];
  134. }
  135. if (start2 > end2) {
  136. [start2, end2] = [end2, start2];
  137. }
  138. // 计算相交的时间戳
  139. var intersectionStart = Math.max(start1, start2);
  140. var intersectionEnd = Math.min(end1, end2);
  141. // 检查是否存在相交时间戳
  142. if (intersectionStart <= intersectionEnd) {
  143. // 返回相交的时间戳
  144. return [intersectionStart, intersectionEnd];
  145. } else {
  146. // 不存在相交时间戳
  147. return null;
  148. }
  149. }
  150. function isTop() {
  151. var now = new Date()
  152. if (now.getHours() >= 12) {
  153. return true
  154. }
  155. return false
  156. }
  157. function weekIndex() {
  158. var week = new Date().getDay()
  159. if (!isTop()) {
  160. return week - 1
  161. }
  162. return week
  163. }
  164. return <View className="chart_content" style={{ width: parseInt(rpxToPx(658) + '') }}>
  165. <View style={{ height: parseInt(rpxToPx(60) + '') }}>
  166. <View className="chart_top_week">
  167. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(0) + ''), opacity: props.isCurrentWeek && isTop() && weekIndex() == 0 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(0)}</Text>
  168. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 1) + ''), opacity: props.isCurrentWeek && isTop() && weekIndex() == 1 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(1)}</Text>
  169. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 2) + ''), opacity: props.isCurrentWeek && isTop() && weekIndex() == 2 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(2)}</Text>
  170. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 3) + ''), opacity: props.isCurrentWeek && isTop() && weekIndex() == 3 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(3)}</Text>
  171. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 4) + ''), opacity: props.isCurrentWeek && isTop() && weekIndex() == 4 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(4)}</Text>
  172. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 5) + ''), opacity: props.isCurrentWeek && isTop() && weekIndex() == 5 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(5)}</Text>
  173. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 6) + ''), opacity: props.isCurrentWeek && isTop() && weekIndex() == 6 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(6)}</Text>
  174. </View>
  175. </View>
  176. <View className="chart_detail">
  177. <View className="verticalLine" style={{ left: 0 }} />
  178. <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 1) + '') }} />
  179. <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 2) + '') }} />
  180. <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 3) + '') }} />
  181. <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 4) + '') }} />
  182. <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 5) + '') }} />
  183. <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 6) + '') }} />
  184. <View className="horizontalLine" style={{ top: rpxToPx(100), backgroundColor: '#262626' }} />
  185. <View className="horizontalLine" style={{ top: rpxToPx(200), backgroundColor: '#383838' }} />
  186. <View className="horizontalLine" style={{ top: rpxToPx(300), backgroundColor: '#262626' }} />
  187. {
  188. charts.map((item, index) => {
  189. return <View className="lineContent" key={index * 9527} style={{ left: rpxToPx(94 * index) }}>
  190. {
  191. (item as any).fasts.length > 0 &&
  192. <View className="lineBgView">
  193. {
  194. (item as any).fasts.map((obj, k) => {
  195. return <View key={k} className="detailLine" style={{
  196. backgroundColor: ColorType.fast,
  197. top: rpxToPx(obj.begin * 400),
  198. height: rpxToPx(obj.height * 400)
  199. }} />
  200. })
  201. }
  202. </View>
  203. }
  204. {
  205. (item as any).sleeps.length > 0 &&
  206. <View className="lineBgView">
  207. {
  208. (item as any).sleeps.map((obj, j) => {
  209. return <View key={j} className="detailLine" style={{
  210. backgroundColor: ColorType.sleep,
  211. top: rpxToPx(obj.begin * 400),
  212. height: rpxToPx(obj.height * 400)
  213. }} />
  214. })
  215. }
  216. </View>
  217. }
  218. </View>
  219. })
  220. }
  221. {
  222. showCurrentTime && <View className="currentLine" style={{
  223. position: 'absolute',
  224. left: position.left,
  225. top: position.top,
  226. width: rpxToPx(94 - 1),
  227. backgroundColor: 'red'
  228. }}>
  229. {/* <View className={position.top < rpxToPx(400)-40 ? 'currentTime currentTimeBottom' : 'currentTime currentTimeTop'}>{TimeFormatter.timelineFormatTime(new Date().getTime())}</View> */}
  230. </View>
  231. }
  232. </View>
  233. <View style={{ height: parseInt(rpxToPx(60) + '') }}>
  234. <View className="chart_bottom_week">
  235. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(0) + ''), opacity: props.isCurrentWeek && !isTop() && weekIndex() == 0 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(1)}</Text>
  236. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 1) + ''), opacity: props.isCurrentWeek && !isTop() && weekIndex() == 1 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(2)}</Text>
  237. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 2) + ''), opacity: props.isCurrentWeek && !isTop() && weekIndex() == 2 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(3)}</Text>
  238. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 3) + ''), opacity: props.isCurrentWeek && !isTop() && weekIndex() == 3 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(4)}</Text>
  239. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 4) + ''), opacity: props.isCurrentWeek && !isTop() && weekIndex() == 4 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(5)}</Text>
  240. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 5) + ''), opacity: props.isCurrentWeek && !isTop() && weekIndex() == 5 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(6)}</Text>
  241. <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 6) + ''), opacity: props.isCurrentWeek && !isTop() && weekIndex() == -1 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(0)}</Text>
  242. </View>
  243. </View>
  244. </View>
  245. }