优化公众号

This commit is contained in:
2025-11-26 10:38:24 +08:00
parent 17a5abd399
commit fe54a72420
2 changed files with 168 additions and 3 deletions

View File

@@ -177,17 +177,95 @@
var vConsole = new VConsole();
// wx364b8ebf17fe45fe wx0eeb7556d60121db
console.log("✅ vConsole 已经启动");
const url = encodeURIComponent("https://ai.satabot.com?userid=6353rf")
const params = new URLSearchParams(window.location.search);
const code = params.get("code");
handlelogin()
function handlelogin() {
const APP_ID = 'wxa957108dae7d9048';
const REDIRECT_URI = encodeURIComponent("https://ai.satabot.com");
// const REDIRECT_URI = encodeURIComponent("https://ai.satabot.com/middle.html");
const STATE = Math.random().toString(36).substring(2, 12);
const SCOPE = 'snsapi_userinfo';
// const authUrl = `https://open.weixin.qq.com/connect/qrconnect?appid=${APP_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=${SCOPE}&state=${STATE}#wechat_redirect`;
// window.location.href = authUrl; // 跳转后页面刷新,后续代码不执行
window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${APP_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=${SCOPE}&state=${STATE}&connect_redirect=1#wechat_redirect`
}
function handleCode(code) {
if (code) {
console.log(code, "code");
fetch(`https://servicestream.satabot.com/api/wechat/access_token?code=${code}`)
.then(res => res.json())
.then(res => {
console.log(res.nickname)
openId = res.openid;
userId = res.openid;
getUserByOpenId(res.openid)
});
} else {
return; // ✅ 没问题,直接退出函数
}
}
function getUserByOpenId(id) {
const data = {
sourceType: 5,
openId: id,
phonenumber: '',
smsCode: '',
}
retryFetch("https://serviceri.satabot.com/login", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
})
.then(res => {
if (res.code == 300) {
showpop(); // 弹出验证码
} else if (res.code == 200) {
setCookie('token', res.token);
userId = res.userId;
getSessionList();
}
})
.catch(err => {
console.error("登录请求失败(重试10次后):", err);
showToast("网络异常,请稍后再试");
})
.finally(() => {
if (!document.querySelector(".weixin-loading").classList.contains("none")) {
document.querySelector(".weixin-loading").classList.add("none");
}
console.log("登录请求结束");
});
}
async function retryFetch(url, options, maxRetry = 10) {
let lastError;
for (let i = 0; i < maxRetry; i++) {
try {
console.log(`登录请求第 ${i + 1} 次尝试...`);
const response = await fetch(url, options);
return await response.json();
} catch (err) {
lastError = err;
console.warn(`第 ${i + 1} 次尝试失败:`, err);
// 最后一次重试失败 → 抛出错误
if (i === maxRetry - 1) throw err;
// 重试前延迟 300ms
await new Promise(res => setTimeout(res, 1000));
}
}
throw lastError;
}
</script>
<script>