feat(nutrition): 添加营养成分分析历史记录功能
- 新增历史记录页面,支持查看、筛选和分页加载营养成分分析记录 - 在分析页面添加历史记录入口,使用Liquid Glass效果 - 优化分析结果展示样式,采用卡片式布局和渐变效果 - 移除流式分析相关代码,简化分析流程 - 添加历史记录API接口和类型定义
This commit is contained in:
@@ -135,3 +135,97 @@ export async function analyzeNutritionImage(request: NutritionAnalysisRequest):
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 营养成分分析记录的接口定义
|
||||
export interface NutritionAnalysisRecord {
|
||||
id: number;
|
||||
userId: string;
|
||||
imageUrl: string;
|
||||
analysisResult: {
|
||||
data: NutritionItem[];
|
||||
success: boolean;
|
||||
message?: string;
|
||||
};
|
||||
status: 'success' | 'failed' | 'processing';
|
||||
message: string;
|
||||
aiProvider: string;
|
||||
aiModel: string;
|
||||
nutritionCount: number;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
// 获取历史记录的请求参数
|
||||
export interface GetNutritionRecordsParams {
|
||||
startDate?: string;
|
||||
endDate?: string;
|
||||
status?: string;
|
||||
page?: number;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
// 获取历史记录的响应格式
|
||||
export interface GetNutritionRecordsResponse {
|
||||
code: number;
|
||||
message: string;
|
||||
data: {
|
||||
records: NutritionAnalysisRecord[];
|
||||
total: number;
|
||||
page: number;
|
||||
limit: number;
|
||||
totalPages: number;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取营养成分分析记录列表
|
||||
*/
|
||||
export async function getNutritionAnalysisRecords(params?: GetNutritionRecordsParams): Promise<GetNutritionRecordsResponse> {
|
||||
try {
|
||||
const searchParams = new URLSearchParams();
|
||||
if (params) {
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value !== undefined) {
|
||||
searchParams.append(key, String(value));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const queryString = searchParams.toString() ? `?${searchParams.toString()}` : '';
|
||||
|
||||
// 使用 api.get 方法,但需要特殊处理响应格式
|
||||
const response = await api.get<any>(`/diet-records/nutrition-analysis-records${queryString}`);
|
||||
|
||||
// 检查响应是否已经是标准格式
|
||||
if (response && typeof response === 'object' && 'code' in response) {
|
||||
return response as GetNutritionRecordsResponse;
|
||||
}
|
||||
|
||||
// 如果不是标准格式,包装成标准格式
|
||||
return {
|
||||
code: 0,
|
||||
message: '获取成功',
|
||||
data: {
|
||||
records: response.records || [],
|
||||
total: response.total || 0,
|
||||
page: response.page || 1,
|
||||
limit: response.limit || 20,
|
||||
totalPages: response.totalPages || Math.ceil((response.total || 0) / (response.limit || 20))
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('[NUTRITION_RECORDS] 获取历史记录失败:', error);
|
||||
// 返回错误格式的响应
|
||||
return {
|
||||
code: 1,
|
||||
message: error instanceof Error ? error.message : '获取营养成分分析记录失败,请稍后重试',
|
||||
data: {
|
||||
records: [],
|
||||
total: 0,
|
||||
page: 1,
|
||||
limit: 20,
|
||||
totalPages: 0
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user