说明:1、翻译模块集成roll翻译api
2、roll实现翻译工厂,rpc随机调用
3、修改api日志返回和请求json格式化
4、前端细节修改
This commit is contained in:
parent
1ae208b8e1
commit
3e9ee267a4
|
|
@ -51,6 +51,7 @@
|
||||||
<el-table-column label="预警记录信息" align="center" prop="warningMessage" :show-overflow-tooltip="true"/>
|
<el-table-column label="预警记录信息" align="center" prop="warningMessage" :show-overflow-tooltip="true"/>
|
||||||
<el-table-column label="限定值" align="center" prop="limitValue" width="100px"/>
|
<el-table-column label="限定值" align="center" prop="limitValue" width="100px"/>
|
||||||
<el-table-column label="实际值" align="center" prop="realValue" width="100px"/>
|
<el-table-column label="实际值" align="center" prop="realValue" width="100px"/>
|
||||||
|
<el-table-column label="预警时间" align="center" prop="createTime" width="180"/>
|
||||||
<el-table-column label="是否处理" align="center" prop="handle" width="100px">
|
<el-table-column label="是否处理" align="center" prop="handle" width="100px">
|
||||||
<template slot-scope="scope">
|
<template slot-scope="scope">
|
||||||
<el-tag :type="scope.row.handle==='1'?'success':'danger'" size="small">
|
<el-tag :type="scope.row.handle==='1'?'success':'danger'" size="small">
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@
|
||||||
</el-form>
|
</el-form>
|
||||||
<!-- 底部 -->
|
<!-- 底部 -->
|
||||||
<div class="el-login-footer">
|
<div class="el-login-footer">
|
||||||
<span>Copyright © 2018-2021 ruoyi.vip All Rights Reserved.</span>
|
<span>Copyright © 2018-2022 xiejs All Rights Reserved.</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@
|
||||||
</el-form>
|
</el-form>
|
||||||
<!-- 底部 -->
|
<!-- 底部 -->
|
||||||
<div class="el-register-footer">
|
<div class="el-register-footer">
|
||||||
<span>Copyright © 2018-2021 ruoyi.vip All Rights Reserved.</span>
|
<span>Copyright © 2018-2022 xiejs All Rights Reserved.</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -13,6 +13,8 @@ public class ApiConst {
|
||||||
|
|
||||||
public static final String BAIDU_FY= "百度翻译";
|
public static final String BAIDU_FY= "百度翻译";
|
||||||
|
|
||||||
|
public static final String ROLL_FY= "ROLL翻译";
|
||||||
|
|
||||||
public static final String TIANXING_PYQ= "天行-朋友圈";
|
public static final String TIANXING_PYQ= "天行-朋友圈";
|
||||||
|
|
||||||
public static final String TIANXING_WYY= "天行-网易云";
|
public static final String TIANXING_WYY= "天行-网易云";
|
||||||
|
|
@ -36,6 +38,8 @@ public class ApiConst {
|
||||||
|
|
||||||
public static final String YOUDAO_FY_URL= "http://fanyi.youdao.com/translate";
|
public static final String YOUDAO_FY_URL= "http://fanyi.youdao.com/translate";
|
||||||
|
|
||||||
|
public static final String ROLL_FY_URL= "https://www.mxnzp.com/api/convert/translate";
|
||||||
|
|
||||||
public static final String TIANXING_PYQ_URL= "http://api.tianapi.com/pyqwenan/index";
|
public static final String TIANXING_PYQ_URL= "http://api.tianapi.com/pyqwenan/index";
|
||||||
|
|
||||||
public static final String TIANXING_WYY_URL= "http://api.tianapi.com/hotreview/index";
|
public static final String TIANXING_WYY_URL= "http://api.tianapi.com/hotreview/index";
|
||||||
|
|
|
||||||
|
|
@ -8,5 +8,5 @@ package com.xjs.consts;
|
||||||
public interface TranslationTypeConst {
|
public interface TranslationTypeConst {
|
||||||
Integer BAIDU = 1;
|
Integer BAIDU = 1;
|
||||||
Integer YOUDAO = 2;
|
Integer YOUDAO = 2;
|
||||||
Integer GOOGLE = 3;
|
Integer ROLL = 3;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package com.xjs.common.aop;
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.date.DateUnit;
|
import cn.hutool.core.date.DateUnit;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.ruoyi.common.core.domain.R;
|
import com.ruoyi.common.core.domain.R;
|
||||||
import com.xjs.business.warning.RemoteWarningCRUDFeign;
|
import com.xjs.business.warning.RemoteWarningCRUDFeign;
|
||||||
import com.xjs.business.warning.domain.ApiRecord;
|
import com.xjs.business.warning.domain.ApiRecord;
|
||||||
|
|
@ -26,7 +27,7 @@ import java.time.LocalDateTime;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Objects;
|
||||||
|
|
||||||
import static com.xjs.consts.ApiWarnHandleConst.NO;
|
import static com.xjs.consts.ApiWarnHandleConst.NO;
|
||||||
|
|
||||||
|
|
@ -105,11 +106,12 @@ public class ApiLogAspect {
|
||||||
Object[] args = joinPoint.getArgs();//请求体
|
Object[] args = joinPoint.getArgs();//请求体
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
for (Object arg : args) {
|
for (Object arg : args) {
|
||||||
builder.append(arg);
|
String json = JSON.toJSONString(arg);
|
||||||
|
builder.append(json);
|
||||||
}
|
}
|
||||||
entity.setMethod(apiLog.method());
|
entity.setMethod(apiLog.method());
|
||||||
entity.setRequest(builder.toString());
|
entity.setRequest(builder.toString());
|
||||||
entity.setResponse(Optional.ofNullable(jsonResult).toString());
|
entity.setResponse(jsonResult.toString());
|
||||||
if (e != null) {
|
if (e != null) {
|
||||||
entity.setIsSuccess(StatusEnum.ERROR);
|
entity.setIsSuccess(StatusEnum.ERROR);
|
||||||
}else {
|
}else {
|
||||||
|
|
@ -170,29 +172,30 @@ public class ApiLogAspect {
|
||||||
haveApiRecord.setUpdateTime(null);
|
haveApiRecord.setUpdateTime(null);
|
||||||
remoteWarningCRUDFeign.updateApiRecordForRPC(haveApiRecord);
|
remoteWarningCRUDFeign.updateApiRecordForRPC(haveApiRecord);
|
||||||
//判断接口请求是否超过阈值
|
//判断接口请求是否超过阈值
|
||||||
if (haveApiRecord.getDayCount() > haveApiRecord.getLimitCount()) {
|
if(Objects.nonNull(haveApiRecord.getLimitCount())){
|
||||||
//把记录添加到预警表中
|
if (haveApiRecord.getDayCount() > haveApiRecord.getLimitCount()) {
|
||||||
ApiWarning apiWarning = new ApiWarning();
|
//把记录添加到预警表中
|
||||||
apiWarning.setLimitValue(String.valueOf(haveApiRecord.getLimitCount()));
|
ApiWarning apiWarning = new ApiWarning();
|
||||||
apiWarning.setRealValue(String.valueOf(haveApiRecord.getDayCount()));
|
apiWarning.setLimitValue(String.valueOf(haveApiRecord.getLimitCount()));
|
||||||
apiWarning.setApiName(haveApiRecord.getApiName());
|
apiWarning.setRealValue(String.valueOf(haveApiRecord.getDayCount()));
|
||||||
apiWarning.setHandle(NO);
|
apiWarning.setApiName(haveApiRecord.getApiName());
|
||||||
apiWarning.setWarningLevel(WarnLevelEnum.NOEMAL.getMessage());
|
apiWarning.setHandle(NO);
|
||||||
if(haveApiRecord.getDayCount()>haveApiRecord.getLimitCount()*2 &&
|
apiWarning.setWarningLevel(WarnLevelEnum.NOEMAL.getMessage());
|
||||||
haveApiRecord.getDayCount() < haveApiRecord.getLimitCount() * 3){
|
if(haveApiRecord.getDayCount()>haveApiRecord.getLimitCount()*2 &&
|
||||||
apiWarning.setWarningLevel(WarnLevelEnum.WARNING.getMessage());
|
haveApiRecord.getDayCount() < haveApiRecord.getLimitCount() * 3){
|
||||||
} else if (haveApiRecord.getDayCount() > haveApiRecord.getLimitCount() * 3) {
|
apiWarning.setWarningLevel(WarnLevelEnum.WARNING.getMessage());
|
||||||
apiWarning.setWarningLevel(WarnLevelEnum.DANGER.getMessage());
|
} else if (haveApiRecord.getDayCount() > haveApiRecord.getLimitCount() * 3) {
|
||||||
|
apiWarning.setWarningLevel(WarnLevelEnum.DANGER.getMessage());
|
||||||
|
}
|
||||||
|
apiWarning.setWarningType(WarnTypeEnum.API.getType());
|
||||||
|
String message = String.format(WarnTypeEnum.API.getMessage(),
|
||||||
|
haveApiRecord.getLimitCount(), haveApiRecord.getDayCount());
|
||||||
|
apiWarning.setWarningMessage(message);
|
||||||
|
remoteWarningCRUDFeign.saveApiWarningForRPC(apiWarning);
|
||||||
|
|
||||||
|
// todo websocket实现即时推送到客户端
|
||||||
}
|
}
|
||||||
apiWarning.setWarningType(WarnTypeEnum.API.getType());
|
|
||||||
String message = String.format(WarnTypeEnum.API.getMessage(),
|
|
||||||
haveApiRecord.getLimitCount(), haveApiRecord.getDayCount());
|
|
||||||
apiWarning.setWarningMessage(message);
|
|
||||||
remoteWarningCRUDFeign.saveApiWarningForRPC(apiWarning);
|
|
||||||
|
|
||||||
// todo websocket实现即时推送到客户端
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -18,8 +18,7 @@ import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import static com.xjs.consts.TranslationTypeConst.BAIDU;
|
import static com.xjs.consts.TranslationTypeConst.*;
|
||||||
import static com.xjs.consts.TranslationTypeConst.YOUDAO;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiejs
|
* @author xiejs
|
||||||
|
|
@ -36,6 +35,8 @@ public class TranslationController {
|
||||||
private TranslationFactory youDaoTranslationFactory;
|
private TranslationFactory youDaoTranslationFactory;
|
||||||
@Autowired
|
@Autowired
|
||||||
private TranslationFactory baiDuTranslationFactory;
|
private TranslationFactory baiDuTranslationFactory;
|
||||||
|
@Autowired
|
||||||
|
private TranslationFactory rollTranslationFactory;
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
@ApiOperation("翻译接口")
|
@ApiOperation("翻译接口")
|
||||||
|
|
@ -50,6 +51,9 @@ public class TranslationController {
|
||||||
if (YOUDAO.equals(translationQo.getTranslationType())) {
|
if (YOUDAO.equals(translationQo.getTranslationType())) {
|
||||||
translationVo = youDaoTranslationFactory.translationApi(translationQo);
|
translationVo = youDaoTranslationFactory.translationApi(translationQo);
|
||||||
}
|
}
|
||||||
|
if (ROLL.equals(translationQo.getTranslationType())) {
|
||||||
|
translationVo = rollTranslationFactory.translationApi(translationQo);
|
||||||
|
}
|
||||||
return AjaxResult.success(translationVo);
|
return AjaxResult.success(translationVo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -75,13 +79,14 @@ public class TranslationController {
|
||||||
/**
|
/**
|
||||||
* 封装随机调用api
|
* 封装随机调用api
|
||||||
*
|
*
|
||||||
* @return 文案工厂
|
* @return 翻译工厂
|
||||||
*/
|
*/
|
||||||
private TranslationFactory randomApi() {
|
private TranslationFactory randomApi() {
|
||||||
ArrayList<TranslationFactory> factories = new ArrayList<>();
|
ArrayList<TranslationFactory> factories = new ArrayList<>();
|
||||||
//添加了新接口只需要在这add接口进去
|
//添加了新接口只需要在这add接口进去
|
||||||
factories.add(youDaoTranslationFactory);
|
factories.add(youDaoTranslationFactory);
|
||||||
factories.add(baiDuTranslationFactory);
|
factories.add(baiDuTranslationFactory);
|
||||||
|
factories.add(rollTranslationFactory);
|
||||||
//--------add----------------------------;-
|
//--------add----------------------------;-
|
||||||
//随机调用集合中的接口
|
//随机调用集合中的接口
|
||||||
return RandomUtil.randomEle(factories);
|
return RandomUtil.randomEle(factories);
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -86,6 +86,7 @@ public class ApiWarningController extends BaseController {
|
||||||
public TableDataInfo list(ApiWarning apiWarning) {
|
public TableDataInfo list(ApiWarning apiWarning) {
|
||||||
startPage();
|
startPage();
|
||||||
List<ApiWarning> list = apiWarningService.list(new QueryWrapper<ApiWarning>()
|
List<ApiWarning> list = apiWarningService.list(new QueryWrapper<ApiWarning>()
|
||||||
|
.orderByDesc("create_time")
|
||||||
.like(Objects.nonNull(apiWarning.getApiName()),"api_name", apiWarning.getApiName()));
|
.like(Objects.nonNull(apiWarning.getApiName()),"api_name", apiWarning.getApiName()));
|
||||||
return getDataTable(list);
|
return getDataTable(list);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue