zhangqiang пре 9 месеци
родитељ
комит
e9a62dbd62

+ 7 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/pom.xml

@@ -10,6 +10,7 @@
 
 	<artifactId>jeecg-system-biz</artifactId>
 
+
 	<dependencies>
 		<dependency>
 			<groupId>org.jeecgframework.boot</groupId>
@@ -44,6 +45,12 @@
 			<groupId>org.jeecgframework.jimureport</groupId>
 			<artifactId>jimureport-nosql-starter</artifactId>
 		</dependency>-->
+
+		<dependency>
+			<groupId>joda-time</groupId>
+			<artifactId>joda-time</artifactId>
+			<version>2.9.4</version>
+		</dependency>
 	</dependencies>
 	
 </project>

+ 0 - 2
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/okki/translate/controller/TranslateApiController.java

@@ -27,9 +27,7 @@ public class TranslateApiController {
         if (!authorization.equals(Authorization)) {
             return Result.error("sign 验证不通过");
         }
-
         TranslateResults translateResults = translateService.doTranslate(translateRequestDto);
-
         return Result.OK(translateResults);
     }
 }

+ 13 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/okki/translate/dto/AwsTranslateResult.java

@@ -0,0 +1,13 @@
+package org.jeecg.modules.okki.translate.dto;
+
+import lombok.Data;
+
+@Data
+public class AwsTranslateResult {
+
+    private String code;
+
+    private String message;
+
+    private String data;
+}

+ 15 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/okki/translate/param/AwsTranslateOptionParam.java

@@ -0,0 +1,15 @@
+package org.jeecg.modules.okki.translate.param;
+
+import lombok.Data;
+
+@Data
+public class AwsTranslateOptionParam {
+
+    private String sourceLanguage;
+
+    private String targetLanguage;
+
+    private String model;
+
+    private String mode;
+}

+ 11 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/okki/translate/param/AwsTranslateParam.java

@@ -0,0 +1,11 @@
+package org.jeecg.modules.okki.translate.param;
+
+import lombok.Data;
+
+@Data
+public class AwsTranslateParam {
+
+    private String text;
+
+    private AwsTranslateOptionParam option;
+}

+ 228 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/okki/translate/service/AwsTranslateService.java

