[yxf] 4 weeks ago
parent
commit
7be19c1cc2
  1. 18
      im-platform/src/main/java/com/bx/implatform/controller/LoginController.java
  2. 71
      im-web/src/components/chat/ChatBox.vue
  3. 91
      im-web/src/components/chat/ChatInput.vue

18
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");
}

71
im-web/src/components/chat/ChatBox.vue

@ -108,6 +108,23 @@
@submit="sendMessage"
/>
<div class="send-btn-area">
<el-select
v-model="countryCode"
style="width: 110px; margin-right: 10px"
size="small"
>
<el-option
v-for="item in countryCodeList"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
<el-button
type="primary"
@click="translateInput(countryCode)"
>翻译</el-button
>
<el-button
type="primary"
icon="el-icon-s-promotion"
@ -321,6 +338,18 @@ export default {
newMessageSize: 0,
ipLocation: "",
maxTmpId: 0,
countryCode: "en",
countryCodeList: [
{ label: "英语", value: "en" },
{ label: "日语", value: "jp" },
{ label: "韩语", value: "kor" },
{ label: "越南语", value: "vie" },
{ label: "俄语", value: "ru" },
{ label: "德语", value: "de" },
{ label: "法语", value: "fra" },
{ label: "葡萄牙语", value: "pt" },
{ label: "阿拉伯语", value: "ara" },
],
};
},
methods: {
@ -1285,6 +1314,48 @@ export default {
});
return res;
},
//
async translateTextToOther(text, country) {
const res = await this.$http({
url: "/getTranslate",
method: "post",
data: { str: text, country: country },
});
return res;
},
//
async translateInput(country) {
try {
//
const chatInput = this.$refs.chatInputEditor;
if (!chatInput.isPureText()) {
this.$message.error("只能翻译纯文本内容");
return;
}
//
const text = chatInput.getTextContent();
if (!text || text.trim() === "") {
this.$message.warning("请输入需要翻译的文本内容");
return;
}
// API
const res = await this.translateTextToOther(text, country);
//
if (res) {
//
chatInput.setTextContent(res);
this.$message.success("翻译成功");
} else {
this.$message.error(res);
}
} catch (error) {
console.error("翻译出错:", error);
this.$message.error("翻译出错,请稍后重试");
}
},
},
computed: {
mine() {

91
im-web/src/components/chat/ChatInput.vue

@ -450,6 +450,97 @@ export default {
},
focus() {
this.$refs.content.focus();
},
//
isPureText() {
let nodes = this.$refs.content.childNodes;
let hasNonTextContent = false;
let each = (nodes) => {
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);
}
}

Loading…
Cancel
Save