deason пре 1 месец
родитељ
комит
5ad73cd638

+ 17 - 0
src/main/java/com/qmth/ops/biz/ai/client/DocApiClient.java

@@ -1,14 +1,17 @@
 package com.qmth.ops.biz.ai.client;
 
+import com.qmth.boot.core.ai.model.AiConstants;
 import com.qmth.boot.core.ai.model.doc.ParseDocTask;
 import com.qmth.boot.core.ai.model.doc.ParseDocTaskResult;
 import com.qmth.boot.core.exception.StatusException;
 import com.qmth.boot.core.rateLimit.service.RateLimiter;
 import com.qmth.boot.core.rateLimit.service.impl.MemoryRateLimiter;
+import com.qmth.boot.tools.codec.CodecUtils;
 import okhttp3.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.nio.charset.StandardCharsets;
 import java.time.Duration;
 
 /**
@@ -60,6 +63,20 @@ public abstract class DocApiClient {
         return null;
     }
 
+    public String encodeTaskId(String taskId) {
+        String str = getConfig().getSupplier() + AiConstants.PARAM_SPLIT + taskId;
+        return CodecUtils.toBase64(str.getBytes(StandardCharsets.UTF_8));
+    }
+
+    public String[] decodeTaskId(String encodeStr) {
+        String str = new String(CodecUtils.fromBase64(encodeStr), StandardCharsets.UTF_8);
+        String[] values = str.split(AiConstants.PARAM_SPLIT);
+        if (values.length != 3) {
+            throw new StatusException("taskId值无效!");
+        }
+        return values;
+    }
+
     protected byte[] download(String url) {
         Request request = new Request.Builder().url(url).get().build();
         try (Response response = this.getClient().newCall(request).execute();) {

+ 2 - 1
src/main/java/com/qmth/ops/biz/ai/client/DocApiConfig.java

@@ -1,5 +1,6 @@
 package com.qmth.ops.biz.ai.client;
 
+import com.qmth.boot.core.ai.model.AiConstants;
 import com.qmth.ops.biz.domain.OcrSupplier;
 
 public class DocApiConfig {
@@ -23,7 +24,7 @@ public class DocApiConfig {
         this.key = supplier.getKey();
         this.secret = supplier.getSecret();
         this.qps = supplier.getQps();
-        this.supplier = String.valueOf(supplier.getId());
+        this.supplier = supplier.getId() + AiConstants.PARAM_SPLIT + supplier.getClientClass();
     }
 
     public String getUrl() {

+ 4 - 3
src/main/java/com/qmth/ops/biz/ai/client/baidu/doc/BaiduParseDocClient.java

@@ -48,7 +48,7 @@ public class BaiduParseDocClient extends DocApiClient {
             if (response.isSuccessful()) {
                 BaiduParseDocTaskResp resp = new ObjectMapper().readValue(respBodyStr, BaiduParseDocTaskResp.class);
                 if (resp.getErrorCode() == 0) {
-                    return new ParseDocTask(resp.getResult().getTaskId());
+                    return new ParseDocTask(encodeTaskId(resp.getResult().getTaskId()));
                 }
             }
 
@@ -64,6 +64,7 @@ public class BaiduParseDocClient extends DocApiClient {
         if (getQueryRateLimiter() != null && !getQueryRateLimiter().acquire()) {
             throw new DocRateLimitExceeded(getConfig().getQps());
         }
+        taskId = decodeTaskId(taskId)[2];
 
         FormBody.Builder formBuilder = new FormBody.Builder();
         formBuilder.add("task_id", taskId);
@@ -121,14 +122,14 @@ public class BaiduParseDocClient extends DocApiClient {
         config.setKey("xxx");
         config.setSecret("xxx");
         config.setQps(10);
-        config.setSupplier("3");
+        config.setSupplier("3@@baidu.doc.BaiduParseDocClient");
         DocApiClient client = new BaiduParseDocClient(config);
 
         // File file = new File("D:\\home\\大纲.pdf");
         // byte[] fileData = ByteArray.fromFile(file).value();
         // System.out.println(client.parseDocTask(fileData, file.getName()).getTaskId());
 
-        ParseDocTaskResult result = client.parseDocTaskQuery("task-rmebOA853Uk592w9uTOLqV8Wtffb3HhR");
+        ParseDocTaskResult result = client.parseDocTaskQuery("M0BAYmFpZHUuZG9jLkJhaWR1UGFyc2VEb2NDbGllbnRAQHRhc2stdnVrV1RHZnNSbFNJdDFBaDNsRUxqNzRRczJzdXNhOXk=");
         System.out.println(result.getStatus());
         System.out.println(result.getContent());
     }

+ 1 - 1
src/main/java/com/qmth/ops/biz/service/DocClientService.java

@@ -50,7 +50,7 @@ public class DocClientService {
                 DocApiClient client = (DocApiClient) clientInstance;
                 clientMap.put(supplier.getId(), client);
                 // 取第一个enable=true的为默认客户端
-                if (supplier.getEnable() && defaultClient == null) {
+                if (defaultClient == null) {
                     defaultClient = client;
                 }
             }