|
@@ -0,0 +1,139 @@
|
|
|
|
|
+package cn.tr.module.smart.coze.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import cn.tr.core.pojo.CommonResult;
|
|
|
|
|
+import cn.tr.module.smart.coze.config.CozeOAuthTokenConfig;
|
|
|
|
|
+import cn.tr.module.smart.coze.properties.CozeProperties;
|
|
|
|
|
+import cn.tr.module.smart.coze.service.CozeService;
|
|
|
|
|
+import cn.tr.module.sys.storage.dto.SysStorageRecordDTO;
|
|
|
|
|
+import cn.tr.module.sys.storage.service.IStorageFileService;
|
|
|
|
|
+import cn.tr.module.sys.tenant.dto.OAuthTokenAddCacheDTO;
|
|
|
|
|
+import com.coze.openapi.client.workflows.run.RunWorkflowReq;
|
|
|
|
|
+import com.coze.openapi.client.workflows.run.RunWorkflowResp;
|
|
|
|
|
+import com.coze.openapi.service.auth.TokenAuth;
|
|
|
|
|
+import com.coze.openapi.service.service.CozeAPI;
|
|
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.util.unit.DataSize;
|
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
+
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @Author zzy
|
|
|
|
|
+ * @Data 2025/7/2
|
|
|
|
|
+ * @Version 1.0
|
|
|
|
|
+ * @Description XXX
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class CozeServiceimpl implements CozeService {
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private CozeProperties cozeProperties;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private IStorageFileService fileService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private CozeOAuthTokenConfig cozeOAuthTokenConfig;
|
|
|
|
|
+
|
|
|
|
|
+ public CommonResult<Map<String, Object>> recognizeImage(MultipartFile file, String configId, String cateId) throws Exception {
|
|
|
|
|
+
|
|
|
|
|
+ // 验证文件
|
|
|
|
|
+ validateImageFile(file);
|
|
|
|
|
+
|
|
|
|
|
+ String imageUrl;
|
|
|
|
|
+ SysStorageRecordDTO uploadResult = fileService.upload(
|
|
|
|
|
+ configId,
|
|
|
|
|
+ cateId,
|
|
|
|
|
+ file.getOriginalFilename(),
|
|
|
|
|
+ file.getBytes(),
|
|
|
|
|
+ null);
|
|
|
|
|
+
|
|
|
|
|
+ imageUrl = uploadResult.getAbsolutePath();
|
|
|
|
|
+ //获取token
|
|
|
|
|
+ OAuthTokenAddCacheDTO oauthToken = cozeOAuthTokenConfig.getAccessToken();
|
|
|
|
|
+ String accessToken = oauthToken.getAccessToken();
|
|
|
|
|
+ TokenAuth tokenAuth = new TokenAuth(accessToken);
|
|
|
|
|
+
|
|
|
|
|
+ CozeAPI coze = new CozeAPI.Builder()
|
|
|
|
|
+ .baseURL(cozeProperties.getBaseUrl())
|
|
|
|
|
+ .auth(tokenAuth)
|
|
|
|
|
+ .readTimeout(60000) // 将超时时间设置为60000毫秒(60秒)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ Map<String, Object> parameters = new HashMap<>();
|
|
|
|
|
+ parameters.put(cozeProperties.getImageParamName(), imageUrl);
|
|
|
|
|
+
|
|
|
|
|
+ // 执行工作流
|
|
|
|
|
+ RunWorkflowResp workflowResp = coze.workflows().runs().create(
|
|
|
|
|
+ RunWorkflowReq.builder()
|
|
|
|
|
+ .workflowID(cozeProperties.getWorkflowId())
|
|
|
|
|
+ .parameters(parameters)
|
|
|
|
|
+ .build()
|
|
|
|
|
+ );
|
|
|
|
|
+ String data = workflowResp.getData();
|
|
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
+ Map<String, Object> responseMap = objectMapper.readValue(data, new TypeReference<Map<String, Object>>() {
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ String outputJson = (String) responseMap.get("output");
|
|
|
|
|
+
|
|
|
|
|
+ List<Map<String, Object>> patientList = objectMapper.readValue(
|
|
|
|
|
+ outputJson,
|
|
|
|
|
+ new TypeReference<List<Map<String, Object>>>() {
|
|
|
|
|
+ }
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
|
|
+ result.put("count", patientList.size());
|
|
|
|
|
+ result.put("patients", patientList);
|
|
|
|
|
+ return CommonResult.success(result);
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ private void validateImageFile(MultipartFile file) throws IOException {
|
|
|
|
|
+ // 检查文件大小
|
|
|
|
|
+ long maxSize = DataSize.parse(cozeProperties.getMaxFileSize()).toBytes();
|
|
|
|
|
+ if (file.getSize() > maxSize) {
|
|
|
|
|
+ throw new IOException("文件大小超过限制: " + cozeProperties.getMaxFileSize());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 检查文件类型
|
|
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
|
|
+ if (fileName == null) {
|
|
|
|
|
+ throw new IOException("无效的文件名");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ String extension = getFileExtension(fileName);
|
|
|
|
|
+ if (!isValidFileType(extension)) {
|
|
|
|
|
+ throw new IOException("不支持的文件类型. 支持的类型: " +
|
|
|
|
|
+ String.join(", ", cozeProperties.getAllowedFileTypes()));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String getFileExtension(String fileName) {
|
|
|
|
|
+ int dotIndex = fileName.lastIndexOf(".");
|
|
|
|
|
+ if (dotIndex > 0 && dotIndex < fileName.length() - 1) {
|
|
|
|
|
+ return fileName.substring(dotIndex + 1).toLowerCase();
|
|
|
|
|
+ }
|
|
|
|
|
+ return "";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private boolean isValidFileType(String extension) {
|
|
|
|
|
+ for (String allowedType : cozeProperties.getAllowedFileTypes()) {
|
|
|
|
|
+ if (allowedType.equalsIgnoreCase(extension)) {
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|