微信公众号和网页端的AI登录判断方式 融合多平台

This commit is contained in:
2025-11-16 21:39:26 +08:00
parent e1b1c4ca1f
commit c6a974fa8c
3 changed files with 358 additions and 135 deletions

View File

@@ -1109,9 +1109,17 @@
// ✅ 先 URL 解码,再 Base64 解码,再 UTF-8 解码
const userId = decodeURIComponent(Number(escape(atob(encodedUserId))));
let userId;
if (from == "weixin") {
userId = encodedUserId;
document.querySelector('.logout').style.display = 'none';
} else {
userId = decodeURIComponent(Number(escape(atob(encodedUserId))));
}
setCookie("userId", userId);
getUserName(userId);
// const userId = encodedUserId;
let sendParam = {
imageBase64: '',
@@ -1143,46 +1151,90 @@
// } else {
if (getCookie("userId")) {
getSessionList();
if (from == "weixin") {
getUserByOpenId(userId)
} else {
getSessionList();
}
} else {
deleteCookie('userId');
window.location.href = 'http://ri.satabot.com/#/login?goRemote=1&clear=1';
if (from == "weixin") {
} else {
window.location.href = 'http://ri.satabot.com/#/login?goRemote=1&clear=1';
}
}
// }
const questions = document.querySelectorAll(".question");
// 获取到用户名称
function getUserName(userId) {
function getUserByOpenId(id) {
try {
fetch("/api/user", {
fetch("/api/getuserByopenid", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
userId,
userId: id,
})
}).then(res => res.json()).then(data => {
if (data.code === 0) {
document.querySelectorAll('.username').forEach((e) => {
e.textContent = data.userName;
})
console.log("成功找到该用户:", data.userName, data.userId);
if (data.code == 0) {
userId = data.userId;
sendParam.userId = userId;
getSessionList();
} else {
console.error("未找到该用户:",);
}
})
} catch (err) {
console.error("请求异常:", err);
}
}
const questions = document.querySelectorAll(".question");
// 获取到用户名称
function getUserName(userId) {
if (from == "weixin") {
document.querySelectorAll('.username').forEach((e) => {
e.textContent = userId;
})
} else {
try {
fetch("/api/user", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
userId,
})
}).then(res => res.json()).then(data => {
if (data.code === 0) {
document.querySelectorAll('.username').forEach((e) => {
e.textContent = data.userName;
})
console.log("成功找到该用户:", data.userName, data.userId);
} else {
window.location.href = data.error;
return;
console.error("未找到该用户:",);
}
})
} catch (err) {
console.error("请求异常:", err);
}
}
}
questions.forEach(q => {
@@ -1196,7 +1248,7 @@
// }
}
isPre = true;
isPre = true;//s是否预设题目
console.log(isUpload, 88)
if (!sendParam.imageBase64 && !fileInput.files.length) {
// if (!isUpload[activeChatId]) {
@@ -1458,6 +1510,7 @@
function sendMessage(isPre) {
debugger;
console.log("发送消息1122");
if (controller) {
showToast("请先停止对话");
@@ -1532,7 +1585,10 @@
sessId = sendParam.sessionId;
sendParam.imageBase64 = img;
// sendParam.userId= encodedUserId;
sendParam.userId = decodeURIComponent(Number(escape(atob(encodedUserId))));
if (from != "weixin") {
sendParam.userId = decodeURIComponent(Number(escape(atob(encodedUserId))));
}
// ✅ 创建 AbortController
controller = new AbortController();
sendParam.requestId = crypto.randomUUID();
@@ -1789,6 +1845,7 @@
}).then(res => res.json()).then(data => {
// chats = data.messages;
// renderChatList();
if (controller) return;
renderMessages(data.messages)
})
@@ -1830,27 +1887,7 @@
// window.location.href = url; // 跳转到微信授权页面
// }
// simulateWechatLogin()
// 页面加载时立即执行:检查是否有微信返回的 code
document.addEventListener('DOMContentLoaded', function () {
const params = new URLSearchParams(window.location.search);
const code = params.get('code');
const state = params.get('state');
if (code) {
console.log('微信返回 code:', code);
// 发送 code 到后端换取用户信息(这里才会执行)
fetch('/api/wechat-login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code })
})
.then(res => res.json())
.then(data => {
console.log('微信用户信息:', data.userInfo);
})
.catch(err => console.error(err));
}
});
// 触发登录时执行:跳转微信授权页
function simulateWechatLogin() {