Pārlūkot izejas kodu

Merge branch 'TestConditionalOnProperty'

# Conflicts:
#	thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/tts/ttsService/TtsTest.java
xiaochan 4 mēneši atpakaļ
vecāks
revīzija
4b91738e82

+ 8 - 16
thyy-thirdpart-api/HELP.md

@@ -1,27 +1,19 @@
 # 关于tts语言生成的配置注意
 
-1. 需要实现 这个接口
+1. 需要实现 这个接口 
     ```java
+    import org.springframework.stereotype.Service;
+   
     @Slf4j
-    // 服务的名字就是配置文件中的名字
-    @Service(value = "xf")
+    @Service
+    @ConditionalOnProperty(prefix = "thyy.tts", name = "service", havingValue = "xfyun")
     public class XfTtsApi implements Tts {
         
     }
     ```
 2. 配置文件中
    ```yaml
-   thyy:
-     tts:
-       directory: D:\a.snapshot\resource\voice #文件存放的路径
-       speech-url: http://172.16.32.160:8080/resource/voice # 生成的地址
-       config:
-         xf:  # 这个key就是上面服务的名字不要写错
-           app-id: fdde4cef
-           api-key: 95a78dd3cfcc4863e3c003352ca8ec65
-           api-secret: OWZjZGE4NjI3MDdkYzg4ZjllY2VjNGQ0
-         xxx:
-           app-id: xxxx
-           api-key: xxxxx
-           api-secret: xxxxx
+    thyy:
+      tts:  
+        service: xfyun
    ```

+ 0 - 14
thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/inspection/InspectionController.java

@@ -23,20 +23,6 @@ public class InspectionController {
         this.service = service;
     }
 
-    @Service
-    @ConditionalOnProperty(prefix = "thyy.inspection", name = "service", havingValue = "default", matchIfMissing = true)
-    static class Default implements InspectionInterface {
-        @Override
-        public List<JyIndexResponse> queryExamIndex(JSONObject request) {
-            return List.of();
-        }
-
-        @Override
-        public JyDetailResponse queryExamDetail(JSONObject request) {
-            return null;
-        }
-    }
-
     @PostMapping("/queryExamIndex")
     public ResultVo<List<JyIndexResponse>> queryExamIndex(@RequestBody JSONObject request) {
         List<JyIndexResponse> list = service.queryExamIndex(request);

+ 11 - 2
thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/inspection/inspectionImpl/ThyyInspectionImpl.java

@@ -1,5 +1,6 @@
 package org.thyy.thirdpartapi.inspection.inspectionImpl;
 
+import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONObject;
 import lombok.extern.slf4j.Slf4j;
@@ -13,11 +14,12 @@ import org.thyy.thirdpartapi.inspection.response.jy.*;
 import org.thyy.utils.exception.BizException;
 import org.thyy.utils.exception.ExceptionEnum;
 
+import java.util.ArrayList;
 import java.util.List;
 
 @Slf4j
 @Service
-@ConditionalOnProperty(prefix = "thyy.inspection", name = "service", havingValue = "shanghaihaotai")
+@ConditionalOnProperty(prefix = "thyy.inspection", name = "service", havingValue = "shanghaihaotai", matchIfMissing = true)
 public class ThyyInspectionImpl implements InspectionInterface {
     private String api;
     private final RestTemplate restTemplate;
@@ -30,13 +32,17 @@ public class ThyyInspectionImpl implements InspectionInterface {
         init();
     }
 
-    public void init( ) {
+    public void init() {
         this.api = inspectionApiConfig.getJy();
         log.info("检验检查模块初始化完成");
     }
 
     @Override
     public List<JyIndexResponse> queryExamIndex(JSONObject request) {
+        if (StrUtil.isBlank(api)) {
+            return new ArrayList<>();
+        }
+
         JSONObject response = restTemplate.postForObject(api + "/self", request, JSONObject.class);
         if (null == response) {
             throw new BizException(ExceptionEnum.NETWORK_ERROR);
@@ -52,6 +58,9 @@ public class ThyyInspectionImpl implements InspectionInterface {
 
     @Override
     public JyDetailResponse queryExamDetail(JSONObject request) {
+        if (StrUtil.isBlank(api)) {
+            return null;
+        }
         JSONObject response = new RestTemplate().postForObject(
                 api + "/detail", request, JSONObject.class);
         if (null == response) {

+ 14 - 1
thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/tts/TtsConfig.java

@@ -1,6 +1,8 @@
 package org.thyy.thirdpartapi.tts;
 
+import lombok.AllArgsConstructor;
 import lombok.Data;
+import lombok.Getter;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
 
@@ -9,9 +11,20 @@ import org.springframework.stereotype.Component;
 @Data
 @ConfigurationProperties("thyy.tts")
 public class TtsConfig {
+
+    @AllArgsConstructor
+    @Getter
+    public static enum ServiceName {
+        xfyun("xfyun"),
+        ttstext("ttstext");
+        private final String name;
+    }
+
     private String directory;
     private String speechUrl;
-    private String service;
+    private ServiceName service;
+
+
     private String appId;
     private String apiKey;
     private String apiSecret;

+ 0 - 26
thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/tts/ttsService/TtsTest.java

@@ -1,26 +0,0 @@
-package org.thyy.thirdpartapi.tts.ttsService;
-
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.stereotype.Service;
-import org.thyy.thirdpartapi.tts.Tts;
-import org.thyy.thirdpartapi.tts.TtsRequest;
-import org.thyy.utils.exception.ExceptionEnum;
-import org.thyy.utils.result.R;
-import org.thyy.utils.result.ResultVo;
-
-@Slf4j
-@Service(value = "ttstext")
-@ConditionalOnProperty(prefix = "thyy.tts", name = "service", havingValue = "default", matchIfMissing = true)
-public class TtsTest implements Tts {
-
-    @Override
-    public ResultVo<String> textToSpeech(TtsRequest request) {
-        return R.fail(ExceptionEnum.NOT_SERVICE);
-    }
-
-    @Override
-    public ResultVo<String> clearAudioDirectory() {
-        return R.fail(ExceptionEnum.NOT_SERVICE);
-    }
-}

+ 5 - 1
thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/tts/ttsService/XfTtsApi.java

@@ -25,7 +25,7 @@ import java.util.concurrent.atomic.AtomicReference;
 
 @Slf4j
 @Configuration
-@ConditionalOnProperty(prefix = "thyy.tts", name = "service", havingValue = "xfyun")
+@ConditionalOnProperty(prefix = "thyy.tts", name = "service", havingValue = "xfyun", matchIfMissing = true)
 public class XfTtsApi implements Tts {
     private TtsClient ttsClient;
     private final TtsConfig cfg;
@@ -52,6 +52,10 @@ public class XfTtsApi implements Tts {
 
     @Override
     public ResultVo<String> textToSpeech(TtsRequest request) {
+        if (StrUtil.isBlank(cfg.getApiKey())) {
+            return R.fail(ExceptionEnum.API_ERROR, "没有有效的Key。");
+        }
+
         String filePath = cfg.getDirectory() + File.separator + request.getId() + ".mp3";
         FileUtil.del(filePath);
         CompletableFuture<Void> future = new CompletableFuture<>();