export default {
name: "ChatBot",
components: {
Avatar,
},
data() {
return {
messages: [
{
id: 1,
content: this.$t("detail.ChatHello"),
isRequest: false,
timestamp: "",
curDone: false,
requestFinishedFlag: false,
zan: false,
cai: false,
caiTotal: 0,
responseText: "",
zanMouseover: false,
caiMouseover: false,
copyMouseover: false,
},
],
inputMessage: "",
sendingMessage: false,
responseAvatar: `../../assets/images/lenovo-icon.png`,
retryWidth: "revert-layer",
clearWidth: "revert-layer",
BMContent: "",
};
},
computed: {
...mapGetters(["name", "language"]),
RetryStyle() {
return { width: this.retryWidth };
},
ClearStyle() {
return { width: this.clearWidth };
},
},
methods: {
startStreaming(params) {
let flag = true;
// 创建 XMLHttpRequest 对象
const xhr = new XMLHttpRequest();
// 设置请求的 URL
xhr.open("POST", axios.defaults.baseURL + `/consultant/consult`, true);
xhr.setRequestHeader("token", getToken());
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.setRequestHeader("Content-Language", store.getters.language);
// 监听 readyStateChange 事件
xhr.onreadystatechange = () => {
// 如果 readyState 是 3,表示正在接收数据
if (xhr.readyState === 3) {
// 将数据添加到文本框中
this.sendingMessage = false;
if (flag) {
flag = false;
this.pushMessage(xhr.responseText, false);
} else {
let json = this.replaceURLsWithLinks(xhr.responseText);
this.messages[this.messages.length - 1].content = json.content;
if (!this.messages[this.messages.length - 1].curDone) {
this.messages[this.messages.length - 1].curDone = json.curDone;
}
}
}
// 如果 readyState 是 4,表示完成请求
if (xhr.readyState === 4) {
if (xhr.status === 200) {
// 避免接受过程中缺少字符
this.sendingMessage = false;
if (flag) {
flag = false;
this.pushMessage(xhr.responseText, false);
} else {
let json = this.replaceURLsWithLinks(xhr.responseText);
this.messages[this.messages.length - 1].content = json.content;
if (!this.messages[this.messages.length - 1].curDone) {
this.messages[this.messages.length - 1].curDone = json.curDone;
}
this.messages[
this.messages.length - 1
].requestFinishedFlag = true; // 用于显示红字提示
this.messages[this.messages.length - 1].responseText =
xhr.responseText; //用于复制
}
}
}
};
// 发送请求
let sendData = params.map((item) => {
let obj = {};
obj.role = item.role;
obj.message = item.message.split("<br />")[0];
obj.index = item.index;
return obj;
});
let data = { legalQuestions: sendData };
xhr.send(JSON.stringify(data));
},
async sendMessage() {
if (this.inputMessage.trim() !== "" && !this.sendingMessage) {
this.pushMessage(this.inputMessage, true);
this.inputMessage = "";
this.sendingMessage = true; // Show loading animation
this.scrollToEnd(); //滚动到聊天窗口底部
let params = [...this.messages];
if (!params[0].isRequest) {
params.shift();
}
params = params.map((obj, i) => ({
role: obj.isRequest ? 1 : 0,
message: obj.content,
index: i + 1,
}));
this.startStreaming(params);
}
},
async ReSendMessage() {
if (!this.sendingMessage && this.messages.length > 0) {
if (this.messages.length == 1 && !this.messages[0].isRequest) {
return;
}
this.sendingMessage = true; // Show loading animation
this.scrollToEnd();
this.messages.pop();
let params = [...this.messages];
if (!params[0].isRequest) {
params.shift();
}
params = params.map((obj, i) => ({
role: obj.isRequest ? 1 : 0,
message: obj.content,
index: i + 1,
}));
this.startStreaming(params);
}
},
pushMessage(content, isRequest) {
const timestamp = new Date().toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
});
this.messages.push({
id: this.messages.length + 1,
content,
isRequest,
timestamp: timestamp,
curDone: false,
requestFinishedFlag: false,
zan: false,
cai: false,
caiTotal: 0,
responseText: content,
zanMouseover: false,
caiMouseover: false,
copyMouseover: false,
});
},
ClearContext() {
if (!this.sendingMessage) this.messages = [];
},
changeWidth(type) {
this.language == "en";
if (this.language == "en") {
type == "Retry"
? (this.retryWidth = "75px")
: (this.clearWidth = "125px");
} else {
type == "Retry"
? (this.retryWidth = "97px")
: (this.clearWidth = "97px");
}
},
resetWidth(type) {
type == "Retry"
? (this.retryWidth = "revert-layer")
: (this.clearWidth = "revert-layer");
},
replaceURLsWithLinks(html) {
if (html.indexOf("{&&}") < 0) {
return {
content: html.replace(/\n/g, "<br />"),
};
} else {
let arr = html.split("{%%}");
let newHtml = "";
if (arr[1] === "1") {
let linkArr = arr[2].replace(/\n/g, "<br />").split("<br />");
let linkhtml = "";
for (var i = 1; i < linkArr.length; i++) {
let itemLinkArr = linkArr[i].split("{&&}");
linkhtml +=
'<a href="' +
itemLinkArr[1] +
'" target="_blank">' +
itemLinkArr[0] +
"</a><br />";
}
newHtml = arr[0] + "<br />" + linkhtml;
} else if (arr[1] === "0") {
//不显示知识来源 ok
newHtml = `${arr[0]}`;
}
let curDone = arr[1] === "1" ? true : false;
return {
content: newHtml.replace(/\n/g, "<br />"),
curDone,
};
}
},
scrollToEnd() {
this.$nextTick(() => {
this.$el.querySelector(".chat-scrollbar").scrollTo({
top: this.$el.querySelector(".chat-scrollbar").scrollHeight,
behavior: "smooth",
});
});
},
},
};
网页聊天工具-与AI对话
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一 需求分析 掌握Java语言的程序设计方法,理论结合实际操作巩固我们所学的现有知识,使用图形用户界面和socke...
- 应该不是最近被这鬼东西烦死到要命,而是说很多我们的时间都被这个即时通信工具打扰到不堪忍受。 究其原因,后面再聊,估...
- 虽然说现在是互联网时代。我们的工作生活。与人交往都离不开互联网。但是也不能过度的使用聊天工具。把所有的精力都放在聊...
- QQ_Chat 基于TCP,Socket编程,模仿腾讯QQ界面,使用Java开发的一款网络聊天工具。(内含报告) ...