侧边标题功能完善
This commit is contained in:
111
index.html
111
index.html
@@ -192,6 +192,7 @@
|
||||
flex: 1;
|
||||
padding: 20px;
|
||||
overflow-y: auto;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.message {
|
||||
@@ -755,6 +756,9 @@
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
console.log(params.get("userId"), 644);
|
||||
const encodedUserId = params.get("userId");
|
||||
const from = params.get("from");
|
||||
|
||||
|
||||
|
||||
// ✅ 先 URL 解码,再 Base64 解码,再 UTF-8 解码
|
||||
const userId = decodeURIComponent(Number(escape(atob(encodedUserId))));
|
||||
@@ -871,17 +875,25 @@
|
||||
function toggleSidebar() {
|
||||
sidebar.classList.toggle("collapsed");
|
||||
}
|
||||
function scrollToBottom() {
|
||||
const chatMessages = document.getElementById("chatMessages");
|
||||
setTimeout(() => {
|
||||
chatMessages.scrollTop = chatMessages.scrollHeight;
|
||||
chatMessages.bottom = '0px';
|
||||
}, 0);
|
||||
}
|
||||
|
||||
|
||||
function renderChatList(isNew) {
|
||||
console.log(sessIdList);
|
||||
chatList.innerHTML = "";
|
||||
chatList.innerHTML = '';
|
||||
|
||||
if (!sessIdList || sessIdList.length == 0) {
|
||||
chatMessages.innerHTML = "";
|
||||
return;
|
||||
}
|
||||
// if (isNew == "new") {
|
||||
activeChatId = sessIdList[sessIdList.length - 1];
|
||||
activeChatId = sessIdList[sessIdList.length - 1].sessId;
|
||||
// } else {
|
||||
// activeChatId = sessIdList[0];
|
||||
// }
|
||||
@@ -891,19 +903,24 @@
|
||||
console.log(item, activeChatId);
|
||||
|
||||
const item1 = document.createElement("div");
|
||||
item1.setAttribute("data-sessionid", item);
|
||||
item1.className = "chat-item" + (activeChatId == item ? " active" : "");
|
||||
item1.setAttribute("data-sessionid", item.sessId);
|
||||
item1.className = "chat-item" + (activeChatId == item.sessId ? " active" : "");
|
||||
const title = document.createElement("div");
|
||||
title.className = "chat-title";
|
||||
title.textContent = `新对话${index + 1}`;
|
||||
item1.onclick = () => switchChat(item);
|
||||
if (item.title) {
|
||||
title.textContent = item.title;
|
||||
} else {
|
||||
title.textContent = `新对话${index + 1}`;
|
||||
}
|
||||
|
||||
item1.onclick = () => switchChat(item.sessId);
|
||||
|
||||
const del = document.createElement("span");
|
||||
del.className = "delete-btn";
|
||||
del.textContent = "✕";
|
||||
del.onclick = (e) => {
|
||||
e.stopPropagation();
|
||||
deleteChat(item);
|
||||
deleteChat(item.sessId);
|
||||
};
|
||||
item1.appendChild(title);
|
||||
item1.appendChild(del);
|
||||
@@ -926,6 +943,7 @@
|
||||
}
|
||||
addMessage(msg.content, msg.role, msg.image_base64);
|
||||
});
|
||||
scrollToBottom();
|
||||
}
|
||||
// 内容 角色 图片 创造时间
|
||||
function addMessage(content = '', role = "assistant", imgUrl = null, time = null) {
|
||||
@@ -1050,20 +1068,39 @@
|
||||
const data = line.slice(5).trim();
|
||||
if (data === "[DONE]") {
|
||||
// 更新聊天滚动条
|
||||
chatMessages.scrollTop = chatMessages.scrollHeight;
|
||||
scrollToBottom();
|
||||
// chatMessages.scrollTop = chatMessages.scrollHeight;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const obj = JSON.parse(data);
|
||||
if (obj.content) {
|
||||
// 每次追加内容到 AI 消息
|
||||
aiMsg.content += obj.content;
|
||||
msgDiv.innerHTML = marked.parse(aiMsg.content);
|
||||
// msgDiv.textContent = aiMsg.content;
|
||||
chatMessages.scrollTop = chatMessages.scrollHeight;
|
||||
// if (obj.content && obj.type != 'title') {
|
||||
// // 每次追加内容到 AI 消息
|
||||
// aiMsg.content += obj.content;
|
||||
// msgDiv.innerHTML = marked.parse(aiMsg.content);
|
||||
// // msgDiv.textContent = aiMsg.content;
|
||||
// chatMessages.scrollTop = chatMessages.scrollHeight;
|
||||
// }
|
||||
if (obj.content && obj.type !== "title") {
|
||||
for (const char of obj.content) {
|
||||
aiMsg.content += char;
|
||||
msgDiv.innerHTML = marked.parse(aiMsg.content);
|
||||
chatMessages.scrollTop = chatMessages.scrollHeight;
|
||||
scrollToBottom();
|
||||
}
|
||||
}
|
||||
if (obj.content && obj.type == 'title') {
|
||||
const chatItem = document.querySelector(`.chat-item[data-sessionid="${sessId}"]`);
|
||||
if (chatItem) {
|
||||
const chatTitle = chatItem.querySelector('.chat-title');
|
||||
if (chatTitle) {
|
||||
chatTitle.textContent = obj.content; // 设置标题
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
console.error("JSON parse error", data);
|
||||
}
|
||||
@@ -1074,7 +1111,7 @@
|
||||
|
||||
function newChat() {
|
||||
const id = generateUniqueId16();
|
||||
sessIdList.push(id);
|
||||
sessIdList.push({ sessId: id, title: "新对话" });
|
||||
createSession(userId, id)
|
||||
const chat = [{
|
||||
id,
|
||||
@@ -1112,7 +1149,7 @@
|
||||
}).then(res => res.json()).then(data => {
|
||||
if (data.code === 0) {
|
||||
getSessionList();
|
||||
// renderChatList();
|
||||
renderChatList();
|
||||
|
||||
console.log("删除成功:", data.message);
|
||||
} else {
|
||||
@@ -1217,6 +1254,12 @@
|
||||
|
||||
})
|
||||
}
|
||||
// sessIdList
|
||||
|
||||
|
||||
function isWeChat() {
|
||||
return /MicroMessenger/i.test(navigator.userAgent);
|
||||
}
|
||||
function getCookie(name) {
|
||||
console.log(document.cookie, 8777222)
|
||||
const value = `; ${document.cookie}`;
|
||||
@@ -1224,25 +1267,31 @@
|
||||
if (parts.length === 2) return parts.pop().split(';').shift();
|
||||
return null;
|
||||
}
|
||||
// gotoWeiXin();
|
||||
function gotoWeiXin() {
|
||||
const appid = "wx549f4ae0f85eb3ea";
|
||||
console.log(isWeChat(), 8888, navigator.userAgent);
|
||||
if (isWeChat()) {
|
||||
const appid = "wx549f4ae0f85eb3ea";
|
||||
// 回调地址(必须和公众号后台配置的网页授权域名一致)
|
||||
const redirect_uri = encodeURIComponent("http://ai.satabot.com/");
|
||||
// 授权方式:snsapi_base(静默) 或 snsapi_userinfo(需要用户确认)
|
||||
const scope = "snsapi_userinfo";
|
||||
// 自定义参数,授权后会原样返回
|
||||
const state = "123";
|
||||
// 拼接完整的授权链接
|
||||
const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
|
||||
// 跳转到微信授权
|
||||
window.location.href = authUrl;
|
||||
}
|
||||
|
||||
// 回调地址(必须和公众号后台配置的网页授权域名一致)
|
||||
const redirect_uri = encodeURIComponent("http://ai.satabot.com/");
|
||||
|
||||
// 授权方式:snsapi_base(静默) 或 snsapi_userinfo(需要用户确认)
|
||||
const scope = "snsapi_userinfo";
|
||||
|
||||
// 自定义参数,授权后会原样返回
|
||||
const state = "123";
|
||||
|
||||
// 拼接完整的授权链接
|
||||
const authUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
|
||||
|
||||
// 跳转到微信授权
|
||||
window.location.href = authUrl;
|
||||
}
|
||||
|
||||
// if(from=="weixin"){
|
||||
// gotoWeiXin();
|
||||
// }else{
|
||||
|
||||
// }
|
||||
|
||||
getSessionList();
|
||||
function getSessionList() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user