This commit is contained in:
薇薇安 2026-01-22 22:32:49 +08:00
parent a7e1e6ca0a
commit 76bde06a4f

View File

@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react' import React, { useState, useEffect, useRef } from 'react'
import { Link } from 'react-router-dom' import { Link } from 'react-router-dom'
import { useSelector, useDispatch } from 'react-redux' import { useSelector, useDispatch } from 'react-redux'
import { api } from '../services/api' import { api } from '../services/api'
@ -264,13 +264,15 @@ const ConfigPanel = () => {
} }
} }
const loadCurrentAccountMeta = async () => { const loadCurrentAccountMeta = async (targetAccountId = 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()
const accounts = Array.isArray(list) ? list : [] 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') // 确保status字段正确设置(如果meta存在但status为空,默认为'active')
if (meta) { if (meta) {
meta.status = meta.status || 'active' meta.status = meta.status || 'active'
@ -444,20 +446,33 @@ const ConfigPanel = () => {
} }
} }
// 使用 ref 记录上一次的 accountId,避免初始化时刷新页面
const prevAccountIdRef = useRef(accountId)
useEffect(() => { useEffect(() => {
// 如果 accountId 变化了(不是初始化),刷新页面
if (prevAccountIdRef.current !== null && prevAccountIdRef.current !== accountId) {
// accountId 变化时,刷新页面以确保所有状态都正确更新
window.location.reload()
return
}
// 初始化时,更新 ref 并加载数据
prevAccountIdRef.current = accountId
loadConfigMeta() loadConfigMeta()
loadConfigs() loadConfigs()
checkFeasibility() checkFeasibility()
loadAccountTradingStatus() loadAccountTradingStatus()
loadCurrentAccountMeta() loadCurrentAccountMeta(accountId)
const timer = setInterval(() => { const timer = setInterval(() => {
// 定时器中使用最新的 accountId
loadAccountTradingStatus() loadAccountTradingStatus()
loadCurrentAccountMeta() loadCurrentAccountMeta(accountId)
}, 3000) }, 3000)
return () => clearInterval(timer) return () => clearInterval(timer)
}, []) }, [accountId]) // 当 accountId 变化时,如果是切换账号则刷新页面
// 当accountId变化时,重新加载相关数据(避免重复调用,已在onChanged和定时器中处理) // 当accountId变化时,重新加载相关数据(避免重复调用,已在onChanged和定时器中处理)
@ -478,16 +493,7 @@ const ConfigPanel = () => {
} }
} }
// 切换账号时,刷新页面数据(先加载account meta,再加载其他) // 注意:accountId 变化时的刷新逻辑已在上面的 useEffect 中处理
useEffect(() => {
setMessage('')
setLoading(true)
loadCurrentAccountMeta().then(() => {
loadConfigs()
checkFeasibility()
loadAccountTradingStatus()
})
}, [accountId]) // 当 accountId 变化时重新加载
const checkFeasibility = async () => { const checkFeasibility = async () => {
setCheckingFeasibility(true) setCheckingFeasibility(true)
@ -884,11 +890,6 @@ const ConfigPanel = () => {
<div className="system-section"> <div className="system-section">
<div className="system-header"> <div className="system-header">
<h3>我的交易进程(当前账号 #{accountId})</h3> <h3>我的交易进程(当前账号 #{accountId})</h3>
<button type="button" className="system-btn" onClick={() => {
console.log(currentAccountMeta)
}} title="打印当前账号信息">
打印当前账号信息
</button>
<div className="system-status"> <div className="system-status">
<span className={`system-status-badge ${accountTradingStatus?.running ? 'running' : 'stopped'}`}> <span className={`system-status-badge ${accountTradingStatus?.running ? 'running' : 'stopped'}`}>
{accountTradingStatus?.running ? '运行中' : '未运行/未知'} {accountTradingStatus?.running ? '运行中' : '未运行/未知'}