PerpetualCalendar.jsx 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
import React, { useState, useEffect } from 'react';
import { Typography, Select, Button, Tooltip } from 'antd';
import styled from 'styled-components';
import { getLunarDate } from '../js/lunarCalendar';
import calendarConfig from '../data/calendar-config.json';

const { Title } = Typography;
const { Option } = Select;

// 从配置文件中获取数据
const {
  monthNames,
  weekdayNames,
  yearRange,
  colors,
  solarHolidays,
  solarTerms,
  legalHolidays
} = calendarConfig;

// 样式
const CalendarContainer = styled.div`
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
  margin-top: 80px; /* 增加顶部边距,避免导航栏遮挡 */
`;

const CalendarHeader = styled.div`
  margin-bottom: 20px;
  padding-top: 20px;
`;

const ControlsContainer = styled.div`
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-bottom: 20px;
  align-items: center;
`;

const CalendarGrid = styled.div`
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 8px;
  margin: 20px 0;
`;

const DayCell = styled.div`
  height: 76px;
  border: 1px solid #eee;
  border-radius: 4px;
  padding: 6px;
  display: flex;
  flex-direction: column;
  position: relative;
  background-color: ${props => props.$isHoliday ? colors.holiday : props.$isToday ? colors.today : 'white'};
  cursor: pointer;
  
  &:hover {
    border-color: #1890ff;
  }
`;

const WeekdayHeader = styled.div`
  font-weight: bold;
  text-align: center;
  padding: 8px;
  color: ${props => props.$isWeekend ? colors.holidayText : 'inherit'};
`;

const DayNumber = styled.div`
  font-size: 1.1rem;
  font-weight: ${props => props.$isToday ? 'bold' : 'normal'};
  color: ${props => props.$isToday ? '#1890ff' : props.$isHoliday ? colors.holidayText : 'inherit'};
`;

const LunarDay = styled.div`
  font-size: 0.8rem;
  color: #888;
  margin-top: 2px;
`;

const HolidayTag = styled.div`
  font-size: 0.8rem;
  color: ${colors.holidayText};
  margin-top: 2px;
  font-weight: bold;
`;

const WorkTag = styled.div`
  position: absolute;
  top: 2px;
  right: 2px;
  width: 16px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: bold;
  border-radius: 2px;
`;

