2025-07-17 18:30:14 +08:00

78 lines
3.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* -------------------- 配置 -------------------- */
const sPayMallUrl = "http://127.0.0.1:8092";
/* -------------------- Cookie 工具 -------------------- */
function setCookie(name, value, days) {
const date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
document.cookie = `${name}=${value};expires=${date.toUTCString()};path=/`;
}
/* -------------------- 主逻辑 -------------------- */
document.addEventListener("DOMContentLoaded", () => {
// ① 异步加载 FingerprintJS 并计算浏览器指纹
import("https://openfpcdn.io/fingerprintjs/v4")
.then(FingerprintJS => FingerprintJS.load())
.then(fp => fp.get())
.then(result => {
const sceneStr = result.visitorId.toUpperCase(); // 作为场景值
/* ---------- 无痕登录按钮 ---------- */
const stealthBtn = document.getElementById("stealth-login-btn");
if (stealthBtn) {
stealthBtn.addEventListener("click", () => {
/**
* 建议:此处可调用后端 “auto_login?sceneStr=xxx” 接口。
* 下面仅示例:直接把指纹写 cookie 后跳首页。
*/
setCookie("loginToken", sceneStr, 30);
window.location.href = "./index.html";
});
}
/* ---------- 获取二维码 ticket ---------- */
fetch(`${sPayMallUrl}/api/v1/login/weixin_qrcode_ticket_scene?sceneStr=${sceneStr}`)
.then(res => res.json())
.then(data => {
if (data.code !== "0000") {
console.error("获取二维码 ticket 失败:", data.info);
return;
}
const ticket = data.data;
const qrCodeImg = document.getElementById("qr-code-img");
qrCodeImg.src = `https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=${ticket}`;
qrCodeImg.classList.remove("pulse");
// ② 开始轮询检查登录状态
const intervalId = setInterval(() => {
checkLoginStatus(ticket, sceneStr, intervalId);
}, 3000); // 每 3 秒检查一次
})
.catch(err => console.error("请求失败:", err));
})
.catch(err => console.error("FingerprintJS 加载失败:", err));
});
/* -------------------- 轮询检查登录 -------------------- */
function checkLoginStatus(ticket, sceneStr, intervalId) {
fetch(`${sPayMallUrl}/api/v1/login/check_login_scene?ticket=${ticket}&sceneStr=${sceneStr}`)
.then(res => res.json())
.then(data => {
if (data.code === "0000") {
console.info("login success");
clearInterval(intervalId);
// 把后端返回的登录凭证写入 cookie30 天)
setCookie("loginToken", data.data, 30);
// 跳转首页
window.location.href = "./index.html";
} else {
console.info("login wait");
}
})
.catch(err => console.error("请求失败:", err));
}