fix(food-encyclopedia): 前台食物图片输出归一化,修复uniapp部分图片不显示

- ToolFoodServiceImpl 新增 normalizeImageForOutput:定位 "crmebimage/" 片段,剥掉前面所有 host 前缀,再用 prefixImage 拼上单层 cdnUrl
- 应用于 search / getDetail / getSimilar 三处 image 输出
- 处理三类历史脏数据:相对路径、双层 host 前缀、AI OSS URL(原样透传)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
msh-agent
2026-05-03 01:19:07 +08:00
parent a3b609e70a
commit e9c2404b2b

View File

@@ -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<String, Object> 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<String, Object> 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<String, Object> 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);