From c23db4aba0d2bed359f92a5b73fb9d8cc99d70b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=87=E8=96=87=E5=AE=89?= Date: Tue, 3 Feb 2026 13:12:36 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=B7=E6=B1=82=E8=B4=A6=E5=8F=B71=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/components/AccountSelector.jsx | 2 +- frontend/src/components/ConfigPanel.jsx | 22 ++++++++++----------- frontend/src/services/api.js | 20 +++++++++++++------ frontend/src/store/appSlice.js | 6 +++--- 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/frontend/src/components/AccountSelector.jsx b/frontend/src/components/AccountSelector.jsx index 58998c5..56deca0 100644 --- a/frontend/src/components/AccountSelector.jsx +++ b/frontend/src/components/AccountSelector.jsx @@ -51,7 +51,7 @@ const AccountSelector = ({ onChanged }) => { }, [accountId, onChanged]) const list = Array.isArray(accounts) ? accounts : [] - const options = (list.length ? list : [{ id: 1, name: 'default' }]).reduce((acc, cur) => { + const options = list.reduce((acc, cur) => { if (!cur || !cur.id) return acc if (acc.some((x) => x.id === cur.id)) return acc acc.push(cur) diff --git a/frontend/src/components/ConfigPanel.jsx b/frontend/src/components/ConfigPanel.jsx index 7589770..a25ea34 100644 --- a/frontend/src/components/ConfigPanel.jsx +++ b/frontend/src/components/ConfigPanel.jsx @@ -267,6 +267,7 @@ const ConfigPanel = () => { } const loadAccountTradingStatus = async () => { + if (!accountId) return try { const res = await api.getAccountTradingStatus(accountId) setAccountTradingStatus(res) @@ -278,9 +279,10 @@ const ConfigPanel = () => { } const loadCurrentAccountMeta = async (targetAccountId = null) => { + // 使用传入的 accountId 或当前的 accountId(确保使用最新的值) + const targetId = targetAccountId !== null ? targetAccountId : accountId + if (!targetId) return null try { - // 使用传入的 accountId 或当前的 accountId(确保使用最新的值) - const targetId = targetAccountId !== null ? targetAccountId : accountId // 统一使用getAccounts获取所有账号(管理员会返回所有账号,普通用户返回自己的) // 这样可以确保获取到完整的status等信息 const list = await api.getAccounts() @@ -463,13 +465,9 @@ const ConfigPanel = () => { const prevAccountIdRef = useRef(accountId) useEffect(() => { - // 如果 accountId 变化了(不是初始化),刷新页面 - // if (prevAccountIdRef.current !== null && prevAccountIdRef.current !== accountId) { - // // accountId 变化时,刷新页面以确保所有状态都正确更新 - // window.location.reload() - // return - // } - + // 如果没有选中账号,不加载数据 + if (!accountId) return + // 初始化时,更新 ref 并加载数据 prevAccountIdRef.current = accountId loadConfigMeta() @@ -480,8 +478,10 @@ const ConfigPanel = () => { const timer = setInterval(() => { // 定时器中使用最新的 accountId - loadAccountTradingStatus() - loadCurrentAccountMeta(accountId) + if (accountId) { + loadAccountTradingStatus() + loadCurrentAccountMeta(accountId) + } }, 3000) return () => clearInterval(timer) diff --git a/frontend/src/services/api.js b/frontend/src/services/api.js index 7d3ba49..4ba29e2 100644 --- a/frontend/src/services/api.js +++ b/frontend/src/services/api.js @@ -31,17 +31,21 @@ const ACCOUNT_ID_STORAGE_KEY = 'ats_account_id'; export const getCurrentAccountId = () => { try { const v = localStorage.getItem(ACCOUNT_ID_STORAGE_KEY); - const n = parseInt(v || '1', 10); - return Number.isFinite(n) && n > 0 ? n : 1; + const n = parseInt(v, 10); + return Number.isFinite(n) && n > 0 ? n : null; } catch (e) { - return 1; + return null; } }; export const setCurrentAccountId = (accountId) => { try { - const n = parseInt(String(accountId || '1'), 10); - localStorage.setItem(ACCOUNT_ID_STORAGE_KEY, String(Number.isFinite(n) && n > 0 ? n : 1)); + const n = parseInt(String(accountId), 10); + if (Number.isFinite(n) && n > 0) { + localStorage.setItem(ACCOUNT_ID_STORAGE_KEY, String(n)); + } else { + localStorage.removeItem(ACCOUNT_ID_STORAGE_KEY); + } } catch (e) { // ignore } @@ -88,7 +92,11 @@ const withAccountHeaders = (headers = {}, accountIdOverride = null) => { currentAccountIdFromStore = aid; } } - return withAuthHeaders({ ...headers, 'X-Account-Id': String(aid) }); + const finalHeaders = { ...headers }; + if (aid) { + finalHeaders['X-Account-Id'] = String(aid); + } + return withAuthHeaders(finalHeaders); }; // 构建API URL的辅助函数,避免双斜杠和格式问题 diff --git a/frontend/src/store/appSlice.js b/frontend/src/store/appSlice.js index 0ccb1f5..ef16d17 100644 --- a/frontend/src/store/appSlice.js +++ b/frontend/src/store/appSlice.js @@ -21,10 +21,10 @@ const getInitialViewingUserId = () => { const getInitialAccountId = () => { try { const v = localStorage.getItem(ACCOUNT_ID_STORAGE_KEY) - const n = parseInt(v || '1', 10) - return Number.isFinite(n) && n > 0 ? n : 1 + const n = parseInt(v, 10) + return Number.isFinite(n) && n > 0 ? n : null } catch (e) { - return 1 + return null } }