公众号弹窗

This commit is contained in:
2025-11-19 11:26:22 +08:00
parent de32939f12
commit a95652b458
2 changed files with 306 additions and 33 deletions

View File

@@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-store, no-cache, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="Thu, 01 Jan 1970 00:00:00 GMT">
@@ -12,9 +12,16 @@
<!-- <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> -->
<script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script>
<style>
* {
touch-action: manipulation;
/* 允许平移和捏合缩放,但禁用双击缩放和长按 */
-webkit-tap-highlight-color: transparent;
/* 移除点击时的默认高亮(可选,优化体验) */
}
body {
margin: 0;
font-family: "Segoe UI", "PingFang SC", Arial, sans-serif;
@@ -787,7 +794,8 @@
/* 获取验证码按钮 */
.code-btn {
border: 1px solid #1890ff;
border: none;
/* border: 1px solid #1890ff; */
background: white;
padding: 5px 12px;
border-radius: 6px;
@@ -911,10 +919,45 @@
z-index: 999999;
}
.popbox .loading-box {
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
z-index: 999999999;
display: flex;
justify-content: center;
align-items: center;
background-color: #fff;
opacity: 0.7;
}
.popbox .loading-box.none {
display: none;
}
.popbox .loading-box .loading {
border: solid #e2e8f0;
/* 浅灰色边框 */
border-top-color: #165DFF;
/* 蓝色顶部边框 */
border-radius: 50%;
animation: spin 1s linear infinite;
/* 旋转动画 */
display: inline-block;
margin-bottom: 20px;
width: 30px;
height: 30px;
}
#sms-form {
background-color: #fff;
border-radius: 8px;
padding: 10px;
position: relative;
}
</style>
<style>
@@ -940,6 +983,36 @@
z-index: 999999;
transition: all 0.3s ease-in-out;
backdrop-filter: blur(2px);
position: relative;
}
.popbgc .loading-box {
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
z-index: 999999999;
}
.popbgc .loading-box.none {
display: none;
}
.popbgc .loading-box .loading {
border: solid #e2e8f0;
/* 浅灰色边框 */
border-top-color: #165DFF;
/* 蓝色顶部边框 */
border-radius: 50%;
animation: spin 1s linear infinite;
/* 旋转动画 */
display: inline-block;
margin-bottom: 20px;
width: 30px;
height: 30px;
}
@@ -1006,6 +1079,10 @@
display: none;
}
.weixin-loading {
display: none;
}
@media (max-width: 768px) {
.popbgc {
width: 100%;
@@ -1018,6 +1095,44 @@
width: 90%;
}
.weixin-loading {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
position: fixed;
z-index: 99999;
background-color: #fff;
top: 0px;
left: 0px;
opacity: 0.7;
}
.weixin-loading.none {
display: none;
}
.weixin-loading .loading {
border: solid #e2e8f0;
/* 浅灰色边框 */
border-top-color: #165DFF;
/* 蓝色顶部边框 */
border-radius: 50%;
animation: spin 1s linear infinite;
/* 旋转动画 */
display: inline-block;
margin-bottom: 20px;
width: 30px;
height: 30px;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
}
</style>
</head>
@@ -1179,6 +1294,7 @@
</div>
</div>
<div class="popbgc none">
<div class="popup">
<h2>开启光伏运维智能化!</h2>
<form id="popupForm">
@@ -1221,18 +1337,23 @@
</form>
</div>
</div>
<div class="weixin-loading none ">
<div class="loading">
</div>
</div>
</div>
<script src="./marked.min.js"></script>
<!-- <script>
<script>
// 开启 vConsole 调试面板
var vConsole = new VConsole();
console.log("✅ vConsole 已经启动");
</script> -->
</script>
@@ -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 = `<div class='popbox'>
<div class="form sms-form" id="sms-form">
<h2 style='margin:0px'>欢迎使用AI智能体</h2>
<div class="loading-box none">
<div class="loading"></div>
</div>
<h2 style='margin:0px'>欢迎使用光伏运维智能体</h2>
<p style='color:#666;margin:13px 0px;'>请先绑定手机号</p>
<div class="input-group">
@@ -1925,7 +2196,7 @@
<button class="code-btn" id="send-code" onclick="getSms()">获取验证码</button>
</div>
<div class="form-group">
<button type="button" class="button button-primary" id="sms-button">
<button type="button" class="button button-primary" id="sms-button" onclick="bindToLogin()">
<!-- <i class="fas fa-user-plus mr-2"></i> -->
绑定
</button>
@@ -2121,6 +2392,7 @@
function getSessionList() {
console.log(userId, "huoqu")
sendParam.userId = userId;
fetch(`/api/sessions?userId=${sendParam.userId}`, {

View File

@@ -155,6 +155,7 @@ app.post('/wechat', async (req, res) => {
if (key === 'AI_INTELLIGENT') {
try {
const timestamp = Date.now();
// 方案一:简单文本回复(推荐,避免超时)
const reply = `
<xml>
@@ -162,7 +163,7 @@ app.post('/wechat', async (req, res) => {
<FromUserName><![CDATA[${toUser}]]></FromUserName>
<CreateTime>${Date.now()}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[欢迎使用AI智能体!正在为您准备服务...<a href="https://ai.satabot.com?userId=${fromUser}&from=weixin" target="_blank">立即进入AI智能体</a>]]></Content>
<Content><![CDATA[欢迎使用AI智能体!正在为您准备服务...<a href="https://ai.satabot.com?userId=${fromUser}&from=weixin&ts=${timestamp}" target="_blank">立即进入AI智能体</a>]]></Content>
</xml>
`;
res.send(reply);