From a95652b458498dcdfb9926200bdc3b22ba67e0e9 Mon Sep 17 00:00:00 2001 From: geyun <1556375442@qq.com> Date: Wed, 19 Nov 2025 11:26:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E4=BC=97=E5=8F=B7=E5=BC=B9=E7=AA=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 336 ++++++++++++++++++++++++++++++++++++++++++++++++----- server2.js | 3 +- 2 files changed, 306 insertions(+), 33 deletions(-) diff --git a/index.html b/index.html index 161f1b3..7c0eb61 100755 --- a/index.html +++ b/index.html @@ -3,7 +3,7 @@ - + @@ -12,9 +12,16 @@ - + @@ -1179,6 +1294,7 @@
+
+
+
+ +
+
- + @@ -1246,6 +1367,7 @@ const chatList = document.getElementById("chatList"); let controller = null; let sessIdList = []; + let openId = ''; let isStopped = false;//是否停止输出 let isUpload = {}; let hasImage = false; @@ -1272,7 +1394,9 @@ let userId; if (from == "weixin") { userId = encodedUserId; - document.querySelector('.logout').style.display = 'none'; + openId = encodedUserId; + setCookie('openId', openId) + document.querySelector('.userinfo-panel .logout').style.display = 'none'; } else { userId = decodeURIComponent(Number(escape(atob(encodedUserId)))); } @@ -1327,29 +1451,144 @@ } } + function handleLogin(data) { + console.log("开始登录") + const loginParams = data; + return new Promise((resolve, reject) => { + fetch("/api/remote/login", { + method: "POST", // 必选:指定请求方法为 POST + headers: { + "Content-Type": "application/json", // 声明请求体是 JSON 格式 + // 可选:如果接口需要 Token 前置验证,可添加 Authorization 头 + // "Authorization": "Bearer 你的前置 Token" + }, + // 必选:请求体,将参数对象转为 JSON 字符串(不能直接传对象) + body: JSON.stringify(loginParams) + }) + .then(res => { + console.log(res, 987) + if (!res.ok) { + throw new Error(`请求失败:${res.status} ${res.statusText}`); + } + return res.json(); // 只有 2xx 状态才解析 JSON + }) + .then(data => { + console.log("登录回复") + resolve(data); + }) + .catch(err => { + reject(err); + }); + }); + } + // 发送验证码 + + function handleBind(num) { + const bindParams = { + phoneNumbers: num, + } + return new Promise((resolve, reject) => { + fetch("/api/remote/external/sms/sendAliSmsCode", { + method: "POST", // 必选:指定请求方法为 POST + headers: { + "Content-Type": "application/json", // 声明请求体是 JSON 格式 + // 可选:如果接口需要 Token 前置验证,可添加 Authorization 头 + // "Authorization": "Bearer 你的前置 Token" + }, + // 必选:请求体,将参数对象转为 JSON 字符串(不能直接传对象) + body: JSON.stringify(bindParams) + }) + .then(res => { + + if (!res.ok) { + throw new Error(`请求失败:${res.status} ${res.statusText}`); + } + return res.json(); // 只有 2xx 状态才解析 JSON + }) + .then(data => { + resolve(data); + }) + .catch(err => { + reject(err); + }); + }); + } // } + function bindToLogin() { + debugger; + document.querySelector('.popbox .loading-box').classList.remove('none'); + const data = { + sourceType: 5, + openId: openId, + phonenumber: document.getElementById('sys-phone').value, + smsCode: document.getElementById('code').value + } + try { + handleLogin(data).then(res => { + if (res.code == 200) { + document.querySelector('.popbox .loading-box').classList.add('none') + userId = res.userId; + setCookie('token', res.token); + unshowpop(); + getSessionList(); + + } else { + showToast(res.msg); + document.querySelector('.popbox .loading-box').classList.remove('none'); + } + }).catch(err => { + + // 捕获异步异常(如网络错误、接口报错、代码报错) + console.error("请求异常:", err); + showToast("登录失败,请重试" + err); // 给用户友好提示 + }) + .finally(() => { + unshowpop(); + document.querySelector('.popbox .loading-box').classList.add('none'); + }); + + } catch (err) { + console.error("请求异常:", err); + } + } function getUserByOpenId(id) { + const data = { + sourceType: 5, + openId: id, + + phonenumber: '', + smsCode: '', + } try { - fetch("/api/getuserByopenid", { - method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - userId: id, + handleLogin(data).then(res => { + if (res.code == 300) { + showpop();//出现验证码弹窗 + } else if (res.code == 200) { + setCookie('token', res.token); + userId = res.userId; - }) - }).then(res => res.json()).then(data => { - if (data.code == 0) { - - userId = data.userId; - sendParam.userId = userId; - getSessionList(); - } else { - console.error("未找到该用户:",); } }) + // fetch("/api/getuserByopenid", { + // method: "POST", + // headers: { + // "Content-Type": "application/json" + // }, + // body: JSON.stringify({ + // userId: id, + + // }) + // }).then(res => res.json()).then(data => { + // if (data.code == 0) { + + // userId = data.userId; + // sendParam.userId = userId; + // getSessionList(); + // } else { + // console.error("未找到该用户:",); + // } + // }) } catch (err) { console.error("请求异常:", err); } @@ -1607,12 +1846,12 @@ }); setTimeout(scrollToBottom, 0); } - function setCookie(name, value) { + function setcookie(name, value) { const d = new Date(); d.setFullYear(d.getFullYear() + 100); // +100 年 document.cookie = `${name}=${value};expires=${d.toUTCString()};path=/`; } - function getCookie(name) { + function getcookie(name) { const cookies = document.cookie.split("; "); for (let c of cookies) { const [key, val] = c.split("="); @@ -1627,9 +1866,9 @@ return; } const cookieName = `popupClickCount_${userId}`; - let clickCount = parseInt(getCookie(cookieName) || "0", 10); + let clickCount = parseInt(getcookie(cookieName) || "0", 10); clickCount++; - setCookie(cookieName, clickCount, 7); + setcookie(cookieName, clickCount, 7); console.log(`用户 ${userId} 当前点击次数:`, clickCount); if (clickCount == 10) { document.querySelector(".popbgc").classList.remove("none"); @@ -1747,6 +1986,8 @@ // sendParam.userId= encodedUserId; if (from != "weixin") { sendParam.userId = decodeURIComponent(Number(escape(atob(encodedUserId)))); + } else { + sendParam.userId = userId; } // ✅ 创建 AbortController @@ -1890,6 +2131,7 @@ } function getSms() { let remainSeconds = 60; + let countTimer = setInterval(() => { if (remainSeconds > 0) { remainSeconds--; @@ -1900,16 +2142,45 @@ document.getElementById('send-code').textContent = "获取验证码"; document.getElementById('send-code').disabled = false; } - - - }, 1000) + const phonenumber = document.getElementById('sys-phone').value; + handleBind(phonenumber).then(res => { + if (res.code == 200) { + + } else { + showToast(res.msg); + clearInterval(countTimer); + document.getElementById('send-code').textContent = "获取验证码"; + document.getElementById('send-code').disabled = false; + return; + } + console.log(res); + }).catch(err => { + showToast(err); + clearInterval(countTimer); + document.getElementById('send-code').textContent = "获取验证码"; + document.getElementById('send-code').disabled = false; + return; + }) + } - // showpop(); + function unshowpop() { + clearInterval(countTimer); + const popbox = document.querySelector('.popbox'); + if (popbox) { + popbox.remove(); + } + } + function showpop() { let html = `
+
-

欢迎使用AI智能体

+
+
+ +
+

欢迎使用光伏运维智能体

请先绑定手机号

@@ -1925,7 +2196,7 @@
- @@ -2121,6 +2392,7 @@ function getSessionList() { + console.log(userId, "huoqu") sendParam.userId = userId; fetch(`/api/sessions?userId=${sendParam.userId}`, { diff --git a/server2.js b/server2.js index 3383a6e..8b30559 100755 --- a/server2.js +++ b/server2.js @@ -155,6 +155,7 @@ app.post('/wechat', async (req, res) => { if (key === 'AI_INTELLIGENT') { try { + const timestamp = Date.now(); // 方案一:简单文本回复(推荐,避免超时) const reply = ` @@ -162,7 +163,7 @@ app.post('/wechat', async (req, res) => { ${Date.now()} - 立即进入AI智能体]]> + 立即进入AI智能体]]> `; res.send(reply);