Merge branch 'prod/20260407' into test
This commit is contained in:
commit
fc1de7c28f
|
|
@ -22,11 +22,11 @@ import com.nflg.mobilebroken.gongfu.pojo.vo.SolutionImageVO;
|
|||
import com.nflg.mobilebroken.gongfu.publisher.TicketEventPublisher;
|
||||
import com.nflg.mobilebroken.gongfu.service.ShengWangService;
|
||||
import com.nflg.mobilebroken.gongfu.service.SsePushService;
|
||||
import com.nflg.mobilebroken.gongfu.service.impl.AliYunTranslate;
|
||||
import com.nflg.mobilebroken.repository.entity.*;
|
||||
import com.nflg.mobilebroken.repository.service.*;
|
||||
import com.nflg.mobilebroken.starter.annotation.MethodInfoMark;
|
||||
import com.nflg.mobilebroken.starter.service.UniPushService;
|
||||
import com.nflg.mobilebroken.starter.service.impl.AliYunTranslate;
|
||||
import com.nflg.mobilebroken.starter.service.impl.DeepSeekTranslate;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
|
|
|
|||
|
|
@ -1,113 +0,0 @@
|
|||
package com.nflg.mobilebroken.gongfu.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
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.gongfu.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.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@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;
|
||||
|
||||
@Resource
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@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) {
|
||||
if (isOnlyDigitsAndSymbols(text)) {
|
||||
return text;
|
||||
}
|
||||
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));
|
||||
String key = "translate:" + SecureUtil.md5(text) + ":" + targetLanguage;
|
||||
String result = stringRedisTemplate.opsForValue().get(key);
|
||||
if (StrUtil.isNotBlank(result)) {
|
||||
log.info("翻译,响应,从缓存读取,翻译结果:{}", result);
|
||||
return result;
|
||||
}
|
||||
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()));
|
||||
result = response.body.getData().getTranslated();
|
||||
stringRedisTemplate.opsForValue().set(key, result, 30, TimeUnit.DAYS);
|
||||
return result;
|
||||
} 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());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String translateWord(String text, String targetLanguage, String formatType) {
|
||||
return translateWord(text, "auto", targetLanguage, formatType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String translateWord(String text, String targetLanguage) {
|
||||
return translateWord(text, targetLanguage, "text");
|
||||
}
|
||||
|
||||
private boolean isOnlyDigitsAndSymbols(String content) {
|
||||
if (StrUtil.isBlank(content)) {
|
||||
return true;
|
||||
}
|
||||
// 正则:只允许数字、符号(不含中英文字母)
|
||||
return content.matches("[0-9\\s\\p{Punct}]+");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue