完成登录配置化

This commit is contained in:
luke 2025-06-02 17:19:57 +08:00
parent 6606e100c9
commit 1398827d0e
3 changed files with 64 additions and 42 deletions

View File

@ -55,6 +55,7 @@
<button id="loginBtn">登录</button> <button id="loginBtn">登录</button>
</div> </div>
<div class="error" id="errorBox"></div> <div class="error" id="errorBox"></div>
<script src="../scripts/config.js"></script>
<script src="../scripts/login.js"></script> <script src="../scripts/login.js"></script>
</body> </body>
</html> </html>

View File

@ -97,3 +97,5 @@ function loadMergedConfig() {
}); });
}); });
} }
window.loadMergedConfig = loadMergedConfig;

View File

@ -1,11 +1,24 @@
// 自动回显 AnythingLLM 密码
document.addEventListener("DOMContentLoaded", function () { document.addEventListener("DOMContentLoaded", async function () {
const config = await window.loadMergedConfig();
const loginConf = config.apiConfig.login || {};
const loginUrl = loginConf.url;
const reqParams = loginConf.requestParams || [];
const resParams = loginConf.responseParams || [];
// 提取字段名(带默认值)
const reqUsernameField = reqParams.find(p => p.label.includes("用户名"))?.field || "username";
const reqPasswordField = reqParams.find(p => p.label.includes("密码"))?.field || "password";
const resTokenField = resParams.find(p => p.label.includes("Token"))?.field || "token";
const resUsernameField = resParams.find(p => p.label.includes("用户名"))?.field || "username";
// 自动填充 LLM 密码(与登录无关,只是附带存储)
chrome.storage.local.get(["llmPassword"], function (res) { chrome.storage.local.get(["llmPassword"], function (res) {
if (res.llmPassword) { if (res.llmPassword) {
document.getElementById("llmPassword").value = res.llmPassword; document.getElementById("llmPassword").value = res.llmPassword;
} }
}); });
});
document.getElementById("loginBtn").addEventListener("click", doLogin); document.getElementById("loginBtn").addEventListener("click", doLogin);
document.addEventListener("keydown", (e) => { document.addEventListener("keydown", (e) => {
@ -24,21 +37,26 @@ function doLogin() {
return; return;
} }
// 保存 llmPassword登录不依赖LLM密码是否填写只是单独存储
chrome.storage.local.set({ llmPassword }); chrome.storage.local.set({ llmPassword });
fetch("http://app.wisdompulse.cn/api/v1/user/login", { const requestBody = {};
requestBody[reqUsernameField] = username;
requestBody[reqPasswordField] = password;
fetch(loginUrl, {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username, password }) body: JSON.stringify(requestBody)
}) })
.then(res => res.json().then(data => ({ status: res.status, body: data }))) .then(res => res.json().then(data => ({ status: res.status, body: data })))
.then(({ status, body }) => { .then(({ status, body }) => {
if (status === 200 && body.token) { const token = body[resTokenField];
// 登录成功,保存登录时间 const returnedUser = body[resUsernameField] || username;
if (status === 200 && token) {
chrome.storage.local.set({ chrome.storage.local.set({
token: body.token, token: token,
username: body.username || username, username: returnedUser,
loginAt: Date.now() loginAt: Date.now()
}, () => { }, () => {
window.location.href = chrome.runtime.getURL("pages/popup.html"); window.location.href = chrome.runtime.getURL("pages/popup.html");
@ -51,3 +69,4 @@ function doLogin() {
errorBox.textContent = "网络错误,请稍后重试"; errorBox.textContent = "网络错误,请稍后重试";
}); });
} }
});