Browse Source

feat(aiyunyi): S-cfc-496 ai.aiyunyi.upload 照片上传代理转发契约闭环

- AicdsController 新增 POST /api/aiyunyi/upload 端点(@RequestParam file 必填 / path 选填 / @RequestAttribute role 闸门)
- AicdsDetectionService 新增 uploadFile 经网关转发
- AicdsGatewayService 新增 uploadFile 转发到平台 /app-api/detection/package/enter/upload
  (B.7 明确 /app-api/ 前缀;平台免登录 PermitAll,只保留网关签名头,不携带业务鉴权头)
- 红单测 AiyunyiUploadContractTest 12 跑 12 过(RED 10 失败 -> GREEN 0 失败)
Agnes Agent 16 hours ago
parent
commit
4fc5c828fb

+ 29 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/AicdsController.java

@@ -8,7 +8,9 @@ import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestAttribute;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import java.util.Map;
@@ -62,4 +64,31 @@ public class AicdsController {
     private boolean isAllowedRole(String role) {
         return role != null && !role.trim().isEmpty() && !"anonymous".equalsIgnoreCase(role.trim());
     }
+
+    /**
+     * 照片上传入口:{@code POST /api/aiyunyi/upload}(能力键 {@code ai.aiyunyi.upload},S-cfc-496)。
+     *
+     * <p>前端「AI 快速识别」先经此端点把照片转发到平台 uploadFile 拿到图片 URL,
+     * 再把 URL 填入 {@code enter/detection_data} 的 {@code *UrlList[].url}(§3.3 / B.7)。</p>
+     *
+     * @param file 待上传照片(multipart/form-data,必填)
+     * @param path 存储路径/文件名(选填)
+     * @param role JwtInterceptor 注入的登录角色,anonymous/空白一律 403
+     * @return 统一 {@code Result},data 为平台返回的图片 URL
+     */
+    @PostMapping("/upload")
+    public Result<Object> upload(@RequestParam("file") MultipartFile file,
+                                 @RequestParam(value = "path", required = false) String path,
+                                 @RequestAttribute("role") String role) {
+        if (!isAllowedRole(role)) {
+            return Result.forbidden("无权限上传爱云医照片");
+        }
+        try {
+            String imageUrl = aicdsDetectionService.uploadFile(file, path);
+            return Result.success(imageUrl);
+        } catch (Exception e) {
+            log.warn("爱云医照片上传失败: {}", e.getMessage());
+            return Result.error("爱云医照片上传失败:" + e.getMessage());
+        }
+    }
 }

+ 16 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/AicdsDetectionService.java

@@ -5,6 +5,7 @@ import com.etotem.cfc.entity.AiYunyiMapping;
 import com.etotem.cfc.mapper.AiYunyiMappingMapper;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import java.util.ArrayList;
@@ -419,4 +420,19 @@ public class AicdsDetectionService {
                     "爱云医视频检测 getPath 返回空白 data.path,禁止静默返回空 URL 导致 web-view 白屏(§4.5)");
         }
     }
+
+    /**
+     * 照片上传(能力键 {@code ai.aiyunyi.upload},S-cfc-496):经网关转发到平台 uploadFile
+     * 拿图片 URL,供前端「AI 快速识别」在提交检测数据前使用(§3.3 / §4.1)。
+     *
+     * @param file 待上传照片(必填)
+     * @param path 存储路径/文件名(选填)
+     * @return 平台返回的图片 URL
+     */
+    public String uploadFile(MultipartFile file, String path) {
+        if (aicdsGatewayService == null) {
+            throw new IllegalStateException("爱云医网关未注入,无法上传照片(B.7)");
+        }
+        return aicdsGatewayService.uploadFile(file, path);
+    }
 }

+ 90 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/AicdsGatewayService.java

@@ -4,6 +4,7 @@ import com.etotem.cfc.config.AiYunyiProperties;
 import com.etotem.cfc.dto.AiyunyiTokenPair;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.core.io.ByteArrayResource;
 import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpMethod;
@@ -11,8 +12,11 @@ import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.http.client.SimpleClientHttpRequestFactory;
 import org.springframework.stereotype.Service;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
 import org.springframework.web.client.RestClientException;
 import org.springframework.web.client.RestTemplate;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.lang.reflect.Array;
 import java.nio.charset.StandardCharsets;
