Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | 20x 20x 20x 20x 20x 20x 20x 6x 6x 5x 5x 5x 1x 1x 20x 1x 1x 1x 1x 20x 1x 1x 20x 4x 4x 4x 3x 3x 1x 1x 1x 4x 20x 4x 4x 4x 20x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 20x 20x 20x | /**
* 扫描操作 Hook
*/
import { useCallback, useState } from 'react';
import { Alert } from 'react-native';
import { supabase, supabaseAdditiveService } from '@/src/lib/supabase';
import {
aiReportService,
recognizeImage,
type GenerateReportResponse,
type OcrResult,
} from '@/src/services/api';
import type { CatFood } from '@/src/types/catFood';
import type { ScanFlowState } from '../types';
interface UseScannerActionsProps {
takePicture: (options: {
quality: number;
cropToScanFrame?: boolean;
zoom?: number;
frameLayout?: { x: number; y: number; width: number; height: number };
}) => Promise<{ uri: string } | null>;
transitionTo: (state: ScanFlowState) => void;
resetFlow: () => void;
}
/**
* 扫描操作 Hook
*
* @returns OCR、拍照、AI报告等操作方法
*/
export function useScannerActions({
takePicture,
transitionTo,
resetFlow,
}: UseScannerActionsProps) {
// ==================== 状态管理 ====================
const [photoUri, setPhotoUri] = useState<string | null>(null);
const [ocrResult, setOcrResult] = useState<OcrResult | null>(null);
const [aiReport, setAiReport] = useState<GenerateReportResponse | null>(null);
const [isProcessing, setIsProcessing] = useState(false);
const [isGeneratingReport, setIsGeneratingReport] = useState(false);
const [showLoadingGame, setShowLoadingGame] = useState(false);
// ==================== 拍照操作 ====================
/**
* 拍照
* 自动裁剪到扫描框内容
* @param zoom - 当前缩放级别(0-1)
* @param frameLayout - 扫描框在屏幕上的实际位置
*/
const handleTakePhoto = useCallback(
async (
zoom?: number,
frameLayout?: { x: number; y: number; width: number; height: number } | null
) => {
try {
const photo = await takePicture({
quality: 0.6,
cropToScanFrame: true, // 启用裁剪到扫描框
zoom: zoom, // 传递缩放信息
frameLayout: frameLayout || undefined, // 传递扫描框位置
});
Eif (photo) {
setPhotoUri(photo.uri);
transitionTo('photo-preview');
}
} catch (error) {
console.error('拍照失败:', error);
Alert.alert('拍照失败', '请重试');
}
},
[takePicture, transitionTo]
);
/**
* 重新拍照
*/
const handleRetakePhoto = useCallback(() => {
setPhotoUri(null);
setOcrResult(null);
setAiReport(null);
transitionTo('taking-photo');
}, [transitionTo]);
/**
* 取消预览
*/
const handleCancelPreview = useCallback(() => {
setPhotoUri(null);
transitionTo('taking-photo');
}, [transitionTo]);
// ==================== OCR 操作 ====================
/**
* 执行 OCR 识别
*/
const performOCR = useCallback(
async (imageUri: string) => {
try {
setIsProcessing(true);
const result = await recognizeImage(imageUri);
setOcrResult(result);
transitionTo('ocr-result');
} catch (error) {
console.error('OCR识别失败:', error);
Alert.alert('识别失败', '请重试');
transitionTo('photo-preview');
} finally {
setIsProcessing(false);
}
},
[transitionTo]
);
/**
* 确认照片并开始OCR
*/
const handleConfirmPhoto = useCallback(async () => {
Iif (!photoUri) return;
transitionTo('processing-ocr');
await performOCR(photoUri);
}, [photoUri, performOCR, transitionTo]);
// ==================== AI 报告操作 ====================
/**
* 生成AI报告
* 修改:生成后自动保存到数据库(如果有选择的猫粮)
*/
const handleGenerateReport = useCallback(
async (selectedCatFood: CatFood | null) => {
Iif (!ocrResult) return;
try {
setShowLoadingGame(true);
setIsGeneratingReport(true);
// 模拟延迟,确保游戏至少展示几秒钟,提升体验
const minGameTime = new Promise((resolve) => setTimeout(resolve, 3000));
const reportPromise = aiReportService.generateReport({
ingredients: ocrResult.text,
max_tokens: 2048,
});
// 等待报告生成和最小游戏时间
const [report] = await Promise.all([reportPromise, minGameTime]);
setAiReport(report);
// ========== 自动保存报告到数据库 ==========
Iif (selectedCatFood) {
try {
const saveReportResult = await aiReportService.saveReport({
catfood_id: selectedCatFood.id,
ingredients_text: ocrResult.text,
tags: report.tags || [],
additives: report.additives || [],
ingredients: report.identified_nutrients || [],
safety: report.safety || '',
nutrient: report.nutrient || '',
percentage: report.percentage ?? false,
percent_data: report.percent_data || {}, // ✅ 使用动态 percent_data
});
} catch (error: any) {
// 权限检查:403 表示需要管理员权限
if (error.response?.status === 403) {
Alert.alert(
'权限不足',
error.response?.data?.message || '该猫粮已有营养成分信息,只有管理员可以更新。',
[
{
text: '了解',
style: 'default',
},
]
);
}
// 保存失败不影响显示报告,继续显示AI分析结果
}
}
// transitionTo('ai-report-detail'); // 移除自动跳转,等待用户关闭游戏弹窗
} catch (error) {
Alert.alert('错误', '生成报告失败');
setShowLoadingGame(false); // 失败时关闭弹窗
} finally {
setIsGeneratingReport(false);
}
},
[ocrResult, transitionTo]
);
/**
* 关闭加载游戏并显示报告
*/
const handleCloseLoadingGame = useCallback(() => {
setShowLoadingGame(false);
if (aiReport) {
transitionTo('ai-report-detail');
}
}, [aiReport, transitionTo]);
/**
* 保存报告到猫粮(更新成分和添加剂关联)
* 注意:AI报告已在生成时自动保存,此函数仅用于更新关联
*/
const handleSaveReport = useCallback(
async (selectedCatFood: CatFood | null) => {
if (!aiReport || !selectedCatFood || !ocrResult) return;
try {
setIsProcessing(true);
// ========== 步骤 1: 查询识别到的成分ID列表 ==========
const ingredientIds: number[] = [];
const notFoundIngredients: string[] = [];
if (aiReport.identified_nutrients && aiReport.identified_nutrients.length > 0) {
for (const nutrientName of aiReport.identified_nutrients) {
try {
const { data, error } = await supabaseAdditiveService.searchIngredient(nutrientName);
if (!error && data && data.ingredient?.id && !data.notFound) {
ingredientIds.push(data.ingredient.id);
} else {
notFoundIngredients.push(nutrientName);
}
} catch {
notFoundIngredients.push(nutrientName);
}
}
}
// ========== 步骤 2: 查询识别到的添加剂ID列表 ==========
const additiveIds: number[] = [];
const notFoundAdditives: string[] = [];
if (aiReport.additives && aiReport.additives.length > 0) {
for (const additiveName of aiReport.additives) {
try {
const { data, error } = await supabaseAdditiveService.searchAdditive(additiveName);
if (!error && data && data.additive?.id && !data.notFound) {
additiveIds.push(data.additive.id);
} else {
notFoundAdditives.push(additiveName);
}
} catch {
notFoundAdditives.push(additiveName);
}
}
}
// ========== 步骤 3: 使用 Supabase 更新猫粮关联 ==========
// 更新成分关联
if (ingredientIds.length > 0) {
// 删除旧关联
await supabase.from('catfood_ingredients').delete().eq('catfood_id', selectedCatFood.id);
// 创建新关联
await supabase.from('catfood_ingredients').insert(
ingredientIds.map((ingredientId) => ({
catfood_id: selectedCatFood.id,
ingredient_id: ingredientId,
}))
);
}
// 更新添加剂关联
if (additiveIds.length > 0) {
// 删除旧关联
await supabase.from('catfood_additives').delete().eq('catfood_id', selectedCatFood.id);
// 创建新关联
await supabase.from('catfood_additives').insert(
additiveIds.map((additiveId) => ({
catfood_id: selectedCatFood.id,
additive_id: additiveId,
}))
);
}
// ========== 步骤 4: 提示用户 ==========
let message = '猫粮成分和添加剂关联已更新';
if (notFoundIngredients.length > 0 || notFoundAdditives.length > 0) {
message += '\n\n部分成分未找到:';
if (notFoundIngredients.length > 0) {
message += `\n成分: ${notFoundIngredients.join(', ')}`;
}
if (notFoundAdditives.length > 0) {
message += `\n添加剂: ${notFoundAdditives.join(', ')}`;
}
}
Alert.alert('更新成功', message, [
{
text: '确定',
onPress: () => resetFlow(),
},
]);
} catch (error) {
console.error('❌ 更新猫粮信息失败:', error);
Alert.alert('更新失败', '请重试');
} finally {
setIsProcessing(false);
}
},
[aiReport, ocrResult, resetFlow]
);
// ==================== 返回值 ====================
return {
// 状态
photoUri,
ocrResult,
aiReport,
isProcessing,
isGeneratingReport,
showLoadingGame,
// 方法
handleTakePhoto,
handleRetakePhoto,
handleCancelPreview,
handleConfirmPhoto,
handleGenerateReport,
handleSaveReport,
setIsGeneratingReport,
handleCloseLoadingGame,
};
}
|