|
|
@@ -1,5 +1,5 @@
|
|
|
import axios from 'axios'
|
|
|
-import type { Recipe, CuisineType } from '@/types'
|
|
|
+import type { Recipe, CuisineType, NutritionAnalysis } from '@/types'
|
|
|
|
|
|
// AI服务配置 - 智谱AI
|
|
|
const AI_CONFIG = {
|
|
|
@@ -12,7 +12,7 @@ const AI_CONFIG = {
|
|
|
apiKey: 'sk-78d4fed678fa4a5ebc5f7beac54b1a78',
|
|
|
model: 'deepseek-chat',
|
|
|
temperature: 0.7,
|
|
|
- timeout: 30000
|
|
|
+ timeout: 300000
|
|
|
}
|
|
|
|
|
|
// 创建axios实例
|
|
|
@@ -48,7 +48,7 @@ export const generateRecipe = async (ingredients: string[], cuisine: CuisineType
|
|
|
|
|
|
prompt += `
|
|
|
|
|
|
-请按照以下JSON格式返回菜谱:
|
|
|
+请按照以下JSON格式返回菜谱,包含营养分析:
|
|
|
{
|
|
|
"name": "菜品名称",
|
|
|
"ingredients": ["食材1", "食材2"],
|
|
|
@@ -62,7 +62,25 @@ export const generateRecipe = async (ingredients: string[], cuisine: CuisineType
|
|
|
],
|
|
|
"cookingTime": 30,
|
|
|
"difficulty": "medium",
|
|
|
- "tips": ["技巧1", "技巧2"]
|
|
|
+ "tips": ["技巧1", "技巧2"],
|
|
|
+ "nutritionAnalysis": {
|
|
|
+ "nutrition": {
|
|
|
+ "calories": 350,
|
|
|
+ "protein": 25,
|
|
|
+ "carbs": 45,
|
|
|
+ "fat": 12,
|
|
|
+ "fiber": 8,
|
|
|
+ "sodium": 800,
|
|
|
+ "sugar": 6,
|
|
|
+ "vitaminC": 30,
|
|
|
+ "calcium": 150,
|
|
|
+ "iron": 3
|
|
|
+ },
|
|
|
+ "healthScore": 8,
|
|
|
+ "balanceAdvice": ["建议搭配蔬菜沙拉增加维生素", "可适量减少盐分"],
|
|
|
+ "dietaryTags": ["高蛋白", "低脂"],
|
|
|
+ "servingSize": "1人份"
|
|
|
+ }
|
|
|
}`
|
|
|
|
|
|
// 调用智谱AI接口
|
|
|
@@ -108,7 +126,8 @@ export const generateRecipe = async (ingredients: string[], cuisine: CuisineType
|
|
|
],
|
|
|
cookingTime: recipeData.cookingTime || 25,
|
|
|
difficulty: recipeData.difficulty || 'medium',
|
|
|
- tips: recipeData.tips || ['注意火候控制', '调味要适中']
|
|
|
+ tips: recipeData.tips || ['注意火候控制', '调味要适中'],
|
|
|
+ nutritionAnalysis: recipeData.nutritionAnalysis || generateFallbackNutrition(ingredients)
|
|
|
}
|
|
|
|
|
|
return recipe
|
|
|
@@ -130,7 +149,8 @@ export const generateRecipe = async (ingredients: string[], cuisine: CuisineType
|
|
|
],
|
|
|
cookingTime: 22,
|
|
|
difficulty: 'medium',
|
|
|
- tips: ['火候要掌握好,避免炒糊', '调料要适量,突出食材本味', '炒制过程中要勤翻动']
|
|
|
+ tips: ['火候要掌握好,避免炒糊', '调料要适量,突出食材本味', '炒制过程中要勤翻动'],
|
|
|
+ nutritionAnalysis: generateFallbackNutrition(ingredients)
|
|
|
}
|
|
|
|
|
|
return fallbackRecipe
|
|
|
@@ -184,7 +204,7 @@ export const generateCustomRecipe = async (ingredients: string[], customPrompt:
|
|
|
|
|
|
用户的特殊要求:${customPrompt}
|
|
|
|
|
|
-请按照以下JSON格式返回菜谱:
|
|
|
+请按照以下JSON格式返回菜谱,包含营养分析:
|
|
|
{
|
|
|
"name": "菜品名称",
|
|
|
"ingredients": ["食材1", "食材2"],
|
|
|
@@ -198,7 +218,25 @@ export const generateCustomRecipe = async (ingredients: string[], customPrompt:
|
|
|
],
|
|
|
"cookingTime": 30,
|
|
|
"difficulty": "medium",
|
|
|
- "tips": ["技巧1", "技巧2"]
|
|
|
+ "tips": ["技巧1", "技巧2"],
|
|
|
+ "nutritionAnalysis": {
|
|
|
+ "nutrition": {
|
|
|
+ "calories": 350,
|
|
|
+ "protein": 25,
|
|
|
+ "carbs": 45,
|
|
|
+ "fat": 12,
|
|
|
+ "fiber": 8,
|
|
|
+ "sodium": 800,
|
|
|
+ "sugar": 6,
|
|
|
+ "vitaminC": 30,
|
|
|
+ "calcium": 150,
|
|
|
+ "iron": 3
|
|
|
+ },
|
|
|
+ "healthScore": 8,
|
|
|
+ "balanceAdvice": ["建议搭配蔬菜沙拉增加维生素", "可适量减少盐分"],
|
|
|
+ "dietaryTags": ["高蛋白", "低脂"],
|
|
|
+ "servingSize": "1人份"
|
|
|
+ }
|
|
|
}`
|
|
|
|
|
|
// 调用智谱AI接口
|
|
|
@@ -244,7 +282,8 @@ export const generateCustomRecipe = async (ingredients: string[], customPrompt:
|
|
|
],
|
|
|
cookingTime: recipeData.cookingTime || 25,
|
|
|
difficulty: recipeData.difficulty || 'medium',
|
|
|
- tips: recipeData.tips || ['根据个人口味调整', '注意火候控制']
|
|
|
+ tips: recipeData.tips || ['根据个人口味调整', '注意火候控制'],
|
|
|
+ nutritionAnalysis: recipeData.nutritionAnalysis || generateFallbackNutrition(ingredients)
|
|
|
}
|
|
|
|
|
|
return recipe
|
|
|
@@ -265,12 +304,62 @@ export const generateCustomRecipe = async (ingredients: string[], customPrompt:
|
|
|
],
|
|
|
cookingTime: 25,
|
|
|
difficulty: 'medium',
|
|
|
- tips: ['根据个人喜好调整口味', '注意食材的新鲜度', '掌握好火候']
|
|
|
+ tips: ['根据个人喜好调整口味', '注意食材的新鲜度', '掌握好火候'],
|
|
|
+ nutritionAnalysis: generateFallbackNutrition(ingredients)
|
|
|
}
|
|
|
|
|
|
return fallbackRecipe
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * 生成后备营养分析数据
|
|
|
+ * @param ingredients 食材列表
|
|
|
+ * @returns NutritionAnalysis
|
|
|
+ */
|
|
|
+const generateFallbackNutrition = (ingredients: string[]): NutritionAnalysis => {
|
|
|
+ // 基于食材数量和类型估算营养成分
|
|
|
+ const baseCalories = ingredients.length * 50 + Math.floor(Math.random() * 100) + 200
|
|
|
+ const hasVegetables = ingredients.some(ing => ['菜', '瓜', '豆', '萝卜', '白菜', '菠菜', '西红柿', '黄瓜', '茄子', '土豆'].some(veg => ing.includes(veg)))
|
|
|
+ const hasMeat = ingredients.some(ing => ['肉', '鸡', '鱼', '虾', '蛋', '牛', '猪', '羊'].some(meat => ing.includes(meat)))
|
|
|
+ const hasGrains = ingredients.some(ing => ['米', '面', '粉', '饭', '面条', '馒头'].some(grain => ing.includes(grain)))
|
|
|
+
|
|
|
+ // 生成饮食标签
|
|
|
+ const dietaryTags: string[] = []
|
|
|
+ if (hasVegetables && !hasMeat) dietaryTags.push('素食')
|
|
|
+ if (hasMeat) dietaryTags.push('高蛋白')
|
|
|
+ if (hasVegetables) dietaryTags.push('富含维生素')
|
|
|
+ if (!hasGrains) dietaryTags.push('低碳水')
|
|
|
+ if (baseCalories < 300) dietaryTags.push('低卡路里')
|
|
|
+
|
|
|
+ // 生成营养建议
|
|
|
+ const balanceAdvice: string[] = []
|
|
|
+ if (!hasVegetables) balanceAdvice.push('建议搭配新鲜蔬菜增加维生素和膳食纤维')
|
|
|
+ if (!hasMeat && !ingredients.some(ing => ['豆', '蛋', '奶'].some(protein => ing.includes(protein)))) {
|
|
|
+ balanceAdvice.push('建议增加蛋白质来源,如豆类或蛋类')
|
|
|
+ }
|
|
|
+ if (hasGrains && hasMeat) balanceAdvice.push('营养搭配均衡,适合日常食用')
|
|
|
+ if (ingredients.length > 5) balanceAdvice.push('食材丰富,营养全面')
|
|
|
+
|
|
|
+ return {
|
|
|
+ nutrition: {
|
|
|
+ calories: baseCalories,
|
|
|
+ protein: hasMeat ? 20 + Math.floor(Math.random() * 15) : 8 + Math.floor(Math.random() * 8),
|
|
|
+ carbs: hasGrains ? 35 + Math.floor(Math.random() * 20) : 15 + Math.floor(Math.random() * 10),
|
|
|
+ fat: hasMeat ? 12 + Math.floor(Math.random() * 8) : 5 + Math.floor(Math.random() * 5),
|
|
|
+ fiber: hasVegetables ? 6 + Math.floor(Math.random() * 4) : 2 + Math.floor(Math.random() * 2),
|
|
|
+ sodium: 600 + Math.floor(Math.random() * 400),
|
|
|
+ sugar: 3 + Math.floor(Math.random() * 5),
|
|
|
+ vitaminC: hasVegetables ? 20 + Math.floor(Math.random() * 30) : undefined,
|
|
|
+ calcium: hasMeat || ingredients.some(ing => ['奶', '豆'].some(ca => ing.includes(ca))) ? 100 + Math.floor(Math.random() * 100) : undefined,
|
|
|
+ iron: hasMeat ? 2 + Math.floor(Math.random() * 3) : undefined
|
|
|
+ },
|
|
|
+ healthScore: Math.floor(Math.random() * 3) + (hasVegetables ? 6 : 4) + (hasMeat ? 1 : 0),
|
|
|
+ balanceAdvice: balanceAdvice.length > 0 ? balanceAdvice : ['营养搭配合理,可以放心享用'],
|
|
|
+ dietaryTags: dietaryTags.length > 0 ? dietaryTags : ['家常菜'],
|
|
|
+ servingSize: '1人份'
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// 导出配置更新函数,供外部使用
|
|
|
export { AI_CONFIG }
|