38 lines
881 B
Go
38 lines
881 B
Go
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
|
||
}
|