fix(兑换率): 移除硬编码的CNY兑换率并添加固定汇率回退逻辑
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 5m45s

移除之前硬编码的CNY兑换率检查,改为从系统配置中读取固定汇率
当API调用失败时,使用配置的固定汇率作为回退方案
添加对无AccessKey情况的固定汇率支持
This commit is contained in:
shanshanzhong 2026-01-05 18:36:24 -08:00
parent b10d0d22e1
commit 55c778b65b

View File

@ -403,10 +403,10 @@ func (l *PurchaseCheckoutLogic) queryExchangeRate(to string, src int64) (amount
// Convert cents to decimal amount
amount = float64(src) / float64(100)
if l.svcCtx.ExchangeRate != 0 && to == "CNY" {
amount = amount * l.svcCtx.ExchangeRate
return amount, nil
}
// if l.svcCtx.ExchangeRate != 0 && to == "CNY" {
// amount = amount * l.svcCtx.ExchangeRate
// return amount, nil
// }
// Retrieve system currency configuration
currency, err := l.svcCtx.SystemModel.GetCurrencyConfig(l.ctx)
@ -420,12 +420,20 @@ func (l *PurchaseCheckoutLogic) queryExchangeRate(to string, src int64) (amount
CurrencyUnit string
CurrencySymbol string
AccessKey string
FixedRate string
}{}
tool.SystemConfigSliceReflectToStruct(currency, &configs)
l.Infow("queryExchangeRate", logger.Field("to", to), logger.Field("unit", configs.CurrencyUnit), logger.Field("hasAccessKey", strings.TrimSpace(configs.AccessKey) != ""))
if strings.TrimSpace(configs.AccessKey) == "" {
if to == "CNY" && strings.TrimSpace(configs.CurrencyUnit) == "USD" && strings.TrimSpace(configs.FixedRate) != "" {
r := tool.FormatStringToFloat(strings.TrimSpace(configs.FixedRate))
if r > 0 {
l.Infow("exchangeRate.fixed", logger.Field("rate", r))
return amount * r, nil
}
}
l.Infof("[PurchaseCheckout] AccessKey is empty, skip conversion")
return amount, nil
}
@ -433,7 +441,19 @@ func (l *PurchaseCheckoutLogic) queryExchangeRate(to string, src int64) (amount
if strings.TrimSpace(configs.CurrencyUnit) != strings.TrimSpace(to) {
result, err := exchangeRate.GetExchangeRete(configs.CurrencyUnit, to, strings.TrimSpace(configs.AccessKey), 1)
if err != nil {
return 0, err
if to == "CNY" && strings.TrimSpace(configs.CurrencyUnit) == "USD" && strings.TrimSpace(configs.FixedRate) != "" {
r := tool.FormatStringToFloat(strings.TrimSpace(configs.FixedRate))
if r > 0 {
l.Infow("exchangeRate.fixed.fallback", logger.Field("rate", r), logger.Field("error", err.Error()))
return amount * r, nil
}
}
// fallback: try without access key
result2, err2 := exchangeRate.GetExchangeRete(configs.CurrencyUnit, to, "", 1)
if err2 != nil {
return 0, err
}
result = result2
}
l.Infow("exchangeRate", logger.Field("from", configs.CurrencyUnit), logger.Field("to", to), logger.Field("rate", result))
amount = result * amount