diff --git a/im-platform/src/main/java/com/bx/implatform/controller/LoginController.java b/im-platform/src/main/java/com/bx/implatform/controller/LoginController.java index fba3da3..ef45259 100644 --- a/im-platform/src/main/java/com/bx/implatform/controller/LoginController.java +++ b/im-platform/src/main/java/com/bx/implatform/controller/LoginController.java @@ -75,13 +75,25 @@ public class LoginController { } @PostMapping("/getTranslate") - @Operation(summary = "获取配置", description = "用户注册") + @Operation(summary = "翻译文本", description = "翻译文本") public Result getTranslate(@RequestBody JSONObject jsonObject) { - System.out.println("1111111111"); String str = jsonObject.getStr("str"); - String trans = BaiduTranslationUtils.translate(str,"zh"); + if(str == null || str.isEmpty()){ + return ResultUtils.success("","success"); + } + + //如果有换行符,替换为空 + str = str.replace("\n", ""); + //翻译目标国家 + String country = jsonObject.getStr("country"); + String trans = ""; + if(country == null || country.isEmpty()) { + trans = BaiduTranslationUtils.translate(str, "zh"); + }else{ + trans = BaiduTranslationUtils.translate(str, country); + } return ResultUtils.success(trans,"success"); } diff --git a/im-web/src/components/chat/ChatBox.vue b/im-web/src/components/chat/ChatBox.vue index f927c31..87d96e2 100644 --- a/im-web/src/components/chat/ChatBox.vue +++ b/im-web/src/components/chat/ChatBox.vue @@ -108,6 +108,23 @@ @submit="sendMessage" />
+ + + + 翻译 { + for (let i = 0; i < nodes.length; i++) { + let node = nodes[i]; + if (!node) { + continue; + } + // 文本节点 + if (node.nodeType === 3) { + continue; + } + let nodeName = node.nodeName.toLowerCase(); + // 忽略script标签 + if (nodeName === 'script') { + continue; + } + // 图片节点(包括emoji) + if (nodeName === 'img') { + hasNonTextContent = true; + return; + } + // 文件节点 + if (nodeName === 'div' && node.dataset.fileId) { + hasNonTextContent = true; + return; + } + // @用户节点 + if (nodeName === 'span' && node.dataset.id) { + hasNonTextContent = true; + return; + } + // 递归检查子节点 + if (node.childNodes && node.childNodes.length > 0) { + each(node.childNodes); + if (hasNonTextContent) { + return; + } + } + } + }; + each(nodes); + return !hasNonTextContent; + }, + // 获取纯文本内容 + getTextContent() { + let nodes = this.$refs.content.childNodes; + let textContent = ''; + let each = (nodes) => { + for (let i = 0; i < nodes.length; i++) { + let node = nodes[i]; + if (!node) { + continue; + } + // 文本节点 + if (node.nodeType === 3) { + textContent += node.textContent; + continue; + } + let nodeName = node.nodeName.toLowerCase(); + // 忽略script标签 + if (nodeName === 'script') { + continue; + } + // 忽略div节点(换行) + if (nodeName === 'div' && !node.dataset.fileId) { + textContent += '\n'; + each(node.childNodes); + continue; + } + // 递归检查子节点 + if (node.childNodes && node.childNodes.length > 0) { + each(node.childNodes); + } + } + }; + each(nodes); + return textContent.trim(); + }, + // 设置纯文本内容 + setTextContent(text) { + // 清空内容 + this.$refs.content.innerHTML = ""; + // 添加文本节点到content中 + let textNode = document.createTextNode(text); + this.$refs.content.appendChild(textNode); + // 将光标定位到文本末尾 + this.selectElement(textNode, text.length); } }