From 7a64ff44c2a92ee123eb8c73e36c402bd8b4a09e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=87=E8=96=87=E5=AE=89?= Date: Fri, 23 Jan 2026 19:46:43 +0800 Subject: [PATCH] a --- frontend/src/services/api.js | 29 +++++++++++++++++++++++++++-- frontend/src/store/appSlice.js | 12 +++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/frontend/src/services/api.js b/frontend/src/services/api.js index 33be5ed..cc49cb8 100644 --- a/frontend/src/services/api.js +++ b/frontend/src/services/api.js @@ -61,8 +61,33 @@ const withAuthHeaders = (headers = {}) => { return { ...headers, Authorization: `Bearer ${token}` }; }; -const withAccountHeaders = (headers = {}) => { - const aid = getCurrentAccountId(); +// 全局变量,用于存储当前的 accountId(从 Redux store 同步) +let currentAccountIdFromStore = null; + +// 设置当前 accountId(由 Redux store 调用) +export const setCurrentAccountIdFromStore = (accountId) => { + currentAccountIdFromStore = accountId; + // 同时更新 localStorage 保持同步 + if (accountId) { + setCurrentAccountId(accountId); + } +}; + +const withAccountHeaders = (headers = {}, accountIdOverride = null) => { + // 优先使用传入的参数,其次使用 Redux store 的值,最后从 localStorage 读取 + let aid; + if (accountIdOverride !== null) { + aid = accountIdOverride; + } else if (currentAccountIdFromStore !== null) { + aid = currentAccountIdFromStore; + } else { + // 从 localStorage 读取(兜底) + aid = getCurrentAccountId(); + // 如果从 localStorage 读取到了值,同步到全局变量(避免下次再读 localStorage) + if (aid) { + currentAccountIdFromStore = aid; + } + } return withAuthHeaders({ ...headers, 'X-Account-Id': String(aid) }); }; diff --git a/frontend/src/store/appSlice.js b/frontend/src/store/appSlice.js index c45b968..0ccb1f5 100644 --- a/frontend/src/store/appSlice.js +++ b/frontend/src/store/appSlice.js @@ -1,4 +1,5 @@ import { createSlice } from '@reduxjs/toolkit' +import { setCurrentAccountIdFromStore } from '../services/api' const VIEWING_USER_ID_KEY = 'ats_viewing_user_id' const ACCOUNT_ID_STORAGE_KEY = 'ats_account_id' @@ -27,10 +28,14 @@ const getInitialAccountId = () => { } } +const initialAccountId = getInitialAccountId() +// 初始化时同步到 api.js +setCurrentAccountIdFromStore(initialAccountId) + const initialState = { currentUser: null, // 当前登录用户 viewingUserId: getInitialViewingUserId(), // 管理员查看的用户ID - accountId: getInitialAccountId(), // 当前选中的账号ID + accountId: initialAccountId, // 当前选中的账号ID accounts: [], // 当前用户的账号列表 users: [], // 用户列表(管理员可见) } @@ -62,6 +67,8 @@ const appSlice = createSlice({ setAccountId: (state, action) => { const accountId = action.payload state.accountId = accountId + // 同步到 api.js 的全局变量,确保 API 请求使用最新的 accountId + setCurrentAccountIdFromStore(accountId) if (accountId) { try { localStorage.setItem(ACCOUNT_ID_STORAGE_KEY, String(accountId)) @@ -110,6 +117,8 @@ const appSlice = createSlice({ const nextAccountId = parseInt(String(firstAccount.id || ''), 10) if (Number.isFinite(nextAccountId) && nextAccountId > 0) { state.accountId = nextAccountId + // 同步到 api.js 的全局变量 + setCurrentAccountIdFromStore(nextAccountId) try { localStorage.setItem(ACCOUNT_ID_STORAGE_KEY, String(nextAccountId)) } catch (e) { @@ -125,6 +134,7 @@ const appSlice = createSlice({ } else { // 如果没有账号,清空 accountId state.accountId = null + setCurrentAccountIdFromStore(null) try { localStorage.removeItem(ACCOUNT_ID_STORAGE_KEY) } catch (e) {