import { View, Text } from '@tarojs/components' import { useDidShow } from '@tarojs/taro' import { FC, useState } from 'react' import { Network } from '@/network' import './index.css' interface SignInRecord { id: number date: string count: number details: string[] } /** * 签到记录页面 */ const RecordPage: FC = () => { const [records, setRecords] = useState([]) const [stats, setStats] = useState({ continuousDays: 0, totalDays: 0 }) const [loading, setLoading] = useState(true) // 页面显示时获取记录 useDidShow(async () => { await fetchRecords() }) // 获取签到记录 const fetchRecords = async () => { try { setLoading(true) const res = await Network.request({ url: '/api/super-topics/records', method: 'GET' }) console.log('签到记录:', res.data) if (res.data?.code === 200 && res.data?.data) { setRecords(res.data.data.records || []) setStats({ continuousDays: res.data.data.continuousDays || 0, totalDays: res.data.data.totalDays || 0 }) } } catch (error) { console.error('获取签到记录失败:', error) } finally { setLoading(false) } } // 格式化日期 const formatDate = (dateStr: string) => { const date = new Date(dateStr) const month = date.getMonth() + 1 const day = date.getDate() const weekDays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'] const weekDay = weekDays[date.getDay()] return `${month}月${day}日 ${weekDay}` } // 获取当前月份的天数 const getCurrentMonthDays = () => { const now = new Date() const year = now.getFullYear() const month = now.getMonth() const firstDay = new Date(year, month, 1) const lastDay = new Date(year, month + 1, 0) const days: { date: Date | null; hasRecord: boolean }[] = [] // 填充前面的空白 for (let i = 0; i < firstDay.getDay(); i++) { days.push({ date: null, hasRecord: false }) } // 获取有签到记录的日期 const recordDates = records.map(r => r.date) // 填充日期 for (let i = 1; i <= lastDay.getDate(); i++) { const date = new Date(year, month, i) const dateStr = `${year}-${String(month + 1).padStart(2, '0')}-${String(i).padStart(2, '0')}` const hasRecord = recordDates.includes(dateStr) days.push({ date, hasRecord }) } return days } const monthDays = getCurrentMonthDays() const now = new Date() const monthNames = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'] return ( {/* 统计卡片 */} 连续签到 {stats.continuousDays} 累计签到 {stats.totalDays} {/* 日历视图 */} {monthNames[now.getMonth()]} {now.getFullYear()} {/* 星期标题 */} {['日', '一', '二', '三', '四', '五', '六'].map((day, index) => ( {day} ))} {/* 日期网格 */} {monthDays.map((item, index) => ( {item.date ? ( {item.date.getDate()} ) : ( )} ))} {/* 签到记录列表 */} 签到历史 {loading ? ( 加载中... ) : records.length === 0 ? ( 暂无签到记录 ) : ( {records.slice(0, 10).map((record) => ( {formatDate(record.date)} {record.count} 个超话 ))} )} ) } export default RecordPage