请求账号1的问题

This commit is contained in:
薇薇安 2026-02-03 13:12:36 +08:00
parent 377ae3b966
commit c23db4aba0
4 changed files with 29 additions and 21 deletions

View File

@ -51,7 +51,7 @@ const AccountSelector = ({ onChanged }) => {
}, [accountId, onChanged]) }, [accountId, onChanged])
const list = Array.isArray(accounts) ? accounts : [] 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 (!cur || !cur.id) return acc
if (acc.some((x) => x.id === cur.id)) return acc if (acc.some((x) => x.id === cur.id)) return acc
acc.push(cur) acc.push(cur)

View File

@ -267,6 +267,7 @@ const ConfigPanel = () => {
} }
const loadAccountTradingStatus = async () => { const loadAccountTradingStatus = async () => {
if (!accountId) return
try { try {
const res = await api.getAccountTradingStatus(accountId) const res = await api.getAccountTradingStatus(accountId)
setAccountTradingStatus(res) setAccountTradingStatus(res)
@ -278,9 +279,10 @@ const ConfigPanel = () => {
} }
const loadCurrentAccountMeta = async (targetAccountId = null) => { const loadCurrentAccountMeta = async (targetAccountId = null) => {
// 使用传入的 accountId 或当前的 accountId(确保使用最新的值)
const targetId = targetAccountId !== null ? targetAccountId : accountId
if (!targetId) return null
try { try {
// 使用传入的 accountId 或当前的 accountId(确保使用最新的值)
const targetId = targetAccountId !== null ? targetAccountId : accountId
// 统一使用getAccounts获取所有账号(管理员会返回所有账号,普通用户返回自己的) // 统一使用getAccounts获取所有账号(管理员会返回所有账号,普通用户返回自己的)
// 这样可以确保获取到完整的status等信息 // 这样可以确保获取到完整的status等信息
const list = await api.getAccounts() const list = await api.getAccounts()
@ -463,13 +465,9 @@ const ConfigPanel = () => {
const prevAccountIdRef = useRef(accountId) const prevAccountIdRef = useRef(accountId)
useEffect(() => { useEffect(() => {
// 如果 accountId 变化了(不是初始化),刷新页面 // 如果没有选中账号,不加载数据
// if (prevAccountIdRef.current !== null && prevAccountIdRef.current !== accountId) { if (!accountId) return
// // accountId 变化时,刷新页面以确保所有状态都正确更新
// window.location.reload()
// return
// }
// 初始化时,更新 ref 并加载数据 // 初始化时,更新 ref 并加载数据
prevAccountIdRef.current = accountId prevAccountIdRef.current = accountId
loadConfigMeta() loadConfigMeta()
@ -480,8 +478,10 @@ const ConfigPanel = () => {
const timer = setInterval(() => { const timer = setInterval(() => {
// 定时器中使用最新的 accountId // 定时器中使用最新的 accountId
loadAccountTradingStatus() if (accountId) {
loadCurrentAccountMeta(accountId) loadAccountTradingStatus()
loadCurrentAccountMeta(accountId)
}
}, 3000) }, 3000)
return () => clearInterval(timer) return () => clearInterval(timer)

View File

@ -31,17 +31,21 @@ const ACCOUNT_ID_STORAGE_KEY = 'ats_account_id';
export const getCurrentAccountId = () => { export const getCurrentAccountId = () => {
try { try {
const v = localStorage.getItem(ACCOUNT_ID_STORAGE_KEY); const v = localStorage.getItem(ACCOUNT_ID_STORAGE_KEY);
const n = parseInt(v || '1', 10); const n = parseInt(v, 10);
return Number.isFinite(n) && n > 0 ? n : 1; return Number.isFinite(n) && n > 0 ? n : null;
} catch (e) { } catch (e) {
return 1; return null;
} }
}; };
export const setCurrentAccountId = (accountId) => { export const setCurrentAccountId = (accountId) => {
try { try {
const n = parseInt(String(accountId || '1'), 10); const n = parseInt(String(accountId), 10);
localStorage.setItem(ACCOUNT_ID_STORAGE_KEY, String(Number.isFinite(n) && n > 0 ? n : 1)); if (Number.isFinite(n) && n > 0) {
localStorage.setItem(ACCOUNT_ID_STORAGE_KEY, String(n));
} else {
localStorage.removeItem(ACCOUNT_ID_STORAGE_KEY);
}
} catch (e) { } catch (e) {
// ignore // ignore
} }
@ -88,7 +92,11 @@ const withAccountHeaders = (headers = {}, accountIdOverride = null) => {
currentAccountIdFromStore = aid; 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的辅助函数,避免双斜杠和格式问题 // 构建API URL的辅助函数,避免双斜杠和格式问题

View File

@ -21,10 +21,10 @@ const getInitialViewingUserId = () => {
const getInitialAccountId = () => { const getInitialAccountId = () => {
try { try {
const v = localStorage.getItem(ACCOUNT_ID_STORAGE_KEY) const v = localStorage.getItem(ACCOUNT_ID_STORAGE_KEY)
const n = parseInt(v || '1', 10) const n = parseInt(v, 10)
return Number.isFinite(n) && n > 0 ? n : 1 return Number.isFinite(n) && n > 0 ? n : null
} catch (e) { } catch (e) {
return 1 return null
} }
} }