feat(nutrition): 优化营养成分表分析功能并移除流式显示
- 移除流式分析文本显示,简化用户界面 - 修复ImagePicker媒体类型配置,使用数组格式 - 简化API响应处理逻辑,直接使用服务端返回数据 - 移除旧格式转换函数,统一使用新的API响应格式 - 清理冗余状态变量和UI组件,提升代码可维护性
This commit is contained in:
@@ -101,74 +101,37 @@ export async function saveNutritionLabelToDietRecord(
|
||||
* 需要先上传图片到COS获取URL,然后调用此接口
|
||||
*/
|
||||
export async function analyzeNutritionImage(request: NutritionAnalysisRequest): Promise<NutritionAnalysisResponse> {
|
||||
return api.post<NutritionAnalysisResponse>('/diet-records/analyze-nutrition-image', request);
|
||||
}
|
||||
try {
|
||||
const response = await api.post<any>('/diet-records/analyze-nutrition-image', request);
|
||||
|
||||
/**
|
||||
* 将新API的分析结果转换为旧格式,以便与现有UI兼容
|
||||
*/
|
||||
export function convertNewApiResultToOldFormat(
|
||||
newResult: NutritionAnalysisResponse,
|
||||
imageUri: string
|
||||
): NutritionLabelAnalysisResult | null {
|
||||
if (!newResult.success || !newResult.data || newResult.data.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 从新API结果中提取营养数据
|
||||
const nutritionData: NutritionLabelData = {
|
||||
energy: 0,
|
||||
protein: 0,
|
||||
fat: 0,
|
||||
carbohydrate: 0,
|
||||
sodium: 0,
|
||||
fiber: 0,
|
||||
sugar: 0,
|
||||
};
|
||||
|
||||
// 查找各个营养素的值并转换为数字
|
||||
newResult.data.forEach(item => {
|
||||
const valueStr = item.value;
|
||||
// 提取数字部分
|
||||
const numericValue = parseFloat(valueStr.replace(/[^\d.]/g, ''));
|
||||
|
||||
switch (item.key) {
|
||||
case 'energy_kcal':
|
||||
// 如果是千焦,转换为千卡 (1千焦 ≈ 0.239千卡)
|
||||
if (valueStr.includes('千焦')) {
|
||||
nutritionData.energy = Math.round(numericValue * 0.239);
|
||||
} else {
|
||||
nutritionData.energy = numericValue;
|
||||
}
|
||||
break;
|
||||
case 'protein':
|
||||
nutritionData.protein = numericValue;
|
||||
break;
|
||||
case 'fat':
|
||||
nutritionData.fat = numericValue;
|
||||
break;
|
||||
case 'carbohydrate':
|
||||
nutritionData.carbohydrate = numericValue;
|
||||
break;
|
||||
case 'sodium':
|
||||
nutritionData.sodium = numericValue;
|
||||
break;
|
||||
case 'fiber':
|
||||
nutritionData.fiber = numericValue;
|
||||
break;
|
||||
case 'sugar':
|
||||
nutritionData.sugar = numericValue;
|
||||
break;
|
||||
// 处理不同的响应格式
|
||||
if (Array.isArray(response)) {
|
||||
// 如果直接返回数组,包装成标准格式
|
||||
return {
|
||||
success: true,
|
||||
data: response as NutritionItem[]
|
||||
};
|
||||
} else if (response && typeof response === 'object') {
|
||||
// 如果是对象,检查是否已经是标准格式
|
||||
if (response.success !== undefined && response.data) {
|
||||
return response as NutritionAnalysisResponse;
|
||||
} else if (Array.isArray(response.data)) {
|
||||
// 如果有data字段且是数组,包装成标准格式
|
||||
return {
|
||||
success: true,
|
||||
data: response.data as NutritionItem[]
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
id: Date.now().toString(),
|
||||
imageUri,
|
||||
nutritionData,
|
||||
confidence: 0.9, // 新API没有提供置信度,使用默认值
|
||||
analyzedAt: new Date().toISOString(),
|
||||
foodName: '营养成分表分析',
|
||||
brand: '未知',
|
||||
};
|
||||
}
|
||||
// 如果都不匹配,返回错误
|
||||
throw new Error('无法解析API返回结果');
|
||||
} catch (error) {
|
||||
console.error('[NUTRITION_ANALYSIS] API调用失败:', error);
|
||||
return {
|
||||
success: false,
|
||||
data: [],
|
||||
message: error instanceof Error ? error.message : '分析失败'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user