修复提示上传图片的逻辑

This commit is contained in:
xiaobao
2025-09-21 11:01:07 +08:00
parent ba07255cfe
commit 9cad03d6f9

View File

@@ -17,6 +17,25 @@
} }
.toast {
position: fixed;
top: 20%;
left: 50%;
transform: translateX(-50%);
background: rgba(0, 0, 0, 0.8);
color: #fff;
padding: 10px 18px;
border-radius: 8px;
font-size: 15px;
opacity: 0;
transition: opacity 0.3s ease;
z-index: 9999;
}
.toast.show {
opacity: 1;
}
/* 侧边栏 */ /* 侧边栏 */
.sidebar { .sidebar {
width: 240px; width: 240px;
@@ -728,6 +747,7 @@
const chatList = document.getElementById("chatList"); const chatList = document.getElementById("chatList");
let sessIdList = []; let sessIdList = [];
let isUpload = {}; let isUpload = {};
let hasImage = false;
let isPre = false;//是否点击了预设问题 let isPre = false;//是否点击了预设问题
// let sessIdList = JSON.parse(localStorage.getItem("sessId")) || []; // let sessIdList = JSON.parse(localStorage.getItem("sessId")) || [];
let sessId; let sessId;
@@ -761,11 +781,15 @@
isPre = true; isPre = true;
console.log(isUpload, 88) console.log(isUpload, 88)
if (!sendParam.imageBase64 && !fileInput.files.length) { if (!sendParam.imageBase64 && !fileInput.files.length) {
if (!isUpload[activeChatId]) { // if (!isUpload[activeChatId]) {
alert("请先上传图片"); if (!hasImage) {
showToast("请先上传图片");
// alert("请先上传图片");
return; return;
} }
} }
const value = q.getAttribute("data-value"); const value = q.getAttribute("data-value");
const preValue = q.getAttribute("data-pre-value"); const preValue = q.getAttribute("data-pre-value");
@@ -824,6 +848,24 @@
console.error("请求异常:", err); console.error("请求异常:", err);
} }
} }
function showToast(message) {
// 创建元素
const toast = document.createElement("div");
toast.className = "toast";
toast.innerText = message;
document.body.appendChild(toast);
// 显示
setTimeout(() => {
toast.classList.add("show");
}, 10);
// 2 秒后自动消失
setTimeout(() => {
toast.classList.remove("show");
setTimeout(() => toast.remove(), 300); // 动画结束后移除
}, 2000);
}
function toggleSidebar() { function toggleSidebar() {
@@ -879,7 +921,10 @@
return; return;
} }
data.forEach(msg => { data.forEach(msg => {
addMessage(msg.content, msg.role, msg.image_base64, msg.created_at); if (msg.image_base64) {
hasImage = true; // 只要有一个不为空,就置为 true
}
addMessage(msg.content, msg.role, msg.image_base64);
}); });
} }
// 内容 角色 图片 创造时间 // 内容 角色 图片 创造时间
@@ -935,6 +980,7 @@
let imgUrl = ''; let imgUrl = '';
if (sendParam.imageBase64) { if (sendParam.imageBase64) {
imgUrl = sendParam.imageBase64; imgUrl = sendParam.imageBase64;
hasImage = true;
if (fileInput.files[0]) { if (fileInput.files[0]) {
const file = fileInput.files[0]; const file = fileInput.files[0];
const url = URL.createObjectURL(file); const url = URL.createObjectURL(file);
@@ -1033,7 +1079,7 @@
const chat = [{ const chat = [{
id, id,
title: "新对话 " + (sessIdList.length), title: "新对话 " + (sessIdList.length),
messages: { role: "assistant", content: "👋 新会话已创建,请输入问题。", } messages: { role: "assistant", content: "👋 新会话已创建,请输入问题。", image_base64: '' }
}]; }];
activeChatId = id; activeChatId = id;
ifSessionId = true; ifSessionId = true;