From 33e91aaea8d70aa15b0cb5a3c2ef65c51df3c73f Mon Sep 17 00:00:00 2001 From: 15945162479 <15945162479@qq.com> Date: Sat, 13 Dec 2025 14:46:09 +0800 Subject: [PATCH] Add File --- src/elderly/services/mediaService.ts | 117 +++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/elderly/services/mediaService.ts diff --git a/src/elderly/services/mediaService.ts b/src/elderly/services/mediaService.ts new file mode 100644 index 0000000..b2dafa2 --- /dev/null +++ b/src/elderly/services/mediaService.ts @@ -0,0 +1,117 @@ +/** + * 老人端媒体服务 + * 负责获取推荐媒体、记录播放、提交反馈 + */ + +const API_BASE_URL = 'http://localhost:8000/api'; + +export interface RecommendedMedia { + id: number; + family_id: string; + media_type: 'photo' | 'video'; + title: string; + description?: string; + file_path: string; + thumbnail_path?: string; + tags: string[]; + time_windows: string[]; + moods: string[]; + occasions: string[]; + cooldown: number; + priority: number; + play_count: number; + last_played_at?: string; +} + +export interface PlayRecordParams { + elderly_id: number; + duration_watched?: number; + completed?: number; + triggered_by?: 'auto' | 'manual' | 'mood'; + mood_before?: string; + mood_after?: string; +} + +export interface FeedbackParams { + elderly_id: number; + feedback_type: 'like' | 'dislike'; +} + +/** + * 获取推荐媒体 + */ +export async function getRecommendedMedia( + familyId: string, + elderlyId: number, + mood?: string, + occasion?: string +): Promise { + const params = new URLSearchParams({ + family_id: familyId, + elderly_id: elderlyId.toString(), + }); + + if (mood) params.append('mood', mood); + if (occasion) params.append('occasion', occasion); + + const response = await fetch(`${API_BASE_URL}/elderly/media/recommended?${params}`); + + if (!response.ok) { + const error = await response.json(); + throw new Error(error.error || '获取推荐媒体失败'); + } + + const data = await response.json(); + return data.media; +} + +/** + * 记录媒体播放 + */ +export async function recordMediaPlay(mediaId: number, params: PlayRecordParams): Promise { + const response = await fetch(`${API_BASE_URL}/elderly/media/${mediaId}/play`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(params), + }); + + if (!response.ok) { + const error = await response.json(); + throw new Error(error.error || '记录播放失败'); + } +} + +/** + * 提交媒体反馈 + */ +export async function submitFeedback(mediaId: number, params: FeedbackParams): Promise { + const response = await fetch(`${API_BASE_URL}/elderly/media/${mediaId}/feedback`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(params), + }); + + if (!response.ok) { + const error = await response.json(); + throw new Error(error.error || '提交反馈失败'); + } +} + +/** + * 获取文件URL(用于前端显示) + */ +export function getMediaUrl(filePath: string): string { + // 将服务器文件路径转换为可访问的URL + return `http://localhost:8000/uploads/${filePath.split(/[/\\]/).pop()}`; +} + +/** + * 获取缩略图URL + */ +export function getThumbnailUrl(thumbnailPath: string): string { + return `http://localhost:8000/uploads/thumbnails/${thumbnailPath.split(/[/\\]/).pop()}`; +}