// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn . package plans import ( "encoding/json" "github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeUser/internal/remotelogs" "github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeUser/internal/web/actions/default/ns/nsutils" "github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/types" timeutil "github.com/iwind/TeaGo/utils/time" ) type IndexAction struct { actionutils.ParentAction } func (this *IndexAction) Init() { this.Nav("", "", "") } func (this *IndexAction) RunGet(params struct{}) { // 当前我的套餐 userPlanResp, err := this.RPC().NSUserPlanRPC().FindNSUserPlan(this.UserContext(), &pb.FindNSUserPlanRequest{}) if err != nil { this.ErrorPage(err) return } var userPlan = userPlanResp.NsUserPlan this.Data["userPlan"] = nil if userPlan != nil && userPlan.NsPlan != nil { if len(userPlan.DayTo) < 8 { userPlan.DayTo = timeutil.Format("Ymd") } this.Data["userPlan"] = maps.Map{ "dayTo": userPlan.DayTo[:4] + "-" + userPlan.DayTo[4:6] + "-" + userPlan.DayTo[6:], "isExpired": userPlan.DayTo < timeutil.Format("Ymd"), "plan": maps.Map{ "id": userPlan.NsPlan.Id, "name": userPlan.NsPlan.Name, }, } } var planMaps = []maps.Map{} // 基础套餐 basicPlanConfig, err := nsutils.FindBasicPlan(this.UserContext()) if err != nil { this.ErrorPage(err) return } planMaps = append(planMaps, maps.Map{ "id": 0, "name": "注册用户", "monthlyPrice": 0, "yearlyPrice": 0, "config": basicPlanConfig, }) // 所有套餐 plansResp, err := this.RPC().NSPlanRPC().FindAllEnabledNSPlans(this.UserContext(), &pb.FindAllEnabledNSPlansRequest{}) if err != nil { this.ErrorPage(err) return } for _, plan := range plansResp.NsPlans { var config = &dnsconfigs.NSPlanConfig{} if len(plan.ConfigJSON) == 0 { continue } err = json.Unmarshal(plan.ConfigJSON, config) if err != nil { remotelogs.Error("NSPlan", "decode plan config failed: plan: "+types.String(plan.Id)+": "+err.Error()) continue } planMaps = append(planMaps, maps.Map{ "id": plan.Id, "name": plan.Name, "monthlyPrice": plan.MonthlyPrice, "yearlyPrice": plan.YearlyPrice, "config": config, }) } this.Data["plans"] = planMaps this.Show() }