a
This commit is contained in:
parent
2ee6e7a009
commit
7a64ff44c2
|
|
@ -61,8 +61,33 @@ const withAuthHeaders = (headers = {}) => {
|
||||||
return { ...headers, Authorization: `Bearer ${token}` };
|
return { ...headers, Authorization: `Bearer ${token}` };
|
||||||
};
|
};
|
||||||
|
|
||||||
const withAccountHeaders = (headers = {}) => {
|
// 全局变量,用于存储当前的 accountId(从 Redux store 同步)
|
||||||
const aid = getCurrentAccountId();
|
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) });
|
return withAuthHeaders({ ...headers, 'X-Account-Id': String(aid) });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { createSlice } from '@reduxjs/toolkit'
|
import { createSlice } from '@reduxjs/toolkit'
|
||||||
|
import { setCurrentAccountIdFromStore } from '../services/api'
|
||||||
|
|
||||||
const VIEWING_USER_ID_KEY = 'ats_viewing_user_id'
|
const VIEWING_USER_ID_KEY = 'ats_viewing_user_id'
|
||||||
const ACCOUNT_ID_STORAGE_KEY = 'ats_account_id'
|
const ACCOUNT_ID_STORAGE_KEY = 'ats_account_id'
|
||||||
|
|
@ -27,10 +28,14 @@ const getInitialAccountId = () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const initialAccountId = getInitialAccountId()
|
||||||
|
// 初始化时同步到 api.js
|
||||||
|
setCurrentAccountIdFromStore(initialAccountId)
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
currentUser: null, // 当前登录用户
|
currentUser: null, // 当前登录用户
|
||||||
viewingUserId: getInitialViewingUserId(), // 管理员查看的用户ID
|
viewingUserId: getInitialViewingUserId(), // 管理员查看的用户ID
|
||||||
accountId: getInitialAccountId(), // 当前选中的账号ID
|
accountId: initialAccountId, // 当前选中的账号ID
|
||||||
accounts: [], // 当前用户的账号列表
|
accounts: [], // 当前用户的账号列表
|
||||||
users: [], // 用户列表(管理员可见)
|
users: [], // 用户列表(管理员可见)
|
||||||
}
|
}
|
||||||
|
|
@ -62,6 +67,8 @@ const appSlice = createSlice({
|
||||||
setAccountId: (state, action) => {
|
setAccountId: (state, action) => {
|
||||||
const accountId = action.payload
|
const accountId = action.payload
|
||||||
state.accountId = accountId
|
state.accountId = accountId
|
||||||
|
// 同步到 api.js 的全局变量,确保 API 请求使用最新的 accountId
|
||||||
|
setCurrentAccountIdFromStore(accountId)
|
||||||
if (accountId) {
|
if (accountId) {
|
||||||
try {
|
try {
|
||||||
localStorage.setItem(ACCOUNT_ID_STORAGE_KEY, String(accountId))
|
localStorage.setItem(ACCOUNT_ID_STORAGE_KEY, String(accountId))
|
||||||
|
|
@ -110,6 +117,8 @@ const appSlice = createSlice({
|
||||||
const nextAccountId = parseInt(String(firstAccount.id || ''), 10)
|
const nextAccountId = parseInt(String(firstAccount.id || ''), 10)
|
||||||
if (Number.isFinite(nextAccountId) && nextAccountId > 0) {
|
if (Number.isFinite(nextAccountId) && nextAccountId > 0) {
|
||||||
state.accountId = nextAccountId
|
state.accountId = nextAccountId
|
||||||
|
// 同步到 api.js 的全局变量
|
||||||
|
setCurrentAccountIdFromStore(nextAccountId)
|
||||||
try {
|
try {
|
||||||
localStorage.setItem(ACCOUNT_ID_STORAGE_KEY, String(nextAccountId))
|
localStorage.setItem(ACCOUNT_ID_STORAGE_KEY, String(nextAccountId))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
@ -125,6 +134,7 @@ const appSlice = createSlice({
|
||||||
} else {
|
} else {
|
||||||
// 如果没有账号,清空 accountId
|
// 如果没有账号,清空 accountId
|
||||||
state.accountId = null
|
state.accountId = null
|
||||||
|
setCurrentAccountIdFromStore(null)
|
||||||
try {
|
try {
|
||||||
localStorage.removeItem(ACCOUNT_ID_STORAGE_KEY)
|
localStorage.removeItem(ACCOUNT_ID_STORAGE_KEY)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user