fix(aiot): snap端点支持图片直接返回,兼容前端<img>标签直接加载

前端将snap API URL直接设为<img src>,浏览器以Accept: image/*请求,
但原接口返回JSON导致图片无法显示。现通过检测Accept头自动判断:
- image/*请求:触发截图后直接返回JPEG字节流
- JSON请求:返回原有JSON格式

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-28 14:38:15 +08:00
parent b47d01fc30
commit c932c1c7a2

View File

@@ -85,10 +85,26 @@ public class AiRoiController {
@Operation(summary = "获取摄像头截图Edge截图 → COS URL")
@GetMapping("/snap")
public Map<String, Object> getSnap(
public ResponseEntity<?> getSnap(
@RequestParam String cameraCode,
@RequestParam(defaultValue = "false") boolean force) {
return screenshotService.requestScreenshot(cameraCode, force);
@RequestParam(defaultValue = "false") boolean force,
@RequestHeader(value = "Accept", defaultValue = "application/json") String accept) {
Map<String, Object> result = screenshotService.requestScreenshot(cameraCode, force);
// 如果是 <img> 标签发起的请求Accept: image/*),直接返回图片字节
if (accept.contains("image/") && "ok".equals(result.get("status"))) {
byte[] image = screenshotService.proxyScreenshotImage(cameraCode);
if (image != null) {
return ResponseEntity.ok()
.contentType(MediaType.IMAGE_JPEG)
.header("Cache-Control", "max-age=60")
.body(image);
}
}
// 默认返回 JSONXHR/fetch 请求)
return ResponseEntity.ok(result);
}
@Operation(summary = "Edge 截图回调Edge 主动调用)")