diff --git a/src/family/pages/Dashboard.tsx b/src/family/pages/Dashboard.tsx new file mode 100644 index 0000000..378ab67 --- /dev/null +++ b/src/family/pages/Dashboard.tsx @@ -0,0 +1,284 @@ +import React, { useState, useEffect } from 'react'; +import { + MessageCircle, + Heart, + Image as ImageIcon, + AlertTriangle, + Video, + Maximize2, +} from 'lucide-react'; +import { MetricCard } from '../components/MetricCard'; +import { TrendChart } from '../components/TrendChart'; +import * as moodService from '../services/moodService'; +import * as mediaService from '../services/mediaService'; + +/** + * 家属端 Dashboard - 今天概览 + * 手机浏览器优化 - 单列布局,紧凑显示 + * 一页看完今天所有重要信息 + */ + +interface DashboardProps { + onNavigate?: (page: 'interaction' | 'messages' | 'care' | 'alerts' | 'media' | 'mood') => void; +} + +interface ChatMessage { + username: string; + is_adopted: number; + type: 'fay' | 'member'; + way: string; + content: string; + createtime: number; + timetext: string; +} + +const API_BASE_URL = 'http://127.0.0.1:5000'; +const familyId = 'family_001'; + +export const Dashboard: React.FC = ({ onNavigate }) => { + const [isVideoFullscreen, setIsVideoFullscreen] = useState(false); + const [todayInteractionCount, setTodayInteractionCount] = useState(0); + const [latestMood, setLatestMood] = useState(null); + const [moodStats, setMoodStats] = useState(null); + const [recentPlays, setRecentPlays] = useState([]); + + // 加载今日交互次数 + const loadTodayInteractionCount = async () => { + try { + const response = await fetch(`${API_BASE_URL}/api/get-msg`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ username: 'User', limit: 300 }), + }); + + if (response.ok) { + const data = await response.json(); + const messages: ChatMessage[] = data.list || []; + + // 获取今天的开始时间戳(00:00:00) + const today = new Date(); + today.setHours(0, 0, 0, 0); + const todayTimestamp = Math.floor(today.getTime() / 1000); + + // 统计今天老人说的话(type === 'member') + const todayMemberMessages = messages.filter( + msg => msg.type === 'member' && msg.createtime >= todayTimestamp + ); + + setTodayInteractionCount(todayMemberMessages.length); + } + } catch (error) { + console.error('获取今日交互次数失败:', error); + // 失败时保持为0,不影响页面显示 + } + }; + + // 加载情绪数据 + const loadMoodData = async () => { + try { + const [latest, stats] = await Promise.all([ + moodService.getFamilyMoods(familyId, { limit: 1 }), + moodService.getMoodStats(familyId, { days: 7 }), + ]); + + if (latest.records.length > 0) { + setLatestMood(latest.records[0]); + } + setMoodStats(stats); + } catch (error) { + console.error('加载情绪数据失败:', error); + } + }; + + // 加载最近播放 + const loadRecentPlays = async () => { + try { + const plays = await mediaService.getRecentPlays(familyId, 2); + setRecentPlays(plays); + } catch (error) { + console.error('加载最近播放失败:', error); + } + }; + + useEffect(() => { + loadTodayInteractionCount(); + loadMoodData(); + loadRecentPlays(); + // 每30秒刷新一次 + const interval = setInterval(() => { + loadTodayInteractionCount(); + loadMoodData(); + loadRecentPlays(); + }, 30000); + return () => clearInterval(interval); + }, []); + + // 情绪趋势数据 - 使用真实数据或默认数据 + const emotionData = moodStats?.daily_stats.map(stat => ({ + date: new Date(stat.date).toLocaleDateString('zh-CN', { month: 'numeric', day: 'numeric' }), + value: stat.avg_score, + })) || []; + + + // 格式化播放时间 + const formatPlayTime = (playedAt: string) => { + const playTime = new Date(playedAt); + const now = new Date(); + const diffMs = now.getTime() - playTime.getTime(); + const diffMins = Math.floor(diffMs / 60000); + const diffHours = Math.floor(diffMs / 3600000); + const diffDays = Math.floor(diffMs / 86400000); + + if (diffMins < 1) return '刚刚'; + if (diffMins < 60) return `${diffMins}分钟前`; + if (diffHours < 24) return `${diffHours}小时前`; + if (diffDays === 1) return '昨天'; + if (diffDays < 7) return `${diffDays}天前`; + + return playTime.toLocaleDateString('zh-CN', { month: 'numeric', day: 'numeric' }); + }; + + + return ( +
+ {/* 主要内容区 - 手机优化 */} +
+ {/* 实时监控视频 */} +
+
+ {/* 视频播放器 */} +
+ {/* 模拟视频画面 */} +
+
+ + {/* 实时状态标签 */} +
+
+ 实时 +
+ + {/* 全屏按钮 */} + + + {/* 视频信息叠加 */} +
+
+
客厅监控
+
张奶奶正在与数字人对话
+
+
+
+ + {/* 视频控制栏 */} +
+
+
+ 画质:高清 + | + 延迟:<1s +
+ +
+
+
+
+ + + {/* 关键指标卡片 - 手机单列布局 */} +
+ onNavigate?.('interaction')} + /> + = 6 ? 'green' : latestMood && latestMood.mood_score >= 4 ? 'yellow' : 'red'} + onClick={() => onNavigate?.('mood')} + /> +
+ + {/* 趋势图表 - 手机单列堆叠 */} +
+ +
+ + {/* 最近播放 - 手机优化 */} +
+
+

最近播放

+ +
+ {recentPlays.length > 0 ? ( +
+ {recentPlays.map((media) => ( +
+
+ {media.thumbnail_path ? ( + {media.title} { + e.currentTarget.style.display = 'none'; + }} + /> + ) : ( + + )} +
+
+

+ {media.title} +

+

{formatPlayTime(media.played_at)}

+
+ 👍 {media.likes} + 👎 {media.dislikes} +
+
+
+ ))} +
+ ) : ( +
+

还没有播放记录

+
+ )} +
+
+
+ ); +};