2025-07-16 17:12:35 +08:00
|
|
|
|
/* -------------------- 配置 -------------------- */
|
|
|
|
|
const sPayMallUrl = "http://127.0.0.1:8092";
|
|
|
|
|
|
2025-07-17 18:30:14 +08:00
|
|
|
|
/* -------------------- Cookie 工具 -------------------- */
|
2025-07-16 17:12:35 +08:00
|
|
|
|
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", () => {
|
2025-07-17 18:30:14 +08:00
|
|
|
|
|
|
|
|
|
// ① 异步加载 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";
|
|
|
|
|
});
|
2025-07-16 17:12:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-17 18:30:14 +08:00
|
|
|
|
/* ---------- 获取二维码 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");
|
2025-07-16 17:12:35 +08:00
|
|
|
|
|
2025-07-17 18:30:14 +08:00
|
|
|
|
// ② 开始轮询检查登录状态
|
|
|
|
|
const intervalId = setInterval(() => {
|
|
|
|
|
checkLoginStatus(ticket, sceneStr, intervalId);
|
|
|
|
|
}, 3000); // 每 3 秒检查一次
|
|
|
|
|
})
|
|
|
|
|
.catch(err => console.error("请求失败:", err));
|
2025-07-16 17:12:35 +08:00
|
|
|
|
})
|
2025-07-17 18:30:14 +08:00
|
|
|
|
.catch(err => console.error("FingerprintJS 加载失败:", err));
|
2025-07-16 17:12:35 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/* -------------------- 轮询检查登录 -------------------- */
|
2025-07-17 18:30:14 +08:00
|
|
|
|
function checkLoginStatus(ticket, sceneStr, intervalId) {
|
|
|
|
|
fetch(`${sPayMallUrl}/api/v1/login/check_login_scene?ticket=${ticket}&sceneStr=${sceneStr}`)
|
2025-07-16 17:12:35 +08:00
|
|
|
|
.then(res => res.json())
|
|
|
|
|
.then(data => {
|
|
|
|
|
if (data.code === "0000") {
|
2025-07-17 18:30:14 +08:00
|
|
|
|
console.info("login success");
|
2025-07-16 17:12:35 +08:00
|
|
|
|
clearInterval(intervalId);
|
|
|
|
|
|
2025-07-17 18:30:14 +08:00
|
|
|
|
// 把后端返回的登录凭证写入 cookie(30 天)
|
2025-07-16 17:12:35 +08:00
|
|
|
|
setCookie("loginToken", data.data, 30);
|
|
|
|
|
|
|
|
|
|
// 跳转首页
|
|
|
|
|
window.location.href = "./index.html";
|
|
|
|
|
} else {
|
|
|
|
|
console.info("login wait");
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(err => console.error("请求失败:", err));
|
|
|
|
|
}
|