diff --git a/msh_crmeb_22/crmeb-service/src/main/java/com/zbkj/service/service/impl/tool/ToolFoodServiceImpl.java b/msh_crmeb_22/crmeb-service/src/main/java/com/zbkj/service/service/impl/tool/ToolFoodServiceImpl.java index 914292b..80ab819 100644 --- a/msh_crmeb_22/crmeb-service/src/main/java/com/zbkj/service/service/impl/tool/ToolFoodServiceImpl.java +++ b/msh_crmeb_22/crmeb-service/src/main/java/com/zbkj/service/service/impl/tool/ToolFoodServiceImpl.java @@ -7,6 +7,7 @@ import com.zbkj.common.exception.CrmebException; import com.zbkj.common.model.tool.V2Food; import com.zbkj.common.request.PageParamRequest; import com.zbkj.service.dao.tool.V2FoodDao; +import com.zbkj.service.service.SystemAttachmentService; import com.zbkj.service.service.tool.DishImageService; import com.zbkj.service.service.tool.ToolFoodService; import lombok.extern.slf4j.Slf4j; @@ -34,6 +35,29 @@ public class ToolFoodServiceImpl implements ToolFoodService { @Resource private DishImageService dishImageService; + @Resource + private SystemAttachmentService systemAttachmentService; + + /** + * 输出端图片归一化: + * - 空值原样返回 + * - data:/blob: 原样返回 + * - 历史脏数据中包含多层 host 前缀的 "https://h//https://h//crmebimage/..." → 仅保留首段 cdnUrl + 'crmebimage/...' + * - 相对路径 "crmebimage/..." → 拼上 cdnUrl + * - AI 生成 OSS URL(不含 crmebimage/,如 https://uthink.../foods/...)原样返回 + */ + private String normalizeImageForOutput(String image) { + if (StrUtil.isBlank(image)) return image; + String s = image.trim(); + if (s.startsWith("data:") || s.startsWith("blob:")) return s; + int idx = s.indexOf("crmebimage/"); + if (idx >= 0) { + // 剥掉 crmebimage/ 之前的所有内容(包含 http(s)://host// 等),再交由 prefixImage 拼上单层 cdnUrl + return systemAttachmentService.prefixImage(s.substring(idx)); + } + return s; + } + /** * 搜索食物 * @param pageParamRequest 分页参数 @@ -63,7 +87,7 @@ public class ToolFoodServiceImpl implements ToolFoodService { Map map = new HashMap<>(); map.put("id", food.getFoodId()); map.put("name", food.getName()); - map.put("image", food.getImage()); + map.put("image", normalizeImageForOutput(food.getImage())); map.put("category", food.getCategory()); map.put("energy", food.getEnergy()); map.put("protein", food.getProtein()); @@ -108,7 +132,7 @@ public class ToolFoodServiceImpl implements ToolFoodService { Map map = new HashMap<>(); map.put("id", food.getFoodId()); map.put("name", food.getName()); - map.put("image", food.getImage()); + map.put("image", normalizeImageForOutput(food.getImage())); map.put("category", food.getCategory()); map.put("energy", food.getEnergy()); map.put("protein", food.getProtein()); @@ -153,7 +177,7 @@ public class ToolFoodServiceImpl implements ToolFoodService { Map map = new HashMap<>(); map.put("id", item.getFoodId()); map.put("name", item.getName()); - map.put("image", item.getImage()); + map.put("image", normalizeImageForOutput(item.getImage())); similarList.add(map); } result.put("list", similarList);