diff --git a/frontend/src/components/ConfigPanel.jsx b/frontend/src/components/ConfigPanel.jsx index b4bf397..09e884f 100644 --- a/frontend/src/components/ConfigPanel.jsx +++ b/frontend/src/components/ConfigPanel.jsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react' +import React, { useState, useEffect, useRef } from 'react' import { Link } from 'react-router-dom' import { useSelector, useDispatch } from 'react-redux' import { api } from '../services/api' @@ -264,13 +264,15 @@ const ConfigPanel = () => { } } - const loadCurrentAccountMeta = async () => { + const loadCurrentAccountMeta = async (targetAccountId = null) => { try { + // 使用传入的 accountId 或当前的 accountId(确保使用最新的值) + const targetId = targetAccountId !== null ? targetAccountId : accountId // 统一使用getAccounts获取所有账号(管理员会返回所有账号,普通用户返回自己的) // 这样可以确保获取到完整的status等信息 const list = await api.getAccounts() const accounts = Array.isArray(list) ? list : [] - const meta = accounts.find((a) => parseInt(String(a?.id || '0'), 10) === parseInt(String(accountId || '0'), 10)) + const meta = accounts.find((a) => parseInt(String(a?.id || '0'), 10) === parseInt(String(targetId || '0'), 10)) // 确保status字段正确设置(如果meta存在但status为空,默认为'active') if (meta) { meta.status = meta.status || 'active' @@ -444,20 +446,33 @@ const ConfigPanel = () => { } } + // 使用 ref 记录上一次的 accountId,避免初始化时刷新页面 + const prevAccountIdRef = useRef(accountId) + useEffect(() => { + // 如果 accountId 变化了(不是初始化),刷新页面 + if (prevAccountIdRef.current !== null && prevAccountIdRef.current !== accountId) { + // accountId 变化时,刷新页面以确保所有状态都正确更新 + window.location.reload() + return + } + + // 初始化时,更新 ref 并加载数据 + prevAccountIdRef.current = accountId loadConfigMeta() loadConfigs() checkFeasibility() loadAccountTradingStatus() - loadCurrentAccountMeta() + loadCurrentAccountMeta(accountId) const timer = setInterval(() => { + // 定时器中使用最新的 accountId loadAccountTradingStatus() - loadCurrentAccountMeta() + loadCurrentAccountMeta(accountId) }, 3000) return () => clearInterval(timer) - }, []) + }, [accountId]) // 当 accountId 变化时,如果是切换账号则刷新页面 // 当accountId变化时,重新加载相关数据(避免重复调用,已在onChanged和定时器中处理) @@ -478,16 +493,7 @@ const ConfigPanel = () => { } } - // 切换账号时,刷新页面数据(先加载account meta,再加载其他) - useEffect(() => { - setMessage('') - setLoading(true) - loadCurrentAccountMeta().then(() => { - loadConfigs() - checkFeasibility() - loadAccountTradingStatus() - }) - }, [accountId]) // 当 accountId 变化时重新加载 + // 注意:accountId 变化时的刷新逻辑已在上面的 useEffect 中处理 const checkFeasibility = async () => { setCheckingFeasibility(true) @@ -884,11 +890,6 @@ const ConfigPanel = () => {

我的交易进程(当前账号 #{accountId})

-
{accountTradingStatus?.running ? '运行中' : '未运行/未知'}