const PerpetualCalendar = () => {
  // 获取今天的日期
  const today = new Date();
  const [selectedYear, setSelectedYear] = useState(today.getFullYear());
  const [selectedMonth, setSelectedMonth] = useState(today.getMonth());
  const [calendarDays, setCalendarDays] = useState([]);

  // 生成年份选项
  const years = [];
  for (let year = yearRange.start; year <= yearRange.end; year++) {
    years.push(year);
  }
  
  // 处理月份变化
  const handleMonthChange = (value) => {
    setSelectedMonth(value);
  };

  // 处理年份变化
  const handleYearChange = (value) => {
    setSelectedYear(value);
  };

  // 跳转到今天
  const goToToday = () => {
    setSelectedYear(today.getFullYear());
    setSelectedMonth(today.getMonth());
  };

  // 上一个月
  const prevMonth = () => {
    if (selectedMonth === 0) {
      setSelectedYear(selectedYear - 1);
      setSelectedMonth(11);
    } else {
      setSelectedMonth(selectedMonth - 1);
    }
  };

  // 下一个月
  const nextMonth = () => {
    if (selectedMonth === 11) {
      setSelectedYear(selectedYear + 1);
      setSelectedMonth(0);
    } else {
      setSelectedMonth(selectedMonth + 1);
    }
  };

  // 判断是否是今天
  const isToday = (year, month, day) => {
    const todayDate = today.getDate();
    const todayMonth = today.getMonth();
    const todayYear = today.getFullYear();
    
    return day === todayDate && month === todayMonth && year === todayYear;
  };

  // 判断是否为法定节假日、节气或其他节日
  const getHolidayInfo = (year, month, day) => {
    const dateStr = `${year}${month < 9 ? '0' : ''}${month + 1}${day < 10 ? '0' : ''}${day}`;
    
    // 检查是否是法定节假日 - 按年份获取对应数据
    if (legalHolidays && legalHolidays[year] && legalHolidays[year][dateStr]) {
      return legalHolidays[year][dateStr];
    }
    
    // 检查是否是二十四节气 - 按年份获取对应数据
    if (solarTerms && solarTerms[year] && solarTerms[year][dateStr]) {
      return { name: solarTerms[year][dateStr], type: 'solarTerm' };
    }
    
    // 检查是否是阳历节日 - 这些不依赖于年份
    const monthDay = `${month < 9 ? '0' : ''}${month + 1}${day < 10 ? '0' : ''}${day}`;
    if (solarHolidays && solarHolidays[monthDay]) {
      return { 
        name: solarHolidays[monthDay].name, 
        type: solarHolidays[monthDay].isHoliday ? 'holiday' : 'festival' 
      };
    }
    
    return null;
  };

  // 判断是否是实际休息日(考虑周末调休)
  const isActualRestDay = (dateObj) => {
    const { holiday, date } = dateObj;
    const dayOfWeek = date.getDay();
    const isWeekend = dayOfWeek === 0 || dayOfWeek === 6;
    
    // 节假日安排优先级最高
    if (holiday) {
      if (holiday.type === 'holiday') {
        return true;  // 法定假日休息
      } else if (holiday.type === 'workday') {
        return false; // 调休工作日要上班
      }
    }
    
    // 其次是周末自然休息
    return isWeekend;
  };

  // 生成月视图的日期
  const generateMonthDays = () => {
    const firstDayOfMonth = new Date(selectedYear, selectedMonth, 1);
    const lastDayOfMonth = new Date(selectedYear, selectedMonth + 1, 0);
    const daysInMonth = lastDayOfMonth.getDate();
    const startingDayOfWeek = firstDayOfMonth.getDay(); // 0 = 星期日
    
    // 记录当月的所有日期和上下月填充的日期
    const days = [];
    
    // 上个月的填充日期
    const prevMonthLastDay = new Date(selectedYear, selectedMonth, 0).getDate();
    for (let i = startingDayOfWeek - 1; i >= 0; i--) {
      const prevMonthDay = prevMonthLastDay - i;
      const prevMonth = selectedMonth === 0 ? 11 : selectedMonth - 1;
      const prevYear = selectedMonth === 0 ? selectedYear - 1 : selectedYear;
      const date = new Date(prevYear, prevMonth, prevMonthDay);
      const lunarInfo = getLunarDate(date, calendarConfig);
      const holiday = getHolidayInfo(prevYear, prevMonth, prevMonthDay);
      
      days.push({
        day: prevMonthDay,
        month: prevMonth,
        year: prevYear,
        isCurrentMonth: false,
        lunar: lunarInfo,
        holiday: holiday,
        date: date
      });
    }
    
    // 当月的日期
    for (let day = 1; day <= daysInMonth; day++) {
      const date = new Date(selectedYear, selectedMonth, day);
      const lunarInfo = getLunarDate(date, calendarConfig);
      const holiday = getHolidayInfo(selectedYear, selectedMonth, day);
      
      days.push({
        day,
        month: selectedMonth,
        year: selectedYear,
        isCurrentMonth: true,
        lunar: lunarInfo,
        holiday: holiday,
        date: date
      });
    }
    
    // 下个月的填充日期
    const remainingCells = 42 - days.length; // 6行7列 = 42个格子
    for (let day = 1; day <= remainingCells; day++) {
      const nextMonth = selectedMonth === 11 ? 0 : selectedMonth + 1;
      const nextYear = selectedMonth === 11 ? selectedYear + 1 : selectedYear;
      const date = new Date(nextYear, nextMonth, day);
      const lunarInfo = getLunarDate(date, calendarConfig);
      const holiday = getHolidayInfo(nextYear, nextMonth, day);
      
      days.push({
        day,
        month: nextMonth,
        year: nextYear,
        isCurrentMonth: false,
        lunar: lunarInfo,
        holiday: holiday,
        date: date
      });
    }
    
    setCalendarDays(days);
  };

  // 当年份或月份变化时更新日历
  useEffect(() => {
    generateMonthDays();
  }, [selectedYear, selectedMonth]);

  return (
    <CalendarContainer>
      <CalendarHeader>
        <Title level={2} style={{ textAlign: 'center' }}>万年历</Title>
      </CalendarHeader>
      
      <ControlsContainer>
        <Button onClick={prevMonth}>&lt;</Button>
        
        <Select
          value={selectedYear}
          onChange={handleYearChange}
          style={{ width: 100 }}
        >
          {years.map(year => (
            <Option key={year} value={year}>{year}</Option>
          ))}
        </Select>
        
        <Select
          value={selectedMonth}
          onChange={handleMonthChange}
          style={{ width: 100 }}
        >
          {monthNames.map((month, index) => (
            <Option key={index} value={index}>{month}</Option>
          ))}
        </Select>
        
        <Button onClick={nextMonth}>&gt;</Button>
        <Button type="primary" onClick={goToToday}>回到今天</Button>
      </ControlsContainer>
      
      {/* 星期表头 */}
      <CalendarGrid>
        {weekdayNames.map((name, index) => (
          <WeekdayHeader key={index} $isWeekend={index === 0 || index === 6}>
            {name}
          </WeekdayHeader>
        ))}
        
        {/* 日期格子 */}
        {calendarDays.map((dateObj, index) => {
          const { day, month, year, isCurrentMonth, lunar, holiday, date } = dateObj;
          const dateIsToday = isToday(year, month, day);
          const dayOfWeek = date.getDay();
          const isWeekend = dayOfWeek === 0 || dayOfWeek === 6;
          
          // 判断是否为法定节假日或调休
          const isHoliday = holiday && holiday.type === 'holiday';
          const isWorkday = holiday && holiday.type === 'workday';
          // 判断是否为实际休息日
          const isRestDay = isActualRestDay(dateObj);
          
          return (
            <Tooltip 
              key={index}
              title={
                <>
                  {holiday && holiday.name && <div>{holiday.name}</div>}
                  {lunar && <div>农历{lunar.month}{lunar.isLeapMonth ? '闰' : ''}{lunar.day}</div>}
                  {isWorkday && <div>调休工作日</div>}
                  {isHoliday && <div>法定节假日</div>}
                  {!isHoliday && !isWorkday && isWeekend && <div>周末</div>}
                </>
              }
              placement="top"
            >
              <DayCell 
                $isToday={dateIsToday}
                $isWeekend={isWeekend}
                $isWorkday={isWorkday}
                $isHoliday={isHoliday}
                style={{ opacity: isCurrentMonth ? 1 : 0.3 }}
              >
                <DayNumber 
                  $isToday={dateIsToday} 
                  $isWeekend={isWeekend} 
                  $isWorkday={isWorkday}
                  $isHoliday={isHoliday}
                >
                  {day}
                </DayNumber>
                
                {lunar && (
                  <LunarDay>
                    {lunar.day === '初一' ? `${lunar.month}${lunar.isLeapMonth ? '闰' : ''}月` : lunar.day}
                  </LunarDay>
                )}
                
                {holiday && holiday.name && (
                  <HolidayTag>{holiday.name}</HolidayTag>
                )}
                
                {isWorkday && (
                  <WorkTag style={{ backgroundColor: colors.work, color: 'white' }}>

                  </WorkTag>
                )}
                
                {isHoliday && !isWorkday && (
                  <WorkTag style={{ backgroundColor: colors.rest, color: 'white' }}>

                  </WorkTag>
                )}
              </DayCell>
            </Tooltip>
          );
        })}
      </CalendarGrid>
    </CalendarContainer>
  );
};

export default PerpetualCalendar;