Files
observer/server/common/plans.go
T

38 lines
881 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package common
import (
"context"
"github.com/gogf/gf/v2/frame/g"
)
// Plan 套餐定价(config.yml plans 节点,静态配置;展示名由 days 派生"N天"
type Plan struct {
Id string `mapstructure:"id"`
Days int `mapstructure:"days"`
PriceCents int64 `mapstructure:"price_cents"`
}
// ListPlans 全部套餐(按配置声明顺序)
func ListPlans(ctx context.Context) ([]*Plan, error) {
var plans []*Plan
if err := g.Cfg().MustGet(ctx, "plans").Scan(&plans); err != nil {
return nil, err
}
return plans, nil
}
// GetPlan 按套餐 ID 查找,不存在返回 (nil, nil),调用方用 CodePlanNotConfigured 包装
func GetPlan(ctx context.Context, id string) (*Plan, error) {
plans, err := ListPlans(ctx)
if err != nil {
return nil, err
}
for _, p := range plans {
if p.Id == id {
return p, nil
}
}
return nil, nil
}