Selaa lähdekoodia

Merge branch 'TestConditionalOnProperty'

# Conflicts:
#	thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/tts/TtsController.java
#	thyy-thirdpart-api/src/main/resources/application.yml
xiaochan 4 kuukautta sitten
vanhempi
commit
806e3dbf2a

+ 1 - 6
thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/tts/Tts.java

@@ -1,15 +1,10 @@
 package org.thyy.thirdpartapi.tts;
 
-import jakarta.servlet.http.HttpServletResponse;
 import org.thyy.utils.result.ResultVo;
 
 
 public interface Tts {
 
-    ResultVo<String> textToSpeech(TtsRequest request, HttpServletResponse response);
-
-    default void init(TtsConfig config, TtsConfig.Config currentConfig) {
-
-    }
+    ResultVo<String> textToSpeech(TtsRequest request);
 
 }

+ 4 - 11
thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/tts/TtsConfig.java

@@ -4,22 +4,15 @@ import lombok.Data;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
 
-import java.util.Map;
 
 @Component
 @Data
 @ConfigurationProperties("thyy.tts")
 public class TtsConfig {
-
-    @Data
-    public static class Config {
-        private String appId;
-        private String apiKey;
-        private String apiSecret;
-    }
-
-    private Map<String, Config> config;
     private String directory;
     private String speechUrl;
-
+    private String service;
+    private String appId;
+    private String apiKey;
+    private String apiSecret;
 }

+ 6 - 30
thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/tts/TtsController.java

@@ -1,7 +1,5 @@
 package org.thyy.thirdpartapi.tts;
 
-import jakarta.annotation.PostConstruct;
-import jakarta.servlet.http.HttpServletResponse;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -9,44 +7,22 @@ import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.thyy.utils.result.ResultVo;
 
-import java.util.Map;
 
 @RestController
 @RequestMapping("/ttsApi")
 @Slf4j
 public class TtsController {
-    private final Map<String, Tts> ttsServices;
-    private final TtsConfig ttsConfig;
-    private Tts ttsService;
 
-    public TtsController(Map<String, Tts> ttsServices, TtsConfig ttsConfig) {
-        this.ttsServices = ttsServices;
-        this.ttsConfig = ttsConfig;
-    }
+    private final Tts ttsService;
 
-    @PostConstruct
-    public void init() {
-        for (Map.Entry<String, Tts> map : ttsServices.entrySet()) {
-            String key = map.getKey();
-            if (ttsConfig.getConfig() == null) {
-                return;
-            }
-            TtsConfig.Config config = ttsConfig.getConfig().get(key);
-            if (config != null) {
-                try {
-                    map.getValue().init(ttsConfig, config);
-                    ttsService = map.getValue();
-                } catch (Exception e) {
-                    log.error("语言合成错误", e);
-                }
-                return;
-            }
-        }
+    public TtsController(Tts ttsService) {
+        this.ttsService = ttsService;
     }
 
+
     @PostMapping("/textToSpeech")
-    public ResultVo<String> textToSpeech(@RequestBody TtsRequest request, HttpServletResponse response) {
-        return ttsService.textToSpeech(request, response);
+    public ResultVo<String> textToSpeech(@RequestBody TtsRequest request) {
+        return ttsService.textToSpeech(request);
     }
 
 }

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

@@ -0,0 +1,21 @@
+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);
+    }
+}

+ 14 - 8
thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/tts/ttsService/XfTtsApi.java

@@ -5,11 +5,11 @@ import cn.hutool.core.util.StrUtil;
 import cn.xfyun.api.TtsClient;
 import cn.xfyun.model.response.TtsResponse;
 import cn.xfyun.service.tts.AbstractTtsWebSocketListener;
-import jakarta.servlet.http.HttpServletResponse;
 import lombok.extern.slf4j.Slf4j;
 import okhttp3.Response;
 import okhttp3.WebSocket;
-import org.springframework.stereotype.Service;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.annotation.Configuration;
 import org.thyy.thirdpartapi.tts.Tts;
 import org.thyy.thirdpartapi.tts.TtsConfig;
 import org.thyy.thirdpartapi.tts.TtsRequest;
@@ -23,19 +23,24 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.atomic.AtomicReference;
 
 @Slf4j
-@Service(value = "xf")
+@Configuration
+@ConditionalOnProperty(prefix = "thyy.tts", name = "service", havingValue = "xfyun")
 public class XfTtsApi implements Tts {
     private TtsClient ttsClient;
-    private TtsConfig cfg;
+    private final TtsConfig cfg;
 
-    public void init(TtsConfig cfg, TtsConfig.Config xf) {
+    public XfTtsApi(TtsConfig cfg) {
+        this.cfg = cfg;
+        init();
+    }
+
+    public void init() {
         try {
-            this.cfg = cfg;
             if (StrUtil.isBlank(cfg.getDirectory())) {
                 cfg.setDirectory(System.getProperty("user.dir"));
             }
             ttsClient = new TtsClient.Builder()
-                    .signature(xf.getAppId(), xf.getApiKey(), xf.getApiSecret())
+                    .signature(cfg.getAppId(), cfg.getApiKey(), cfg.getApiSecret())
                     .vcn("aisjinger").volume(100).build();
             log.info("讯飞SDK初始化成功。");
         } catch (Exception e) {
@@ -44,7 +49,7 @@ public class XfTtsApi implements Tts {
     }
 
 
-    public ResultVo<String> textToSpeech(TtsRequest request, HttpServletResponse response) {
+    public ResultVo<String> textToSpeech(TtsRequest request) {
         String filePath = cfg.getDirectory() + File.separator + request.getId() + ".mp3";
         FileUtil.del(filePath);
 
@@ -84,6 +89,7 @@ public class XfTtsApi implements Tts {
             future.completeExceptionally(e);
         }
 
+
         String voiceUrl = cfg.getSpeechUrl() + "/" + request.getId() + ".mp3";
         AtomicReference<ResultVo<String>> resultVo = new AtomicReference<>(R.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, ""));
 

+ 7 - 6
thyy-thirdpart-api/src/main/resources/application.yml

@@ -6,17 +6,18 @@ server:
   servlet:
     context-path: /thyy/thirdpart/api
 
+
 thyy:
   tts:
+    service: xfyun
     directory: D:\a.snapshot\resource\voice
     speech-url: http://172.16.32.160:8080/resource/voice
-    config:
-      xf:
-        app-id: fdde4cef
-        api-key: 95a78dd3cfcc4863e3c003352ca8ec65
-        api-secret: OWZjZGE4NjI3MDdkYzg4ZjllY2VjNGQ0
+    app-id: fdde4cef
+    api-key: 95a78dd3cfcc4863e3c003352ca8ec65
+    api-secret: OWZjZGE4NjI3MDdkYzg4ZjllY2VjNGQ0
+
   api:
     inspection:
       config:
         csth:
-          jy: http://172.16.32.178/apis/third/report/query
+          jy: http://172.16.32.178/apis/third/report/query

+ 1 - 0
thyy-utils/src/main/java/org/thyy/utils/exception/ExceptionEnum.java

@@ -31,6 +31,7 @@ public enum ExceptionEnum implements ExceptionImp {
     NO_PERMISSION_TO_VIEW(1008, "无权查看。"),
     UNAUTHORIZED_OPERATION(1009, "无权操作。"),
     API_ERROR(1010, "第三方接口异常"),
+    NOT_SERVICE(1010, "没有这个服务"),
 
     // 以下是需要弹窗提示的错误
     LOGICAL_ERROR(2001, "错误。"),