From 2ded5d21e98dbf26f3496cb268038ffea17d6a8c Mon Sep 17 00:00:00 2001 From: wsb1224 Date: Sat, 4 Jul 2026 09:55:03 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9C=AC=E5=9C=B0=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=9B=BE=E7=89=87OCR=E6=97=B6=E7=9B=B4=E6=8E=A5=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E6=9C=AC=E5=9C=B0=E6=96=87=E4=BB=B6=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8DHTTP=20404?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 本地上传的图片URL(/uploads/路径)直接从local_upload_dir读取, 不再通过公网域名下载,解决后端无法访问自己域名导致404的问题。 Co-Authored-By: Claude Sonnet 4.6 --- backend/app/services/logistics_service.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/backend/app/services/logistics_service.py b/backend/app/services/logistics_service.py index 1ce26d7..ef3d6e9 100644 --- a/backend/app/services/logistics_service.py +++ b/backend/app/services/logistics_service.py @@ -825,7 +825,8 @@ class LogisticsService: """ import base64 as _b64 import hashlib as _md5 - from urllib.parse import urlencode + from pathlib import Path as _Path, PurePosixPath as _URLPath + from urllib.parse import urlencode, urlparse image_url = (payload.get("image_url") or "").strip() if not image_url: @@ -842,11 +843,18 @@ class LogisticsService: # 1. 下载图片并 base64 编码 logger.info("[recognize_waybill] 下载图片: %s", image_url) try: - with request.urlopen(image_url, timeout=15) as resp: - image_bytes = resp.read() - logger.info("[recognize_waybill] 图片下载成功, 大小: %d bytes", len(image_bytes)) + url_path = _URLPath(urlparse(image_url).path) + if len(url_path.parts) >= 2 and url_path.parts[1] == "uploads": + # 本地上传的文件,直接从本地文件系统读取 + local_file = _Path(settings.local_upload_dir) / url_path.name + logger.info("[recognize_waybill] 本地文件读取: %s", local_file) + image_bytes = local_file.read_bytes() + else: + with request.urlopen(image_url, timeout=15) as resp: + image_bytes = resp.read() + logger.info("[recognize_waybill] 图片读取成功, 大小: %d bytes", len(image_bytes)) except Exception as img_exc: - logger.warning("[recognize_waybill] 图片下载失败: %s", img_exc) + logger.warning("[recognize_waybill] 图片读取失败: %s", img_exc) raise image_b64 = _b64.b64encode(image_bytes).decode("utf-8")