54 lines
1.9 KiB
JavaScript
54 lines
1.9 KiB
JavaScript
// 自动回显 AnythingLLM 密码
|
||
document.addEventListener("DOMContentLoaded", function () {
|
||
chrome.storage.local.get(["llmPassword"], function (res) {
|
||
if (res.llmPassword) {
|
||
document.getElementById("llmPassword").value = res.llmPassword;
|
||
}
|
||
});
|
||
});
|
||
|
||
document.getElementById("loginBtn").addEventListener("click", doLogin);
|
||
document.addEventListener("keydown", (e) => {
|
||
if (e.key === "Enter") doLogin();
|
||
});
|
||
|
||
function doLogin() {
|
||
const username = document.getElementById("username").value.trim();
|
||
const password = document.getElementById("password").value.trim();
|
||
const llmPassword = document.getElementById("llmPassword").value;
|
||
const errorBox = document.getElementById("errorBox");
|
||
errorBox.textContent = "";
|
||
|
||
if (!username || !password) {
|
||
errorBox.textContent = "请输入用户名和密码";
|
||
return;
|
||
}
|
||
|
||
// 保存 llmPassword(登录不依赖LLM密码是否填写,只是单独存储)
|
||
chrome.storage.local.set({ llmPassword });
|
||
|
||
fetch("http://app.wisdompulse.cn/api/v1/user/login", {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({ username, password })
|
||
})
|
||
.then(res => res.json().then(data => ({ status: res.status, body: data })))
|
||
.then(({ status, body }) => {
|
||
if (status === 200 && body.token) {
|
||
// 登录成功,保存登录时间
|
||
chrome.storage.local.set({
|
||
token: body.token,
|
||
username: body.username || username,
|
||
loginAt: Date.now()
|
||
}, () => {
|
||
window.location.href = chrome.runtime.getURL("pages/popup.html");
|
||
});
|
||
} else {
|
||
errorBox.textContent = body.message || "登录失败";
|
||
}
|
||
})
|
||
.catch(() => {
|
||
errorBox.textContent = "网络错误,请稍后重试";
|
||
});
|
||
}
|