xiaochan 7 месяцев назад
Родитель
Сommit
e194a4eeaa

+ 0 - 3
thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/tts/Tts.java

@@ -8,8 +8,5 @@ public interface Tts {
 
     ResultVo<String> textToSpeech(TtsRequest request, HttpServletResponse response);
 
-    default void init(TtsConfig config, TtsConfig.Config currentConfig) {
-
-    }
 
 }

+ 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;
 }

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

@@ -1,54 +1,25 @@
 package org.thyy.thirdpartapi.tts;
 
-import jakarta.annotation.PostConstruct;
 import jakarta.servlet.http.HttpServletResponse;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 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("/xfTtsApi")
 @Slf4j
 public class TtsController {
 
-    private final Map<String, Tts> ttsServices;
-    private final TtsConfig ttsConfig;
     private final Tts ttsService;
 
-
-    public TtsController(Map<String, Tts> ttsServices, TtsConfig ttsConfig, Tts ttsService) {
-        this.ttsServices = ttsServices;
-        this.ttsConfig = ttsConfig;
+    public TtsController(Tts ttsService) {
         this.ttsService = 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;
-//            }
-//        }
-//    }
-
 
     @PostMapping("/textToSpeech")
     public ResultVo<String> textToSpeech(@RequestBody TtsRequest request, HttpServletResponse response) {

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

@@ -3,20 +3,20 @@ package org.thyy.thirdpartapi.tts.ttsService;
 import jakarta.servlet.http.HttpServletResponse;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.context.annotation.Configuration;
 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")
-@Configuration
-@ConditionalOnProperty(prefix = "tts", name = "service", havingValue = "ttstext")
+@ConditionalOnProperty(prefix = "thyy.tts", name = "service", havingValue = "default", matchIfMissing = true)
 public class TtsTest implements Tts {
+
     @Override
     public ResultVo<String> textToSpeech(TtsRequest request, HttpServletResponse response) {
-        return R.ok("test");
+        return R.fail(ExceptionEnum.NOT_SERVICE);
     }
 }

+ 48 - 42
thyy-thirdpart-api/src/main/java/org/thyy/thirdpartapi/tts/ttsService/XfTtsApi.java

@@ -11,7 +11,6 @@ import okhttp3.Response;
 import okhttp3.WebSocket;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.context.annotation.Configuration;
-import org.springframework.stereotype.Service;
 import org.thyy.thirdpartapi.tts.Tts;
 import org.thyy.thirdpartapi.tts.TtsConfig;
 import org.thyy.thirdpartapi.tts.TtsRequest;
@@ -25,21 +24,24 @@ import java.util.concurrent.ExecutionException;
 import java.util.concurrent.atomic.AtomicReference;
 
 @Slf4j
-@Service(value = "xf")
 @Configuration
-@ConditionalOnProperty(prefix = "tts", name = "service", matchIfMissing = true, havingValue = "xf")
+@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) {
@@ -49,46 +51,50 @@ public class XfTtsApi implements Tts {
 
 
     public ResultVo<String> textToSpeech(TtsRequest request, HttpServletResponse response) {
+
+        if (ttsClient == null) {
+            return R.fail(ExceptionEnum.LOGICAL_ERROR, "未发现服务");
+        }
+
         String filePath = cfg.getDirectory() + File.separator + request.getId() + ".mp3";
         FileUtil.del(filePath);
 
         CompletableFuture<Void> future = new CompletableFuture<>();
 
-//        try {
-//            File file = new File(filePath);
-//            ttsClient.send(request.getText(), new AbstractTtsWebSocketListener(file) {
-//                @Override
-//                public void onSuccess(byte[] bytes) {
-//                }
-//
-//                @Override
-//                public void onFail(WebSocket webSocket, Throwable throwable, Response response) {
-//                    log.error("[onFail]生成语音文件失败:{}", throwable.getMessage());
-//                }
-//
-//                @Override
-//                public void onBusinessFail(WebSocket webSocket, TtsResponse ttsResponse) {
-//                    log.error("[onBusinessFail]生成语音文件失败:{}", ttsResponse.toString());
-//                }
-//
-//                @Override
-//                public void onClosing(WebSocket webSocket, int code, String reason) {
-//                    super.onClosing(webSocket, code, reason);
-//                }
-//
-//                @Override
-//                public void onClosed(WebSocket webSocket, int code, String reason) {
-//                    super.onClosed(webSocket, code, reason);
-//                    future.complete(null);
-//                }
-//            });
-//        } catch (Exception e) {
-//            log.error("生成语音文件失败", e);
-//            log.error("错误码查询链接:https://www.xfyun.cn/document/error-code");
-//            future.completeExceptionally(e);
-//        }
-
-        future.complete(null);
+        try {
+            File file = new File(filePath);
+            ttsClient.send(request.getText(), new AbstractTtsWebSocketListener(file) {
+                @Override
+                public void onSuccess(byte[] bytes) {
+                }
+
+                @Override
+                public void onFail(WebSocket webSocket, Throwable throwable, Response response) {
+                    log.error("[onFail]生成语音文件失败:{}", throwable.getMessage());
+                }
+
+                @Override
+                public void onBusinessFail(WebSocket webSocket, TtsResponse ttsResponse) {
+                    log.error("[onBusinessFail]生成语音文件失败:{}", ttsResponse.toString());
+                }
+
+                @Override
+                public void onClosing(WebSocket webSocket, int code, String reason) {
+                    super.onClosing(webSocket, code, reason);
+                }
+
+                @Override
+                public void onClosed(WebSocket webSocket, int code, String reason) {
+                    super.onClosed(webSocket, code, reason);
+                    future.complete(null);
+                }
+            });
+        } catch (Exception e) {
+            log.error("生成语音文件失败", e);
+            log.error("错误码查询链接:https://www.xfyun.cn/document/error-code");
+            future.completeExceptionally(e);
+        }
+
 
         String voiceUrl = cfg.getSpeechUrl() + "/" + request.getId() + ".mp3";
         AtomicReference<ResultVo<String>> resultVo = new AtomicReference<>(R.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, ""));

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

@@ -6,15 +6,12 @@ server:
   servlet:
     context-path: /thyy/thirdpart/api
 
-tts:
-  service: ttstext
 
 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

+ 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, "错误。"),