@@ -638,4 +642,90 @@ public class AicdsGatewayService {
         }
         return false;
     }
+
+    // ==================== 照片上传能力(ai.aiyunyi.upload,S-cfc-496) ====================
+
+    /** B.7:平台 uploadFile 转发路径(注意 {@code /app-api/} 前缀,非 {@code /open-api/})。 */
+    private static final String PATH_UPLOAD_FILE = "/app-api/detection/package/enter/upload";
+
+    /**
+     * 能力键 {@code ai.aiyunyi.upload}(S-cfc-496):契约 B.7 —— 照片上传代理。
+     *
+     * <p>平台 {@code uploadFile} 为免登录 PermitAll 接口:multipart/form-data,
+     * {@code file} 必填 / {@code path} 选填。转发请求只保留网关签名头
+     * ({@code X-App-Key / X-Timestamp / X-Api-Sign}),刻意<b>不</b>附加业务鉴权头
+     * (免登录接口不接收业务令牌 / 用户加密头)。</p>
+     *
+     * <p>响应 {@code {"code":0,"data":"https://.../xxx.jpg","msg":""}},
+     * {@code data} 即图片 URL,直接填入 {@code enter/detection_data} 的
+     * {@code *UrlList[].url}(§3.3 / §4.1)。</p>
+     *
+     * @param file 待上传照片(必填)
+     * @param path 存储路径/文件名(选填,如 a.jpg)
+     * @return 平台返回的图片 URL
+     */
+    public String uploadFile(MultipartFile file, String path) {
+        if (file == null || file.isEmpty()) {
+            throw new IllegalArgumentException("爱云医照片上传缺少 file(B.7 必填)");
+        }
+        if (isCircuitOpen()) {
+            throw new IllegalStateException("爱云医网关熔断中,拒绝发起照片上传");
+        }
+        String appKey = resolveAppKey();
+        String timestamp = String.valueOf(System.currentTimeMillis());
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
+        headers.set(H_APP_KEY, appKey);
+        headers.set(H_TIMESTAMP, timestamp);
+        headers.set(H_API_SIGN, resolveSign(appKey, timestamp));
+
+        MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>();
+        form.add("file", toUploadResource(file));
+        if (!isBlank(path)) {
+            form.add("path", path);
+        }
+        HttpEntity<MultiValueMap<String, Object>> entity =
+                new HttpEntity<MultiValueMap<String, Object>>(form, headers);
+        String url = resolveBaseUrl() + PATH_UPLOAD_FILE;
+        try {
+            ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.POST, entity,
+                    Map.class, new Object[0]);
+            consecutiveFailures.set(0);
+            return extractUploadImageUrl(response == null ? null : response.getBody());
+        } catch (RestClientException e) {
+            recordFailure();
+            throw e;
+        }
+    }
+
+    /** 把上传文件转为 multipart 资源,保留原始文件名(平台按后缀识别图片类型)。 */
+    private static ByteArrayResource toUploadResource(final MultipartFile file) {
+        try {
+            return new ByteArrayResource(file.getBytes()) {
+                @Override
+                public String getFilename() {
+                    return file.getOriginalFilename();
+                }
+            };
+        } catch (java.io.IOException e) {
+            throw new IllegalStateException("读取待上传照片失败", e);
+        }
+    }
+
+    /** 解析 uploadFile 响应 {@code {code,data,msg}},{@code data} 即图片 URL;非 0/缺失归一为异常。 */
+    private static String extractUploadImageUrl(Map<String, Object> body) {
+        if (body == null) {
+            throw new IllegalStateException("爱云医照片上传响应为空(B.7)");
+        }
+        Object code = body.get("code");
+        if (code instanceof Number && ((Number) code).intValue() != 0) {
+            throw new IllegalStateException("爱云医照片上传失败,code=" + code);
+        }
+        Object data = body.get("data");
+        String imageUrl = data == null ? null : data.toString();
+        if (imageUrl == null || imageUrl.trim().isEmpty()) {
+            throw new IllegalStateException("爱云医照片上传响应缺少 data 图片 URL(B.7)");
+        }
+        return imageUrl;
+    }
 }