修改api地址和优化暂停输出的逻辑,

This commit is contained in:
2025-11-16 12:05:38 +08:00
parent b59064cdc9
commit 843a447680
88 changed files with 38248 additions and 181 deletions

View File

@@ -9,7 +9,7 @@
<meta http-equiv="Expires" content="Thu, 01 Jan 1970 00:00:00 GMT">
<title>新能源智能体</title>
<link rel="icon" href="./img/logo.png" type="image/x-icon">
<script src=" https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxopensdk.js">
<!-- <script src=" https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxopensdk.js"> -->
</script>
<!-- <script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script> -->
@@ -233,7 +233,7 @@
padding: 20px;
overflow-y: auto;
position: relative;
/* height: 400px */
height: 400px
}
.message {
@@ -933,6 +933,7 @@
const chatList = document.getElementById("chatList");
let controller = null;
let sessIdList = [];
let isStopped = false;//是否停止输出
let isUpload = {};
let hasImage = false;
let isPre = false;//是否点击了预设问题
@@ -963,7 +964,8 @@
imageBase64: '',
userId: userId,
sessionId: '',
userPrompt: ''
userPrompt: '',
requestId: ''
}
if (isMobile()) {
toggleSidebar();
@@ -1000,7 +1002,7 @@
// 获取到用户名称
function getUserName(userId) {
try {
fetch("http://1.95.137.212:30009/user", {
fetch("/api/user", {
method: "POST",
headers: {
"Content-Type": "application/json"
@@ -1100,7 +1102,7 @@
function createSession(userId, sessionId) {
try {
fetch("http://1.95.137.212:30009/session", {
fetch("/api/session", {
method: "POST",
headers: {
"Content-Type": "application/json"
@@ -1276,6 +1278,13 @@
function sendMessage(isPre) {
console.log("发送消息1122");
if (controller) {
showToast("请先停止对话");
return;
}
isStopped = false;
const sendBtnImg = document.querySelector('#sendBtn img');
const stopBtn = document.querySelector('#stopBtn');
@@ -1315,19 +1324,16 @@
}
chatInput.value = "";
if (controller) {
controller.abort();
controller = null;
}
if (!sendBtnImg.classList.contains('none')) {
sendBtnImg.classList.add('none');
stopBtn.classList.remove('none')
}
getAiResponse(imgUrl);
}
let reader;
const getAiResponse = async (img) => {
deleteImg();
const aiMsg = { role: "assistant", content: "", isImage: false };
const msgDiv = document.createElement("div");
@@ -1337,7 +1343,7 @@
const interval = setInterval(() => {
dots = (dots + 1) % 4; // 0,1,2,3
msgDiv.textContent = '思考中' + '.'.repeat(dots);
}, 500);
}, 200);
msgDiv.className = "message assistant thinking ";
chatMessages.appendChild(msgDiv);
@@ -1348,30 +1354,59 @@
sendParam.userId = decodeURIComponent(Number(escape(atob(encodedUserId))));
// ✅ 创建 AbortController
controller = new AbortController();
const res = await fetch("http://1.95.137.212:30009/analyze", {
sendParam.requestId = crypto.randomUUID();
const res = await fetch("/api/analyze", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(sendParam),
signal: controller.signal, // ✅ 加上 signal
}).catch(err => {
console.log("✅ 流被中断:", err.name);
clearInterval(interval);
const sendBtnImg = document.querySelector('#sendBtn img');
const stopBtn = document.querySelector('#stopBtn');
sendBtnImg.classList.remove('none');
stopBtn.classList.add('none');
return null;
});
if (!res) return; // 流已中断则直接退出
if (!res) {
const sendBtnImg = document.querySelector('#sendBtn img');
const stopBtn = document.querySelector('#stopBtn');
if (sendBtnImg.classList.contains("none")) {
sendBtnImg.classList.remove('none');
}
if (!stopBtn.classList.contains("none")) {
stopBtn.classList.add('none');
}
return null;
}
sendParam.imageBase64 = '';
const reader = res.body.getReader();
reader = res.body.getReader();
const decoder = new TextDecoder("utf-8");
let buffer = "";
msgDiv.classList.remove("thinking");
clearInterval(interval);
console.log(isStopped, '是否点击停止')
while (true) {
const { done, value } = await reader.read();
console.log(done, value, 'done')
console.log("回答")
let result;
try {
result = await reader.read();
} catch (e) {
console.log("流已中断:", e);
break; // ← 必须退出循环
}
const { done, value } = result;
if (done) {
controller = null;
const sendBtnImg = document.querySelector('#sendBtn img');
const stopBtn = document.querySelector('#stopBtn');
sendBtnImg.classList.add('none');
@@ -1388,9 +1423,9 @@
if (!line.startsWith("data:")) continue;
const data = line.slice(5).trim();
console.log("完成回答", data)
if (data == "[DONE]") {
console.log("完成回答")
console.log("完成回答");
controller = null;
// 更新聊天滚动条
const sendBtnImg = document.querySelector('#sendBtn img');
const stopBtn = document.querySelector('#stopBtn');
@@ -1412,7 +1447,8 @@
// }
if (obj.content && obj.type !== "title") {
for (const char of obj.content) {
await new Promise(r => setTimeout(r, 5)); // 控制字速
await new Promise(r => setTimeout(r, 1)); // 控制字速
if (isStopped) return;
aiMsg.content += char;
msgDiv.innerHTML = marked.parse(aiMsg.content);
@@ -1473,7 +1509,7 @@
}
const deleteSession = (sessionId, userId) => {
try {
fetch(`http://1.95.137.212:30009/session?sessionId=${sessionId}&userId=${userId}`, {
fetch(`/api/session?sessionId=${sessionId}&userId=${userId}`, {
method: "DELETE"
}).then(res => res.json()).then(data => {
if (data.code === 0) {
@@ -1562,7 +1598,7 @@
// 调用获取历史记录的接口
function getHistory() {
// sendParam.userId = encodedUserId;
fetch(`http://1.95.137.212:30009/history?sessionId=${sendParam.sessionId}`, {
fetch(`/api/history?sessionId=${sendParam.sessionId}`, {
// fetch(`http://1.95.137.212:30008/history?sessionId=${sendParam.sessionId}`, {
method: "GET",
// headers: { "Content-Type": "application/json" },
@@ -1611,7 +1647,7 @@
// window.location.href = url; // 跳转到微信授权页面
// }
simulateWechatLogin()
// simulateWechatLogin()
// 页面加载时立即执行:检查是否有微信返回的 code
document.addEventListener('DOMContentLoaded', function () {
const params = new URLSearchParams(window.location.search);
@@ -1639,9 +1675,10 @@
const APP_ID = 'wx0eeb7556d60121db';
const REDIRECT_URI = encodeURIComponent("https://www.satabot.com");
const STATE = Math.random().toString(36).substring(2, 12);
const SCOPE = 'snsapi_login';
const authUrl = `https://open.weixin.qq.com/connect/qrconnect?appid=${APP_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=${SCOPE}&state=${STATE}#wechat_redirect`;
window.location.href = authUrl; // 跳转后页面刷新,后续代码不执行
const SCOPE = 'snsapi_base';
// const authUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx807d86fb6b3d4fd2&redirect_uri=http%3A%2F%2Fdevelopers.weixin.qq.com&response_type=code&scope=snsapi_userinfo&state=STATE&forcePopup=true#wechat_redirect";
const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${APP_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=${SCOPE}&state=${STATE}&forcePopup=true#wechat_redirect`;
// window.location.href = authUrl; // 跳转后页面刷新,后续代码不执行
}
// if(from=="weixin"){
@@ -1660,7 +1697,7 @@
function getSessionList() {
sendParam.userId = userId;
fetch(`http://1.95.137.212:30009/sessions?userId=${sendParam.userId}`, {
fetch(`/api/sessions?userId=${sendParam.userId}`, {
// fetch(`http://1.95.137.212:30008/history?sessionId=${sendParam.sessionId}`, {
method: "GET",
// headers: { "Content-Type": "application/json" },
@@ -1670,7 +1707,7 @@
console.log(data, 8777)
if (data.code == 300) {
// window.location.href='http://localhost/#/login?goRemote=1 '
// window.location.href = data.error;
window.location.href = data.error;
return;
}
sessIdList = data.sessions;
@@ -1694,6 +1731,14 @@
document.getElementById("stopBtn").addEventListener("click", () => {
const sendBtnImg = document.querySelector('#sendBtn img');
const stopBtn = document.querySelector('#stopBtn');
console.log(controller, '暂停消息');
isStopped = true;
if (reader) {
reader.cancel("stop");
reader = null;
}
if (controller) {
controller.abort();
controller = null;