refactor: enhance price retrieval logic in getStandardPrice function

- Improved the price retrieval logic to handle multiple data types (float64, int, int64, json.Number, string) for better flexibility.
- Updated comments to clarify the source of pricing configurations and ensure consistency with frontend settings.
- Adjusted default pricing values for the "match" product type to align with new business requirements.
This commit is contained in:
Alex-larget
2026-04-02 18:31:35 +08:00
parent 0756d9ff0c
commit 055f0a374e

View File

@@ -1213,9 +1213,13 @@ func getStandardPrice(db *gorm.DB, productType, productID string) (float64, erro
switch productType {
case "fullbook", "vip", "match":
// 从 system_config 读取
// 价格来源要与对应前端配置端一致,防止小程序显示 1 元但后端用错价格。
configKey := "chapter_config"
if productType == "vip" {
configKey = "vip_config"
} else if productType == "match" {
// 找伙伴匹配次数价格应使用 match_config
configKey = "match_config"
}
var row model.SystemConfig
if err := db.Where("config_key = ?", configKey).First(&row).Error; err == nil {
@@ -1226,13 +1230,37 @@ func getStandardPrice(db *gorm.DB, productType, productID string) (float64, erro
"vip": "price",
"match": "matchPrice",
}
if v, ok := cfg[fieldMap[productType]].(float64); ok && v > 0 {
return v, nil
raw := cfg[fieldMap[productType]]
if raw != nil {
switch v := raw.(type) {
case float64:
if v > 0 {
return v, nil
}
case int:
if v > 0 {
return float64(v), nil
}
case int64:
if v > 0 {
return float64(v), nil
}
case json.Number:
f, err := v.Float64()
if err == nil && f > 0 {
return f, nil
}
case string:
f, err := strconv.ParseFloat(strings.TrimSpace(v), 64)
if err == nil && f > 0 {
return f, nil
}
}
}
}
}
// 兜底默认值
defaults := map[string]float64{"fullbook": 9.9, "vip": 1980, "match": 68}
defaults := map[string]float64{"fullbook": 9.9, "vip": 1980, "match": 1}
if p, ok := defaults[productType]; ok {
return p, nil
}