|
|
@@ -0,0 +1,157 @@
|
|
|
+package com.njuzr.eaibackend.service;
|
|
|
+
|
|
|
+import com.njuzr.eaibackend.dto.TranslationDTO;
|
|
|
+import com.njuzr.eaibackend.exception.MyException;
|
|
|
+import com.njuzr.eaibackend.mapper.TranslationMapper;
|
|
|
+import com.njuzr.eaibackend.po.Translation;
|
|
|
+import com.njuzr.eaibackend.utils.WebClientUtil;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.Data;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author: Leonezhurui
|
|
|
+ * @Date: 2024/3/21 - 01:15
|
|
|
+ * @Package: EAI-Backend
|
|
|
+ */
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class TranslateService {
|
|
|
+
|
|
|
+ @Value("${youdao.appKey}")
|
|
|
+ private String appKey;
|
|
|
+
|
|
|
+ @Value("${youdao.appSecret}")
|
|
|
+ private String appSecret;
|
|
|
+ private final WebClientUtil webClientUtil = new WebClientUtil("http://localhost:5000/api/translate");
|
|
|
+
|
|
|
+ private final TranslationMapper translationMapper;
|
|
|
+
|
|
|
+ public TranslateService(TranslationMapper translationMapper) {
|
|
|
+ this.translationMapper = translationMapper;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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("", translationDTO, MyResponseObject.class);
|
|
|
+
|
|
|
+ if (Objects.equals(response.getCode(), 200)) {
|
|
|
+ Translation translation = new Translation();
|
|
|
+ translation.setUserId(userId);
|
|
|
+ translation.setAssignmentId(assignmentId);
|
|
|
+ translation.setQueryText(translationDTO.getQ());
|
|
|
+ translation.setQueryResult(response.data);
|
|
|
+ translation.setTargetLanguage(translationDTO.getTo());
|
|
|
+ translation.setQueryTime(new Date());
|
|
|
+
|
|
|
+ translationMapper.insert(translation);
|
|
|
+ } else {
|
|
|
+ log.error("请求字典出错");
|
|
|
+ throw MyException.create(HttpStatus.INTERNAL_SERVER_ERROR, "请求字典出错");
|
|
|
+ }
|
|
|
+
|
|
|
+ return response.data;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Data
|
|
|
+ @AllArgsConstructor
|
|
|
+ public static class MyRequestObject {
|
|
|
+ private String q;
|
|
|
+ private String from;
|
|
|
+ private String to;
|
|
|
+ private String appKey;
|
|
|
+ private String salt;
|
|
|
+ private String sign;
|
|
|
+ private String signType;
|
|
|
+ private String curtime;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Data
|
|
|
+ public static class MyResponseObject {
|
|
|
+ private int code;
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+}
|