parent
1ae208b8e1
commit
3e9ee267a4
@ -0,0 +1,29 @@
|
||||
package com.xjs.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* roll平台密钥
|
||||
* @author xiejs
|
||||
* @since 2022-01-07
|
||||
*/
|
||||
@Component
|
||||
@ConfigurationProperties("roll.open")
|
||||
@Data
|
||||
public class RollProperties {
|
||||
|
||||
/**
|
||||
* 应用id
|
||||
*/
|
||||
private String app_id;
|
||||
|
||||
|
||||
/**
|
||||
* 应用密钥
|
||||
*/
|
||||
private String app_secret;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,27 @@
|
||||
package com.xjs.common.client;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xjs.common.aop.ApiLog;
|
||||
import com.xjs.common.client.factory.RollTranslationFeignFactory;
|
||||
import com.xjs.translation.domain.qo.translation.RollTranslationQo;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.cloud.openfeign.SpringQueryMap;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import static com.xjs.consts.ApiConst.ROLL_FY;
|
||||
import static com.xjs.consts.ApiConst.ROLL_FY_URL;
|
||||
|
||||
/**
|
||||
* roll翻译接口api远程调用feign
|
||||
* @author xiejs
|
||||
* @since 2022-01-07
|
||||
*/
|
||||
@FeignClient(name = "rollTranslation", url = ROLL_FY_URL, fallbackFactory = RollTranslationFeignFactory.class)
|
||||
public interface RollTranslationFeignClient {
|
||||
|
||||
@GetMapping
|
||||
@ApiLog(name = ROLL_FY,
|
||||
url = ROLL_FY_URL,
|
||||
method = "Get")
|
||||
JSONObject translationApi(@SpringQueryMap RollTranslationQo qo);
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.xjs.common.client.factory;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.xjs.common.client.RollTranslationFeignClient;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* roll翻译降级
|
||||
* @author xiejs
|
||||
* @since 2022-01-07
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class RollTranslationFeignFactory implements FallbackFactory<RollTranslationFeignClient> {
|
||||
@Override
|
||||
public RollTranslationFeignClient create(Throwable cause) {
|
||||
log.error("api模块roll翻译服务调用失败:{},执行降级处理", cause.getMessage());
|
||||
return qo -> {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("error", R.FAIL);
|
||||
return jsonObject;
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.xjs.translation.domain.qo.translation;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author xiejs
|
||||
* @since 2022-01-07
|
||||
*/
|
||||
@Data
|
||||
public class RollTranslationQo {
|
||||
/**
|
||||
* 翻译内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 翻译源语言
|
||||
*/
|
||||
private String from = "auto";
|
||||
|
||||
/**
|
||||
* 翻译目标语言
|
||||
*/
|
||||
private String to ="auto";
|
||||
|
||||
/**
|
||||
* 应用id
|
||||
*/
|
||||
private String app_id;
|
||||
|
||||
|
||||
/**
|
||||
* 应用密钥
|
||||
*/
|
||||
private String app_secret;
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package com.xjs.translation.factory.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.xjs.common.client.RollTranslationFeignClient;
|
||||
import com.xjs.config.RollProperties;
|
||||
import com.xjs.exception.ApiException;
|
||||
import com.xjs.translation.domain.qo.translation.RollTranslationQo;
|
||||
import com.xjs.translation.domain.qo.translation.TranslationQo;
|
||||
import com.xjs.translation.domain.vo.translation.TranslationVo;
|
||||
import com.xjs.translation.factory.TranslationFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* roll翻译平台工厂实现
|
||||
*
|
||||
* @author xiejs
|
||||
* @since 2022-01-07
|
||||
*/
|
||||
@Component
|
||||
public class RollTranslationFactory implements TranslationFactory {
|
||||
@Autowired
|
||||
private RollProperties rollProperties;
|
||||
@Autowired
|
||||
private RollTranslationFeignClient rollTranslationFeignClient;
|
||||
|
||||
@Override
|
||||
public TranslationVo translationApi(TranslationQo translationQo) {
|
||||
RollTranslationQo rollTranslationQo = new RollTranslationQo();
|
||||
rollTranslationQo.setApp_id(rollProperties.getApp_id());
|
||||
rollTranslationQo.setApp_secret(rollProperties.getApp_secret());
|
||||
rollTranslationQo.setContent(translationQo.getQ());
|
||||
JSONObject translationApi = rollTranslationFeignClient.translationApi(rollTranslationQo);
|
||||
if (translationApi.containsKey("error")) {
|
||||
throw new ApiException("ROLL翻译接口调用异常");
|
||||
}
|
||||
if (translationApi.getInteger("code") == 1) {
|
||||
JSONObject data = translationApi.getJSONObject("data");
|
||||
TranslationVo translationVo = new TranslationVo();
|
||||
translationVo.setFrom(data.getString("originLanguage"));
|
||||
translationVo.setTo(data.getString("resultLanguage"));
|
||||
ArrayList<Map<String, String>> transResult = new ArrayList<>();
|
||||
Map<String, String> hashMap = new HashMap<>();
|
||||
hashMap.put("src", data.getString("origin"));
|
||||
hashMap.put("dst", data.getString("result"));
|
||||
transResult.add(hashMap);
|
||||
translationVo.setTransResult(transResult);
|
||||
return translationVo;
|
||||
}else
|
||||
return new TranslationVo();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue