This commit is contained in:
薇薇安 2026-01-26 21:00:43 +08:00
parent 1eb5c618eb
commit 3057ce0e8b
3 changed files with 36 additions and 28 deletions

View File

@ -732,9 +732,11 @@ class ConfigManager:
except Exception:
value = self.get(key, default)
# ⚠️ 关键修复:百分比配置值格式转换
# ⚠️ 临时兼容性处理:百分比配置值格式转换
# 如果配置值是百分比形式(>1),转换为比例形式(除以100)
# 兼容数据库中存储的百分比形式(如30表示30%)和比例形式(如0.30表示30%)
# 兼容数据库中可能存在的旧数据(百分比形式,如30表示30%)
# 数据迁移完成后,可以移除此逻辑
# 统一格式:数据库、前端、后端都使用比例形式(0.30表示30%)
if isinstance(value, (int, float)) and value is not None:
# 需要转换的百分比配置项
percent_keys = [
@ -752,10 +754,11 @@ class ConfigManager:
]
if key in percent_keys:
# 如果值>1,认为是百分比形式,转换为比例形式
# 如果值>1,认为是百分比形式(旧数据),转换为比例形式
# 数据迁移完成后,所有值都应该<=1,此逻辑可以移除
if value > 1:
value = value / 100.0
logger.debug(f"配置值格式转换: {key} = {value*100:.2f}% (从百分比形式转换为比例形式)")
logger.warning(f"配置值格式转换(临时兼容): {key} = {value*100:.2f}% (从百分比形式转换为比例形式,建议执行数据迁移)")
return value

View File

@ -1503,15 +1503,14 @@ const ConfigItem = ({ label, config, onUpdate, disabled }) => {
if (isNaN(numVal)) {
return ''
}
// 两类:
// 1) 常规比例型(如 STOP_LOSS_PERCENT=0.08):展示为 8(%)
// 2) pct-like(如 LIMIT_ORDER_OFFSET_PCT=0.5 表示0.5%):展示为 0.5(%)
// ⚠️ 简化:直接显示数据库中的值(0.30),用户直接输入小数(0.30)
// 如果值>1,说明可能是旧数据(百分比形式),需要转换;否则直接显示
if (isPctLike) {
const pctNum = numVal <= 0.05 ? numVal * 100 : numVal
return formatPercent(pctNum)
// pct-like:值本身就是"百分比数值"(<=1表示<=1%),直接显示
return formatPercent(numVal <= 0.05 ? numVal * 100 : numVal)
}
const percent = numVal <= 1 ? numVal * 100 : numVal
return formatPercent(percent)
// 常规比例型:直接显示数据库中的值(0.30),如果>1则可能是旧数据
return formatPercent(numVal <= 1 ? numVal : numVal / 100)
}
return val === null || val === undefined ? '' : val
}
@ -1555,7 +1554,7 @@ const ConfigItem = ({ label, config, onUpdate, disabled }) => {
} else if (localValue === '.' || localValue === '-.') {
// "." 或 "-." 视为无效,恢复原值
const restoreValue = isPercentKey
? (typeof value === 'number' ? formatPercent((value <= 1 ? value * 100 : value)) : value)
? (typeof value === 'number' ? formatPercent(value <= 1 ? value : value / 100) : value)
: value
setLocalValue(restoreValue)
return
@ -1569,7 +1568,7 @@ const ConfigItem = ({ label, config, onUpdate, disabled }) => {
if (isNaN(numValue)) {
// 如果输入无效,恢复原值
const restoreValue = isPercentKey
? (typeof value === 'number' ? formatPercent((value <= 1 ? value * 100 : value)) : value)
? (typeof value === 'number' ? formatPercent(value <= 1 ? value : value / 100) : value)
: value
setLocalValue(restoreValue)
return
@ -1586,11 +1585,12 @@ const ConfigItem = ({ label, config, onUpdate, disabled }) => {
}
} else {
// 常规比例型:前端按“百分比”输入(0~100),存储为 0~1
if (processedValue < 0 || processedValue > 100) {
if (processedValue < 0 || processedValue > 1) {
setLocalValue(getInitialDisplayValue(value))
return
}
processedValue = processedValue / 100
// ⚠️ 简化:直接使用输入的值(0.30),不做转换
// processedValue = processedValue / 100 // 已移除转换逻辑
}
}
} else if (config.type === 'boolean') {
@ -1742,11 +1742,10 @@ const ConfigItem = ({ label, config, onUpdate, disabled }) => {
// return
// }
// 如果是百分比配置,限制输入范围(0-100)
// ⚠️ 简化:如果是百分比配置,限制输入范围(0-1),用户直接输入小数
if (isPercentKey) {
const numValue = parseFloat(newValue)
const maxAllowed = isPctLike ? 1 : 100
if (newValue !== '' && !isNaN(numValue) && (numValue < 0 || numValue > maxAllowed)) {
if (newValue !== '' && !isNaN(numValue) && (numValue < 0 || numValue > 1)) {
// 超出范围,不更新
return
}

View File

@ -32,12 +32,14 @@ const ConfigItem = ({ label, config, onUpdate, disabled }) => {
if (isNaN(numVal)) {
return ''
}
// ⚠️ 简化:直接显示数据库中的值(0.30),用户直接输入小数(0.30)
// 如果值>1,说明可能是旧数据(百分比形式),需要转换;否则直接显示
if (isPctLike) {
const pctNum = numVal <= 0.05 ? numVal * 100 : numVal
return formatPercent(pctNum)
// pct-like:值本身就是"百分比数值"(<=1表示<=1%),直接显示
return formatPercent(numVal <= 0.05 ? numVal * 100 : numVal)
}
const percent = numVal <= 1 ? numVal * 100 : numVal
return formatPercent(percent)
// 常规比例型:直接显示数据库中的值(0.30),如果>1则可能是旧数据
return formatPercent(numVal <= 1 ? numVal : numVal / 100)
}
return val === null || val === undefined ? '' : val
}
@ -68,11 +70,15 @@ const ConfigItem = ({ label, config, onUpdate, disabled }) => {
setIsEditing(false)
return
}
if (isPctLike) {
finalValue = numVal <= 1 ? numVal / 100 : numVal / 100
} else {
finalValue = numVal <= 100 ? numVal / 100 : numVal / 100
// ⚠️ 简化:用户直接输入小数(0.30),直接存储,不做转换
// 验证范围:0-1之间(比例形式)
if (numVal < 0 || numVal > 1) {
setLocalValue(getInitialDisplayValue(config.value))
setIsEditing(false)
return
}
// 直接使用输入的值(0.30),不做转换
finalValue = numVal
} else {
finalValue = parseFloat(localValue)
if (isNaN(finalValue)) {
@ -121,8 +127,8 @@ const ConfigItem = ({ label, config, onUpdate, disabled }) => {
if (config.type === 'number') {
if (isPercentKey) {
const numValue = parseFloat(newValue)
const maxAllowed = isPctLike ? 1 : 100
if (newValue !== '' && !isNaN(numValue) && (numValue < 0 || numValue > maxAllowed)) {
// ⚠️ 简化:验证范围改为0-1(比例形式),用户直接输入小数
if (newValue !== '' && !isNaN(numValue) && (numValue < 0 || numValue > 1)) {
return
}
}