parent
022b3b88a3
commit
81c1bf3a3b
|
|
@ -125,6 +125,16 @@
|
||||||
<artifactId>poi-ooxml</artifactId>
|
<artifactId>poi-ooxml</artifactId>
|
||||||
<version>5.2.3</version>
|
<version>5.2.3</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun</groupId>
|
||||||
|
<artifactId>alimt20181012</artifactId>
|
||||||
|
<version>1.4.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun</groupId>
|
||||||
|
<artifactId>tea-openapi</artifactId>
|
||||||
|
<version>0.3.8</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,11 @@
|
||||||
package com.nflg.mobilebroken.admin.controller;
|
package com.nflg.mobilebroken.admin.controller;
|
||||||
|
|
||||||
import com.nflg.mobilebroken.admin.service.DeviceQRCodeService;
|
import com.nflg.mobilebroken.admin.service.DeviceQRCodeService;
|
||||||
|
import com.nflg.mobilebroken.admin.service.ITranslate;
|
||||||
import com.nflg.mobilebroken.common.pojo.ApiResult;
|
import com.nflg.mobilebroken.common.pojo.ApiResult;
|
||||||
import com.nflg.mobilebroken.common.util.VUtils;
|
import com.nflg.mobilebroken.common.util.VUtils;
|
||||||
import com.nflg.mobilebroken.repository.service.ITicketCallService;
|
import com.nflg.mobilebroken.repository.service.ITicketCallService;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
@ -26,6 +24,9 @@ public class TestController extends ControllerBase{
|
||||||
@Resource
|
@Resource
|
||||||
private ITicketCallService ticketCallService;
|
private ITicketCallService ticketCallService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ITranslate translate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 显示二维码
|
* 显示二维码
|
||||||
* @param deviceNo 设备编号
|
* @param deviceNo 设备编号
|
||||||
|
|
@ -52,4 +53,14 @@ public class TestController extends ControllerBase{
|
||||||
public ApiResult<Boolean> test(@RequestParam Integer userId){
|
public ApiResult<Boolean> test(@RequestParam Integer userId){
|
||||||
return ApiResult.success(ticketCallService.isInCall(userId));
|
return ApiResult.success(ticketCallService.isInCall(userId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 翻译为日语
|
||||||
|
* @param text 要翻译的文本
|
||||||
|
* @return 翻译结果
|
||||||
|
*/
|
||||||
|
@PostMapping("translate")
|
||||||
|
public ApiResult<String> translate(@RequestBody String text){
|
||||||
|
return ApiResult.success(translate.translateWord(text, "auto", "ja", "html"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
package com.nflg.mobilebroken.admin.controller;
|
||||||
|
|
||||||
|
import com.nflg.mobilebroken.admin.service.ITranslate;
|
||||||
|
import com.nflg.mobilebroken.common.pojo.ApiResult;
|
||||||
|
import com.nflg.mobilebroken.common.pojo.request.TranslateWordRequest;
|
||||||
|
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 javax.annotation.Resource;
|
||||||
|
import javax.validation.Valid;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 翻译相关
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/translate")
|
||||||
|
public class TranslateController extends ControllerBase{
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ITranslate translate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文字翻译
|
||||||
|
* @param request 请求参数
|
||||||
|
* @return 翻译结果
|
||||||
|
*/
|
||||||
|
@PostMapping("word")
|
||||||
|
public ApiResult<String> translateWord(@Valid @RequestBody TranslateWordRequest request){
|
||||||
|
return ApiResult.success(translate.translateWord(request.getText(), "auto", request.getTargetLanguage(), "html"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
package com.nflg.mobilebroken.admin.service;
|
||||||
|
|
||||||
|
public interface ITranslate {
|
||||||
|
|
||||||
|
String translateWord(String text,String sourceLanguage,String targetLanguage,String formatType);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,76 @@
|
||||||
|
package com.nflg.mobilebroken.admin.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.aliyun.alimt20181012.models.TranslateGeneralResponse;
|
||||||
|
import com.aliyun.credentials.Client;
|
||||||
|
import com.aliyun.credentials.models.Config;
|
||||||
|
import com.aliyun.tea.TeaException;
|
||||||
|
import com.nflg.mobilebroken.admin.service.ITranslate;
|
||||||
|
import com.nflg.mobilebroken.common.constant.STATE;
|
||||||
|
import com.nflg.mobilebroken.common.exception.NflgException;
|
||||||
|
import com.nflg.mobilebroken.common.util.VUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@ConditionalOnProperty(name = "translate.platform", havingValue = "aliyun")
|
||||||
|
@Slf4j
|
||||||
|
public class AliYunTranslate implements ITranslate {
|
||||||
|
|
||||||
|
@Value("${translate.aliyun.accessKeyId}")
|
||||||
|
private String accessKeyId;
|
||||||
|
|
||||||
|
@Value("${translate.aliyun.accessKeySecret}")
|
||||||
|
private String accessKeySecret;
|
||||||
|
|
||||||
|
@Value("${translate.aliyun.endpoint}")
|
||||||
|
private String endpoint;
|
||||||
|
|
||||||
|
private com.aliyun.alimt20181012.Client client;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() throws Exception {
|
||||||
|
Config credentialConfig = new Config();
|
||||||
|
credentialConfig.type="access_key";
|
||||||
|
credentialConfig.accessKeyId=accessKeyId;
|
||||||
|
credentialConfig.accessKeySecret=accessKeySecret;
|
||||||
|
Client credentialClient = new Client(credentialConfig);
|
||||||
|
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
||||||
|
.setCredential(credentialClient);
|
||||||
|
// Endpoint 请参考 https://api.aliyun.com/product/alimt
|
||||||
|
config.endpoint = endpoint;
|
||||||
|
client=new com.aliyun.alimt20181012.Client(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String translateWord(String text, String sourceLanguage, String targetLanguage, String formatType) {
|
||||||
|
com.aliyun.alimt20181012.models.TranslateGeneralRequest request = new com.aliyun.alimt20181012.models.TranslateGeneralRequest()
|
||||||
|
.setTargetLanguage(targetLanguage)
|
||||||
|
.setSourceLanguage(sourceLanguage)
|
||||||
|
.setFormatType(formatType)
|
||||||
|
.setSourceText(text);
|
||||||
|
log.info("翻译,请求参数:{}", JSONUtil.toJsonStr(request));
|
||||||
|
try {
|
||||||
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
|
TranslateGeneralResponse response = client.translateGeneralWithOptions(request, new com.aliyun.teautil.models.RuntimeOptions());
|
||||||
|
VUtils.trueThrowBusinessError(response.statusCode != 200).throwMessage("翻译失败:"+response.getBody().getMessage());
|
||||||
|
log.info("翻译,响应,识别到的原始语言:{},字数:{},翻译结果:{}"
|
||||||
|
, response.getBody().getData().getDetectedLanguage()
|
||||||
|
,response.getBody().getData().getWordCount()
|
||||||
|
,JSONUtil.toJsonStr(response.getBody().getData().getTranslated()));
|
||||||
|
return response.body.getData().getTranslated();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
TeaException error = new TeaException(ex.getMessage(), ex);
|
||||||
|
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||||
|
// 错误 message
|
||||||
|
log.error("翻译异常:{}", error.getMessage());
|
||||||
|
// 诊断地址
|
||||||
|
log.error("诊断地址:{}", error.getData().get("Recommend"));
|
||||||
|
throw new NflgException(STATE.BusinessError, "翻译失败:" + error.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package com.nflg.mobilebroken.common.pojo.request;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotBlank;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class TranslateWordRequest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 翻译目标语言
|
||||||
|
*/
|
||||||
|
@NotBlank
|
||||||
|
private String targetLanguage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 需要翻译的内容,支持html格式
|
||||||
|
*/
|
||||||
|
@NotBlank
|
||||||
|
private String text;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue