43 lines
1.4 KiB
JavaScript
43 lines
1.4 KiB
JavaScript
const TOKEN_EXPIRE_MS = 24 * 60 * 60 * 1000; // 默认保留 1天
|
|
|
|
document.getElementById("loginBtn").addEventListener("click", async () => {
|
|
const username = document.getElementById("username").value.trim();
|
|
const password = document.getElementById("password").value.trim();
|
|
const errorBox = document.getElementById("errorBox");
|
|
errorBox.textContent = "";
|
|
|
|
if (!username || !password) {
|
|
errorBox.textContent = "请输入用户名和密码";
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const res = await fetch("http://app.wisdompulse.cn/api/v1/user/login", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ username, password })
|
|
});
|
|
|
|
const data = await res.json();
|
|
if (res.ok && data.token) {
|
|
await chrome.storage.local.set({
|
|
token: data.token,
|
|
username: data.username || username,
|
|
loginAt: Date.now()
|
|
});
|
|
chrome.action.setPopup({ popup: "popup.html" });
|
|
window.location.href = "popup.html";
|
|
} else {
|
|
errorBox.textContent = data.message || "登录失败";
|
|
}
|
|
} catch (err) {
|
|
errorBox.textContent = "网络错误,请稍后重试";
|
|
}
|
|
});
|
|
|
|
document.addEventListener("keydown", (e) => {
|
|
if (e.key === "Enter") {
|
|
document.getElementById("loginBtn").click();
|
|
}
|
|
});
|