bindbox-game/internal/service/title/effect_validate.go
邹方成 6ee627139c
Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 40s
feat: 新增支付测试小程序与微信支付集成
feat(pay): 添加支付API基础结构
feat(miniapp): 创建支付测试小程序页面与配置
feat(wechatpay): 配置微信支付参数与证书
fix(guild): 修复成员列表查询条件
docs: 更新代码规范文档与需求文档
style: 统一前后端枚举显示与注释格式
refactor(admin): 重构用户奖励发放接口参数处理
test(title): 添加称号效果参数验证测试
2025-11-17 00:42:08 +08:00

90 lines
4.3 KiB
Go

package title
import (
"bytes"
"encoding/json"
"fmt"
)
func (s *service) ValidateEffectParams(effectType int32, raw string) (string, error) {
dec := json.NewDecoder(bytes.NewBufferString(raw))
dec.DisallowUnknownFields()
switch effectType {
case 1:
var p struct {
TemplateID int64 `json:"template_id"`
Frequency struct {
Period string `json:"period"`
Times int `json:"times"`
} `json:"frequency"`
}
if err := dec.Decode(&p); err != nil { return "", err }
if p.TemplateID < 0 { return "", fmt.Errorf("template_id无效") }
if p.Frequency.Times < 1 || p.Frequency.Times > 100 { return "", fmt.Errorf("times范围错误") }
if p.Frequency.Period != "day" && p.Frequency.Period != "week" && p.Frequency.Period != "month" { return "", fmt.Errorf("period无效") }
b, _ := json.Marshal(p); return string(b), nil
case 2:
var p struct {
DiscountType string `json:"discount_type"`
ValueX1000 int32 `json:"value_x1000"`
MaxDiscountX1000 int32 `json:"max_discount_x1000"`
}
if err := dec.Decode(&p); err != nil { return "", err }
if p.DiscountType != "percentage" && p.DiscountType != "fixed" { return "", fmt.Errorf("discount_type无效") }
if p.ValueX1000 < 0 || p.MaxDiscountX1000 < 0 { return "", fmt.Errorf("数值必须>=0") }
b, _ := json.Marshal(p); return string(b), nil
case 3:
var p struct {
MultiplierX1000 int32 `json:"multiplier_x1000"`
DailyCapPoints int32 `json:"daily_cap_points"`
}
if err := dec.Decode(&p); err != nil { return "", err }
if p.MultiplierX1000 < 0 || p.DailyCapPoints < 0 { return "", fmt.Errorf("数值必须>=0") }
b, _ := json.Marshal(p); return string(b), nil
case 4:
var p struct {
TemplateID int64 `json:"template_id"`
Frequency struct {
Period string `json:"period"`
Times int `json:"times"`
} `json:"frequency"`
}
if err := dec.Decode(&p); err != nil { return "", err }
if p.TemplateID < 0 { return "", fmt.Errorf("template_id无效") }
if p.Frequency.Times < 1 || p.Frequency.Times > 100 { return "", fmt.Errorf("times范围错误") }
if p.Frequency.Period != "week" && p.Frequency.Period != "month" { return "", fmt.Errorf("period无效") }
b, _ := json.Marshal(p); return string(b), nil
case 5:
var p struct {
TargetPrizeIDs []int64 `json:"target_prize_ids"`
BoostX1000 int32 `json:"boost_x1000"`
CapX1000 *int32 `json:"cap_x1000"`
}
if err := dec.Decode(&p); err != nil { return "", err }
if p.BoostX1000 < 0 || p.BoostX1000 > 100000 { return "", fmt.Errorf("boost_x1000范围错误") }
if p.CapX1000 != nil && *p.CapX1000 < 0 { return "", fmt.Errorf("cap_x1000必须>=0") }
if len(p.TargetPrizeIDs) > 200 { return "", fmt.Errorf("target_prize_ids数量过多") }
m := make(map[int64]struct{})
out := make([]int64, 0, len(p.TargetPrizeIDs))
for _, id := range p.TargetPrizeIDs { if _, ok := m[id]; !ok { m[id] = struct{}{}; out = append(out, id) } }
p.TargetPrizeIDs = out
b, _ := json.Marshal(p); return string(b), nil
case 6:
var p struct {
TargetPrizeIDs []int64 `json:"target_prize_ids"`
ChanceX1000 int32 `json:"chance_x1000"`
PeriodCapTimes *int32 `json:"period_cap_times"`
}
if err := dec.Decode(&p); err != nil { return "", err }
if p.ChanceX1000 < 0 || p.ChanceX1000 > 100000 { return "", fmt.Errorf("chance_x1000范围错误") }
if p.PeriodCapTimes != nil && *p.PeriodCapTimes < 0 { return "", fmt.Errorf("period_cap_times必须>=0") }
if len(p.TargetPrizeIDs) > 200 { return "", fmt.Errorf("target_prize_ids数量过多") }
m := make(map[int64]struct{})
out := make([]int64, 0, len(p.TargetPrizeIDs))
for _, id := range p.TargetPrizeIDs { if _, ok := m[id]; !ok { m[id] = struct{}{}; out = append(out, id) } }
p.TargetPrizeIDs = out
b, _ := json.Marshal(p); return string(b), nil
default:
return raw, nil
}
}