From ea2d83087e8706eaefb444eb6e07371b6b7d4ff0 Mon Sep 17 00:00:00 2001 From: "Luke.Ye" Date: Sun, 22 Mar 2026 13:52:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=87=E6=8D=A2=E5=AE=B6?= =?UTF-8?q?=E7=94=A8ipv6=E9=97=AE=E9=A2=98=E2=80=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manifest.json | 7 +++-- scripts/config.js | 15 +++++---- scripts/login.js | 79 +++++++++++++++++++++++++++++------------------ 3 files changed, 62 insertions(+), 39 deletions(-) diff --git a/manifest.json b/manifest.json index 913ba86..74da4e4 100644 --- a/manifest.json +++ b/manifest.json @@ -1,13 +1,14 @@ { "manifest_version": 3, "name": "私域知识库", - "version": "2.1.0", + "version": "2.1.2", "description": "通过关键词快速搜索内部文档", "permissions": ["storage"], "host_permissions": [ "http://app.wisdompulse.cn/*", "http://share.wisdompulse.cn/*", - "http://app-test.wisdompulse.cn/*" + "http://app-test.wisdompulse.cn/*", + "https://share.wisdompulse.cn/*" ], "action": { "default_popup": "pages/index.html", @@ -17,4 +18,4 @@ "128": "assets/icon.png" } } -} +} \ No newline at end of file diff --git a/scripts/config.js b/scripts/config.js index 441a9b7..777f8c6 100644 --- a/scripts/config.js +++ b/scripts/config.js @@ -5,7 +5,7 @@ const defaultConfig = { apiConfig: { materialPublicUrl: { name: "材料公开地址", - url: "http://share.wisdompulse.cn/public", + url: "https://share.wisdompulse.cn/public", requestParams: [], responseParams: [] }, @@ -73,7 +73,6 @@ const defaultConfig = { } }; - function loadMergedConfig() { return new Promise((resolve) => { chrome.storage.local.get(["customConfig"], (res) => { @@ -85,15 +84,20 @@ function loadMergedConfig() { res.customConfig.llmTokenExpireHours != null ? res.customConfig.llmTokenExpireHours : merged.llmTokenExpireHours; merged.llmWorkspaceName = res.customConfig.llmWorkspaceName != null ? res.customConfig.llmWorkspaceName : merged.llmWorkspaceName; + for (const key in merged.apiConfig) { const override = res.customConfig.apiConfig?.[key]; if (override) { merged.apiConfig[key].url = override.url || merged.apiConfig[key].url; merged.apiConfig[key].requestParams.forEach((param, i) => { - if (override.requestParams?.[i]?.field) param.field = override.requestParams[i].field; + if (override.requestParams?.[i]?.field) { + param.field = override.requestParams[i].field; + } }); merged.apiConfig[key].responseParams.forEach((param, i) => { - if (override.responseParams?.[i]?.field) param.field = override.responseParams[i].field; + if (override.responseParams?.[i]?.field) { + param.field = override.responseParams[i].field; + } }); } } @@ -101,5 +105,4 @@ function loadMergedConfig() { resolve(merged); }); }); -} - +} \ No newline at end of file diff --git a/scripts/login.js b/scripts/login.js index eff6aad..60e4969 100644 --- a/scripts/login.js +++ b/scripts/login.js @@ -1,4 +1,3 @@ - document.addEventListener("DOMContentLoaded", async function () { const config = await loadMergedConfig(); const loginConf = config.apiConfig.login || {}; @@ -7,21 +6,25 @@ document.addEventListener("DOMContentLoaded", async function () { 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"; - document.getElementById("loginBtn").addEventListener("click", doLogin); + const loginBtn = document.getElementById("loginBtn"); + const errorBox = document.getElementById("errorBox"); + + loginBtn.addEventListener("click", doLogin); document.addEventListener("keydown", (e) => { - if (e.key === "Enter") doLogin(); + if (e.key === "Enter") { + doLogin(); + } }); - function doLogin() { + async function doLogin() { const username = document.getElementById("username").value.trim(); const password = document.getElementById("password").value.trim(); - const errorBox = document.getElementById("errorBox"); + errorBox.textContent = ""; if (!username || !password) { @@ -33,30 +36,46 @@ document.addEventListener("DOMContentLoaded", async function () { requestBody[reqUsernameField] = username; requestBody[reqPasswordField] = password; - fetch(loginUrl, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(requestBody) - }) - .then(res => res.json().then(data => ({ status: res.status, body: data }))) - .then(({ status, body }) => { - const token = body[resTokenField]; - const returnedUser = body[resUsernameField] || username; + loginBtn.disabled = true; + loginBtn.textContent = "登录中..."; - if (status === 200 && token) { - chrome.storage.local.set({ - token: token, - username: returnedUser, - loginAt: Date.now() - }, () => { - window.location.href = chrome.runtime.getURL("pages/popup.html"); - }); - } else { - errorBox.textContent = body.message || "登录失败"; - } - }) - .catch(() => { - errorBox.textContent = "网络错误,请稍后重试"; + try { + const res = await fetch(loginUrl, { + method: "POST", + credentials: "include", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(requestBody) }); + + const rawText = await res.text(); + let body = {}; + try { + body = rawText ? JSON.parse(rawText) : {}; + } catch (e) { + body = {}; + } + + const token = body[resTokenField]; + const returnedUser = body[resUsernameField] || username; + + if (res.status === 200 && token) { + chrome.storage.local.set({ + token: token, + username: returnedUser, + loginAt: Date.now() + }, () => { + window.location.href = chrome.runtime.getURL("pages/popup.html"); + }); + return; + } + + errorBox.textContent = body.message || body.msg || "登录失败"; + } catch (e) { + console.error("login error:", e); + errorBox.textContent = "网络错误,请稍后重试"; + } finally { + loginBtn.disabled = false; + loginBtn.textContent = "登录"; + } } -}); +}); \ No newline at end of file