From a46697c5c390ab6b34cea20d87262fbecd2b2410 Mon Sep 17 00:00:00 2001 From: icarus Date: Fri, 22 Aug 2025 20:21:29 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(ocr):=20=E6=94=B9=E8=BF=9BOCR=E5=A4=84?= =?UTF-8?q?=E7=90=86=E4=B8=AD=E7=9A=84=E6=B6=88=E6=81=AF=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=92=8C=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在useOcr钩子中统一管理OCR处理的消息提示,并完善错误处理逻辑 移除TranslatePage中重复的消息管理代码,简化OCR处理流程 --- src/renderer/src/hooks/useOcr.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/renderer/src/hooks/useOcr.ts b/src/renderer/src/hooks/useOcr.ts index 4e5a915a3e..4513f31ad9 100644 --- a/src/renderer/src/hooks/useOcr.ts +++ b/src/renderer/src/hooks/useOcr.ts @@ -1,8 +1,11 @@ +import { loggerService } from '@logger' import { useAppSelector } from '@renderer/store' import { ImageFileMetadata, isImageFile, SupportedOcrFile } from '@renderer/types' +import { uuid } from '@renderer/utils' +import { formatErrorMessage } from '@renderer/utils/error' import { useTranslation } from 'react-i18next' -export const useImageOcr = () => {} +const logger = loggerService.withContext('useOcr') export const useOcr = () => { const { t } = useTranslation() @@ -18,14 +21,24 @@ export const useOcr = () => { } /** - * 对支持的文件进行OCR识别 + * 对支持的文件进行OCR识别. * @param file 支持OCR的文件 * @returns OCR识别结果的Promise - * @throws 当文件类型不支持时抛出错误 + * @throws 当文件类型不支持或OCR失败时抛出错误 */ const ocr = async (file: SupportedOcrFile) => { - if (isImageFile(file)) { - return ocrImage(file) + const key = uuid() + window.message.loading({ content: t('ocr.processing'), key, duration: 0 }) + try { + if (isImageFile(file)) { + return ocrImage(file) + } + } catch (e) { + logger.error('Failed to ocr.', e as Error) + window.message.error(t('ocr.error.unknown') + ': ' + formatErrorMessage(e)) + throw e + } finally { + window.message.destroy(key) } // @ts-expect-error all types should be covered throw new Error(t('ocr.file.not_supported', { type: file.type })) From bbf3af070a4417683b54f19c3fb2063ad71ec881 Mon Sep 17 00:00:00 2001 From: icarus Date: Fri, 22 Aug 2025 20:49:05 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat(i18n):=20=E6=B7=BB=E5=8A=A0OCR?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E7=9A=84=E9=94=99=E8=AF=AF=E5=92=8C=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E7=BF=BB=E8=AF=91=E6=96=87=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/src/i18n/locales/zh-cn.json | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/renderer/src/i18n/locales/zh-cn.json b/src/renderer/src/i18n/locales/zh-cn.json index dbb83cb29d..743c1d7621 100644 --- a/src/renderer/src/i18n/locales/zh-cn.json +++ b/src/renderer/src/i18n/locales/zh-cn.json @@ -1559,9 +1559,13 @@ "tip": "如果响应成功,则只针对超过30秒的消息进行提醒" }, "ocr": { + "error": { + "unknown": "OCR过程发生错误" + }, "file": { "not_supported": "不支持的文件类型 {{type}}" - } + }, + "processing": "OCR 处理中..." }, "ollama": { "keep_alive_time": {