| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- import { View, Text } from "@tarojs/components";
- import './WeekCalendarItem.scss'
- import { rpxToPx } from "@/utils/tools";
- import { useEffect, useState } from "react";
- import { ColorType } from "@/context/themes/color";
- import { TimeFormatter } from "@/utils/time_format";
- import dayjs from 'dayjs'
- import showAlert from "@/components/basic/Alert";
- import { title } from "process";
- const utc = require('dayjs/plugin/utc')
- const timezone = require('dayjs/plugin/timezone')
- dayjs.extend(utc)
- dayjs.extend(timezone)
- let timer
- export default function WeekCalendarItem(props: { data: any, isCurrentWeek: boolean }) {
- const [charts, setCharts] = useState([])
- const [showCurrentTime, setShowCurrentTime] = useState(false)
- const [position, setPosition] = useState({ left: 0, top: 0 })
- useEffect(() => {
- if (props.isCurrentWeek) {
- if (timer)
- clearInterval(timer)
- if (props.isCurrentWeek) {
- timer = setInterval(() => {
- getPosition()
- }, 6000)
- getPosition()
- }
- setShowCurrentTime(props.isCurrentWeek)
- }
- else {
- if (timer)
- clearInterval(timer)
- }
- var array: any = []
- for (var i = 0; i < 7; i++) {
- var start = props.data.start + 24 * 3600 * 1000 * i
- var end = props.data.start + (24 * 3600 * 1000) * (i + 1)
- var fasts: any = []
- var sleeps: any = []
- array.push({
- start,
- end,
- fasts,
- sleeps
- })
- props.data.list.map((item, index) => {
- var isFast = item.scenario == 'FAST'
- var real_start = item.real_start_time
- var real_end = item.real_end_time
- if (item.real_end_timezone && item.real_end_timezone.id) {
- // var strEnd = dayjs(real_end).tz(item.real_end_timezone.id).format('YYYY-MM-DDTHH:mm:ss')
- var strEnd = TimeFormatter.tzTimeFormateLocalTime(real_end, item.real_end_timezone.id,'YYYY-MM-DDTHH:mm:ss')
- var duration = real_end - real_start
- // if (index = 1) {
- // if (new Date(strEnd)) {
- // // 示例字符串
- // var isoString = '2021-04-01T15:20:00';
- // // 将字符串转换为Date对象
- // var date = new Date(strEnd);
- // showAlert({
- // title: strEnd,
- // content: date.getTime()+''
- // })
- // }
- // else {
- // showAlert({
- // title: strEnd,
- // content: '转换失败'
- // })
- // }
- // }
- real_end = new Date(strEnd).getTime()
- real_start = real_end - duration
- }
- var range = getIntersection(start, end, real_start, real_end)
- if (range) {
- var begin = (range[0] - start) / (24 * 3600 * 1000)
- var height = (range[1] - range[0]) / (24 * 3600 * 1000)
- if (isFast) {
- fasts.push({
- begin,
- height
- })
- }
- else {
- sleeps.push({
- begin, height
- })
- }
- }
- })
- }
- setCharts(array)
- // if (array.length>0 && props.isCurrentWeek){
- // showAlert({
- // title:'111',
- // content:JSON.stringify(props.data)
- // })
- // }
- return () => {
- timer && clearInterval(timer)
- }
- }, [props.data])
- useEffect(() => {
- if (timer)
- clearInterval(timer)
- if (props.isCurrentWeek) {
- timer = setInterval(() => {
- getPosition()
- }, 60000)
- getPosition()
- }
- setShowCurrentTime(props.isCurrentWeek)
- }, [props.isCurrentWeek])
- function getPosition() {
- var now = new Date().getTime()
- var start = props.data.start
- var index = Math.floor((now - start) / (3600 * 24 * 1000))
- var left = (now - start) % (3600 * 24 * 1000)
- var top = left / (3600 * 24 * 1000) * 400
- if (top >= 398) {
- top = 398
- }
- setPosition({
- left: rpxToPx(94 * index),
- top: rpxToPx(top)
- })
- }
- function getIntersection(start1, end1, start2, end2) {
- // 确保 start1 小于等于 end1,start2 小于等于 end2
- if (start1 > end1) {
- [start1, end1] = [end1, start1];
- }
- if (start2 > end2) {
- [start2, end2] = [end2, start2];
- }
- // 计算相交的时间戳
- var intersectionStart = Math.max(start1, start2);
- var intersectionEnd = Math.min(end1, end2);
- // 检查是否存在相交时间戳
- if (intersectionStart <= intersectionEnd) {
- // 返回相交的时间戳
- return [intersectionStart, intersectionEnd];
- } else {
- // 不存在相交时间戳
- return null;
- }
- }
- function isTop() {
- var now = new Date()
- if (now.getHours() >= 12) {
- return true
- }
- return false
- }
- function weekIndex() {
- var week = new Date().getDay()
- if (!isTop()) {
- return week - 1
- }
- return week
- }
- return <View className="chart_content" style={{ width: parseInt(rpxToPx(658) + '') }}>
- <View style={{ height: parseInt(rpxToPx(60) + '') }}>
- <View className="chart_top_week">
- <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(0) + ''), opacity: props.isCurrentWeek && isTop() && weekIndex() == 0 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(0)}</Text>
- <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 1) + ''), opacity: props.isCurrentWeek && isTop() && weekIndex() == 1 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(1)}</Text>
- <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 2) + ''), opacity: props.isCurrentWeek && isTop() && weekIndex() == 2 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(2)}</Text>
- <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 3) + ''), opacity: props.isCurrentWeek && isTop() && weekIndex() == 3 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(3)}</Text>
- <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 4) + ''), opacity: props.isCurrentWeek && isTop() && weekIndex() == 4 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(4)}</Text>
- <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 5) + ''), opacity: props.isCurrentWeek && isTop() && weekIndex() == 5 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(5)}</Text>
- <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 6) + ''), opacity: props.isCurrentWeek && isTop() && weekIndex() == 6 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(6)}</Text>
- </View>
- </View>
- <View className="chart_detail">
- <View className="verticalLine" style={{ left: 0 }} />
- <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 1) + '') }} />
- <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 2) + '') }} />
- <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 3) + '') }} />
- <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 4) + '') }} />
- <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 5) + '') }} />
- <View className="verticalLine" style={{ left: parseInt(rpxToPx(94 * 6) + '') }} />
- <View className="horizontalLine" style={{ top: rpxToPx(100), backgroundColor: '#262626' }} />
- <View className="horizontalLine" style={{ top: rpxToPx(200), backgroundColor: '#383838' }} />
- <View className="horizontalLine" style={{ top: rpxToPx(300), backgroundColor: '#262626' }} />
- {
- charts.map((item, index) => {
- return <View className="lineContent" key={index * 9527} style={{ left: rpxToPx(94 * index) }}>
- {
- (item as any).fasts.length > 0 &&
- <View className="lineBgView">
- {
- (item as any).fasts.map((obj, k) => {
- return <View key={k} className="detailLine" style={{
- backgroundColor: ColorType.fast,
- top: rpxToPx(obj.begin * 400),
- height: rpxToPx(obj.height * 400)
- }} />
- })
- }
- </View>
- }
- {
- (item as any).sleeps.length > 0 &&
- <View className="lineBgView">
- {
- (item as any).sleeps.map((obj, j) => {
- return <View key={j} className="detailLine" style={{
- backgroundColor: ColorType.sleep,
- top: rpxToPx(obj.begin * 400),
- height: rpxToPx(obj.height * 400)
- }} />
- })
- }
- </View>
- }
- </View>
- })
- }
- {
- showCurrentTime && <View className="currentLine" style={{
- position: 'absolute',
- left: position.left,
- top: position.top,
- width: rpxToPx(94 - 1),
- backgroundColor: 'red'
- }}>
- {/* <View className={position.top < rpxToPx(400)-40 ? 'currentTime currentTimeBottom' : 'currentTime currentTimeTop'}>{TimeFormatter.timelineFormatTime(new Date().getTime())}</View> */}
- </View>
- }
- </View>
- <View style={{ height: parseInt(rpxToPx(60) + '') }}>
- <View className="chart_bottom_week">
- <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(0) + ''), opacity: props.isCurrentWeek && !isTop() && weekIndex() == 0 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(1)}</Text>
- <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 1) + ''), opacity: props.isCurrentWeek && !isTop() && weekIndex() == 1 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(2)}</Text>
- <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 2) + ''), opacity: props.isCurrentWeek && !isTop() && weekIndex() == 2 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(3)}</Text>
- <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 3) + ''), opacity: props.isCurrentWeek && !isTop() && weekIndex() == 3 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(4)}</Text>
- <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 4) + ''), opacity: props.isCurrentWeek && !isTop() && weekIndex() == 4 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(5)}</Text>
- <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 5) + ''), opacity: props.isCurrentWeek && !isTop() && weekIndex() == 5 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(6)}</Text>
- <Text className="chart_week_text" style={{ left: parseInt(rpxToPx(94 * 6) + ''), opacity: props.isCurrentWeek && !isTop() && weekIndex() == -1 ? 1 : 0.4 }}>{TimeFormatter.getDayOfWeek(0)}</Text>
- </View>
- </View>
- </View>
- }
|