This commit is contained in:
2025-12-13 14:46:09 +08:00
parent 026600d27e
commit 33e91aaea8

View File

@@ -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<RecommendedMedia[]> {
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<void> {
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<void> {
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()}`;
}