|
|
@@ -36,29 +36,7 @@ public class TranslateService {
|
|
|
}
|
|
|
|
|
|
public String translate(Long assignmentId, Long userId, TranslationDTO translationDTO) {
|
|
|
- // 1 请求,构造请求条件
|
|
|
-// String salt = UUID.randomUUID().toString();
|
|
|
-// String curtime = String.valueOf(System.currentTimeMillis() / 1000);
|
|
|
-// String sign;
|
|
|
-// try {
|
|
|
-// sign = calculateSign(appKey, appSecret, translationDTO.getQ(), salt, curtime);
|
|
|
-// } catch (NoSuchAlgorithmException e) {
|
|
|
-// log.error(e.getMessage());
|
|
|
-// throw MyException.create(HttpStatus.INTERNAL_SERVER_ERROR, "不支持SHA-256算法");
|
|
|
-// }
|
|
|
-
|
|
|
-// MyRequestObject request = new MyRequestObject(
|
|
|
-// translationDTO.getQ(),
|
|
|
-// translationDTO.getFrom(),
|
|
|
-// translationDTO.getTo(),
|
|
|
-// appKey,
|
|
|
-// salt,
|
|
|
-// sign,
|
|
|
-// "v3",
|
|
|
-// curtime
|
|
|
-// );
|
|
|
-
|
|
|
- MyResponseObject response = webClientUtil.post("http://localhost:5000/api/translate", translationDTO, MyResponseObject.class);
|
|
|
+ MyResponseObject response = webClientUtil.post("http://47.111.23.171:5000/api/translate", translationDTO, MyResponseObject.class);
|
|
|
|
|
|
if (Objects.equals(response.getCode(), 200)) {
|
|
|
Translation translation = new Translation();
|
|
|
@@ -98,53 +76,4 @@ public class TranslateService {
|
|
|
private String data;
|
|
|
private String msg;
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 计算鉴权签名 -
|
|
|
- * 计算方式 : sign = sha256(appKey + input(q) + salt + curtime + appSecret)
|
|
|
- *
|
|
|
- * @param appKey 您的应用ID
|
|
|
- * @param appSecret 您的应用密钥
|
|
|
- * @param q 请求内容
|
|
|
- * @param salt 随机值
|
|
|
- * @param curtime 当前时间戳(秒)
|
|
|
- * @return 鉴权签名sign
|
|
|
- */
|
|
|
- public String calculateSign(String appKey, String appSecret, String q, String salt, String curtime)
|
|
|
- throws NoSuchAlgorithmException {
|
|
|
- String strSrc = appKey + getInput(q) + salt + curtime + appSecret;
|
|
|
- return encrypt(strSrc);
|
|
|
- }
|
|
|
-
|
|
|
- private String encrypt(String strSrc) throws NoSuchAlgorithmException {
|
|
|
- byte[] bt = strSrc.getBytes();
|
|
|
- MessageDigest md = MessageDigest.getInstance("SHA-256");
|
|
|
- md.update(bt);
|
|
|
- byte[] bts = md.digest();
|
|
|
- StringBuilder des = new StringBuilder();
|
|
|
- for (byte b : bts) {
|
|
|
- String tmp = (Integer.toHexString(b & 0xFF));
|
|
|
- if (tmp.length() == 1) {
|
|
|
- des.append("0");
|
|
|
- }
|
|
|
- des.append(tmp);
|
|
|
- }
|
|
|
- return des.toString();
|
|
|
- }
|
|
|
-
|
|
|
- private static String getInput(String input) {
|
|
|
- if (input == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- String result;
|
|
|
- int len = input.length();
|
|
|
- if (len <= 20) {
|
|
|
- result = input;
|
|
|
- } else {
|
|
|
- String startStr = input.substring(0, 10);
|
|
|
- String endStr = input.substring(len - 10, len);
|
|
|
- result = startStr + len + endStr;
|
|
|
- }
|
|
|
- return result;
|
|
|
- }
|
|
|
}
|