@@ -0,0 +1,228 @@
+package org.jeecg.modules.okki.translate.service;
+
+import com.amazonaws.ClientConfiguration;
+import com.amazonaws.DefaultRequest;
+import com.amazonaws.Request;
+import com.amazonaws.Response;
+import com.amazonaws.auth.AWS4Signer;
+import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
+import com.amazonaws.http.AmazonHttpClient;
+import com.amazonaws.http.ExecutionContext;
+import com.amazonaws.http.HttpMethodName;
+import com.google.gson.Gson;
+import lombok.extern.slf4j.Slf4j;
+import org.jeecg.modules.okki.translate.param.AwsTranslateOptionParam;
+import org.jeecg.modules.okki.translate.param.AwsTranslateParam;
+import org.jeecg.modules.okki.translate.tools.ErrorHandler;
+import org.jeecg.modules.okki.translate.tools.ResponseHandler;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.net.URI;
+import java.util.*;
+
+@Slf4j
+@Service
+public class AwsTranslateService {
+
+    @Value("${aws.region}")
+    private String Region;
+
+    @Value("${aws.host}")
+    private String Host;
+
+    @Value("${aws.service}")
+    private String Service;
+
+    public String doSingleTranslate(String key, String contents, String fromLanguage, String targetLanguage , Map<String, Map<String, String>> translatedInfo, String originalValue) {
+        final AwsTranslateParam awsTranslateParam = new AwsTranslateParam();
+        final AwsTranslateOptionParam awsTranslateOptionParam = new AwsTranslateOptionParam();
+        awsTranslateOptionParam.setSourceLanguage(fromLanguage);
+        awsTranslateOptionParam.setTargetLanguage(targetLanguage);
+        awsTranslateOptionParam.setModel("CLAUDE_3_HAIKU");
+        awsTranslateOptionParam.setMode("TEXT");
+        awsTranslateParam.setOption(awsTranslateOptionParam);
+        awsTranslateParam.setText(contents);
+        Gson gson = new Gson();
+        String str = gson.toJson(awsTranslateParam);
+        InputStream is = new ByteArrayInputStream(str.getBytes());
+        Map<String, String> headerMap = new HashMap<>();
+        headerMap.put("content-type", "application/json");
+        Request<Void> request = new DefaultRequest<>(Service); //Request to Lambda
+        request.setHttpMethod(HttpMethodName.POST);
+        request.setContent(is);
+        request.setHeaders(headerMap);
+        request.setEndpoint(URI.create(Host + "/api/v1/generation/translate"));
+        //Sign it...
+        AWS4Signer signer = new AWS4Signer();
+        signer.setRegionName(Region);
+        signer.setServiceName(request.getServiceName());
+        // automatically get the credentials from Env for EC2/Lambda roles
+        signer.sign(request, new DefaultAWSCredentialsProviderChain().getCredentials());
+        //Execute it and get the response...
+        Response<String> rsp = new AmazonHttpClient(new ClientConfiguration())
+                .requestExecutionBuilder()
+                .executionContext(new ExecutionContext(true))
+                .request(request)
+                .errorResponseHandler(new ErrorHandler())
+                .execute(new ResponseHandler());
+        return rsp.getAwsResponse();
+    }
+
+//    static {
+//        LANGUAGE_MAP = new HashMap<>();
+//        LANGUAGE_MAP.put("zh", "ZH_CN");
+//        LANGUAGE_MAP.put("en", "EN_US");
+//        LANGUAGE_MAP.put("ar", "AR");
+//        LANGUAGE_MAP.put("bg", "BG");
+//        LANGUAGE_MAP.put("hr", "HR");
+//        LANGUAGE_MAP.put("cs", "CS");
+//        LANGUAGE_MAP.put("da", "DA");
+//        LANGUAGE_MAP.put("nl", "NL");
+//        LANGUAGE_MAP.put("fi", "FI");
+//        LANGUAGE_MAP.put("fr", "FR");
+//        LANGUAGE_MAP.put("de", "DE");
+//        LANGUAGE_MAP.put("el", "EL");
+//        LANGUAGE_MAP.put("hi", "HI");
+//        LANGUAGE_MAP.put("it", "IT");
+//        LANGUAGE_MAP.put("ja", "JA");
+//        LANGUAGE_MAP.put("ko", "KO");
+//        LANGUAGE_MAP.put("no", "NB");
+//        LANGUAGE_MAP.put("pl", "PL");
+//        LANGUAGE_MAP.put("pt", "PT");
+//        LANGUAGE_MAP.put("ro", "RO");
+//        LANGUAGE_MAP.put("ru", "RU");
+//        LANGUAGE_MAP.put("es", "ES");
+//        LANGUAGE_MAP.put("sv", "SV");
+//        LANGUAGE_MAP.put("ca", "CA");
+//        LANGUAGE_MAP.put("tl", "FIL");
+//        LANGUAGE_MAP.put("iw", "HE");
+//        LANGUAGE_MAP.put("id", "ID");
+//        LANGUAGE_MAP.put("lv", "LV");
+//        LANGUAGE_MAP.put("lt", "LT");
+//        LANGUAGE_MAP.put("sr", "SR");
+//        LANGUAGE_MAP.put("sk", "SK");
+//        LANGUAGE_MAP.put("sl", "SL");
+//        LANGUAGE_MAP.put("uk", "UK");
+//        LANGUAGE_MAP.put("vi", "VI");
+//        LANGUAGE_MAP.put("sq", "GL");
+//        LANGUAGE_MAP.put("hu", "HU");
+//        LANGUAGE_MAP.put("mt", "MT");
+//        LANGUAGE_MAP.put("th", "TH");
+//        LANGUAGE_MAP.put("tr", "TR");
+//        LANGUAGE_MAP.put("fa", "FA");
+//        LANGUAGE_MAP.put("af", "AF");
+//        LANGUAGE_MAP.put("ms", "MS");
+//        LANGUAGE_MAP.put("sw", "SW");
+//        LANGUAGE_MAP.put("ga", "GA");
+//        LANGUAGE_MAP.put("cy", "CY");
+//        LANGUAGE_MAP.put("be", "bel");
+//        LANGUAGE_MAP.put("is", "IS");
+//        LANGUAGE_MAP.put("mk", "MK");
+//        LANGUAGE_MAP.put("yi", "SQ");
+//        LANGUAGE_MAP.put("hy", "ET");
+//    }
+
+//    enum Language {
+//        @documentation("Chinese")
+//        ZH_CN
+//        @documentation("English")
+//        EN_US
+//        @documentation("Arabic")
+//                AR
+//        @documentation("Bulgarian")
+//        BG
+//        @documentation("Croatian")
+//                HR
+//        @documentation("Czech")
+//        CS
+//        @documentation("Danish")
+//                DA
+//        @documentation("Dutch")
+//        NL
+//        @documentation("Finnish")
+//                FI
+//        @documentation("French")
+//        FR
+//        @documentation("German")
+//                DE
+//        @documentation("Greek")
+//        EL
+//        @documentation("Hindi")
+//                HI
+//        @documentation("Italian")
+//        IT
+//        @documentation("Japanese")
+//                JA
+//        @documentation("Korean")
+//        KO
+//        @documentation("Norwegian")
+//                NB
+//        @documentation("Polish")
+//        PL
+//        @documentation("Portuguese")
+//                PT
+//        @documentation("Romanian")
+//        RO
+//        @documentation("Russian")
+//                RU
+//        @documentation("Spanish")
+//        ES
+//        @documentation("Swedish")
+//                SV
+//        @documentation("Catalan")
+//        CA
+//        @documentation("Filipino")
+//                FIL
+//        @documentation("Hebrew")
+//        HE
+//        @documentation("Indonesian")
+//                ID
+//        @documentation("Latvian")
+//        LV
+//        @documentation("Lithuanian")
+//                LT
+//        @documentation("Serbian")
+//        SR
+//        @documentation("Slovak")
+//                SK
+//        @documentation("Slovenian")
+//        SL
+//        @documentation("Ukrainian")
+//                UK
+//        @documentation("Vietnamese")
+//        VI
+//        @documentation("Galician")
+//                GL
+//        @documentation("Hungarian")
+//        HU
+//        @documentation("Maltese")
+//                MT
+//        @documentation("Thai")
+//        TH
+//        @documentation("Turkish")
+//                TR
+//        @documentation("Persian")
+//        FA
+//        @documentation("Afrikaans")
+//                AF
+//        @documentation("Malay")
+//        MS
+//        @documentation("Swahili")
+//                SW
+//        @documentation("Irish")
+//        GA
+//        @documentation("Welsh")
+//                CY
+//        @documentation("Icelandic")
+//        IS
+//        @documentation("Macedonian")
+//                MK
+//        @documentation("Albanian")
+//        SQ
+//        @documentation("Estonian")
+//                ET
+//        }
+}

+ 1 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/okki/translate/service/TranslateService.java

@@ -77,6 +77,7 @@ public class TranslateService {
 
     private void processTranslation(String key, String decodedSrc, String fromLanguage, String targetLanguage , String fromLanguageTransCode,  String targetLanguageTransCode, Map<String, Map<String, String>> translatedInfo, String originalValue) {
         try {
+            // 如果aws支持的语言走aws
             String transObj = transBaiduApi.getTransResult(decodedSrc, fromLanguageTransCode, targetLanguageTransCode);
 
             RransResultBaidu resultBaidu = JSON.parseObject(transObj, RransResultBaidu.class);

+ 25 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/okki/translate/tools/ErrorHandler.java

@@ -0,0 +1,25 @@
+package org.jeecg.modules.okki.translate.tools;
+
+import com.amazonaws.AmazonServiceException;
+import com.amazonaws.SdkBaseException;
+import com.amazonaws.http.HttpResponse;
+import com.amazonaws.http.HttpResponseHandler;
+
+public class ErrorHandler implements HttpResponseHandler<SdkBaseException> {
+    @Override
+    public SdkBaseException handle(HttpResponse httpResponse)  {
+        int status = httpResponse.getStatusCode();
+        String message = httpResponse.getStatusText();
+        if(status < 200 || status >= 300) {
+            AmazonServiceException ase = new AmazonServiceException("Unexpected status: " + status + " " + message);
+            ase.setStatusCode(status);
+            return ase;
+        }
+        return null;
+    }
+
+    @Override
+    public boolean needsConnectionLeftOpen() {
+        return false;
+    }
+}

+ 37 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/okki/translate/tools/ResponseHandler.java

@@ -0,0 +1,37 @@
+package org.jeecg.modules.okki.translate.tools;
+
+import com.amazonaws.AmazonServiceException;
+import com.amazonaws.http.HttpResponse;
+import com.amazonaws.http.HttpResponseHandler;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+
+public class ResponseHandler implements HttpResponseHandler<String> {
+    @Override
+    public String handle(HttpResponse httpResponse) throws Exception {
+        int status = httpResponse.getStatusCode();
+        if(status < 200 || status >= 300) {
+            AmazonServiceException ase = new AmazonServiceException("Unexpected status: " + status);
+            ase.setStatusCode(status);
+            throw ase;
+        }
+        InputStream stream = httpResponse.getContent();
+        InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
+        StringBuilder sb = new StringBuilder();
+        int byteRead;
+        // read chunks from chat stream
+        System.out.println("Reading from stream:");
+        while ((byteRead = reader.read()) != -1) {
+            System.out.print((char) byteRead);
+            sb.append((char) byteRead);
+        }
+        return sb.toString();
+    }
+
+    @Override
+    public boolean needsConnectionLeftOpen() {
+        return false;
+    }
+}

+ 7 - 1
jeecg-boot/jeecg-module-system/jeecg-system-start/src/main/resources/application-dev.yml

@@ -329,4 +329,10 @@ OKKI:
   BASE_URL_V1 : https://cms.dev.xiaoman.cn/shop-api/External/sitev1
   # 小满生产环境
 #  BASE_URL: https://cms.xiaoman.cn/shop-api/External/site
-#  CLIENT_SECRET: G8v7vplPP9cm7vgUFXqZJOB4TVjoomIy
+#  CLIENT_SECRET: G8v7vplPP9cm7vgUFXqZJOB4TVjoomIy
+aws:
+  accessKeyId: AKIAQ3EGT6WUVFM2K2N7
+  secretKey: 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
+  region: us-west-2
+  host: https://nsyqc4gjshaehd4b6brkq3jij40iucuc.lambda-url.us-west-2.on.aws
+  service : lambda

+ 7 - 1
jeecg-boot/jeecg-module-system/jeecg-system-start/src/main/resources/application-prod.yml

@@ -314,4 +314,10 @@ justauth:
 OKKI:
   BASE_URL: https://cms.xiaoman.cn/shop-api/External/site
   CLIENT_SECRET : G8v7vplPP9cm7vgUFXqZJOB4TVjoomIy
-  BASE_URL_V1 : https://cms.xiaoman.cn/shop-api/External/sitev1
+  BASE_URL_V1 : https://cms.xiaoman.cn/shop-api/External/sitev1
+aws:
+  accessKeyId: AKIAQ3EGT6WUVFM2K2N7
+  secretKey: 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
+  region: us-west-2
+  host: https://nsyqc4gjshaehd4b6brkq3jij40iucuc.lambda-url.us-west-2.on.aws
+  service : lambda

+ 5 - 0
jeecg-boot/pom.xml

@@ -143,6 +143,11 @@
 			<artifactId>pegdown</artifactId>
 			<version>${pegdown.version}</version>
 		</dependency>
+		<dependency>
+			<groupId>com.amazonaws</groupId>
+			<artifactId>aws-java-sdk-core</artifactId>
+			<version>1.12.638</version>
+		</dependency>
 	</dependencies>
 
 	<dependencyManagement>