// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. //go:build plus package userPlans import ( "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeCommon/pkg/langs/codes" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/iwind/TeaGo/actions" "github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/types" timeutil "github.com/iwind/TeaGo/utils/time" "time" ) type CreatePopupAction struct { actionutils.ParentAction } func (this *CreatePopupAction) Init() { this.Nav("", "", "") } func (this *CreatePopupAction) RunGet(params struct{}) { // 所有套餐 planResp, err := this.RPC().PlanRPC().FindAllAvailablePlans(this.AdminContext(), &pb.FindAllAvailablePlansRequest{}) if err != nil { this.ErrorPage(err) return } var planMaps = []maps.Map{} for _, plan := range planResp.Plans { planMaps = append(planMaps, maps.Map{ "id": plan.Id, "name": plan.Name, "priceType": plan.PriceType, "monthlyPrice": plan.MonthlyPrice, "seasonallyPrice": plan.SeasonallyPrice, "yearlyPrice": plan.YearlyPrice, }) } this.Data["plans"] = planMaps // 默认结束日期 this.Data["defaultDayTo"] = timeutil.Format("Y-m-d", time.Now().AddDate(20, 0, 0)) this.Show() } func (this *CreatePopupAction) RunPost(params struct { UserId int64 PlanId int64 Name string Period string CountMonths int32 CountSeasons int32 CountYears int32 DayTo string Must *actions.Must CSRF *actionutils.CSRF }) { defer this.CreateLogInfo(codes.UserPlan_LogBuyUserPlan, params.UserId, params.PlanId) params.Must. Field("userId", params.UserId). Gt(0, "请选择用户"). Field("name", params.Name). Require("请输入备注名称"). Field("planId", params.PlanId). Gt(0, "请选择套餐") // 检查余额 planResp, err := this.RPC().PlanRPC().FindEnabledPlan(this.AdminContext(), &pb.FindEnabledPlanRequest{PlanId: params.PlanId}) if err != nil { this.ErrorPage(err) return } var plan = planResp.Plan if plan == nil { this.Fail("找不到要购买的套餐") } var countPeriod int32 if plan.PriceType == serverconfigs.PlanPriceTypePeriod { var cost float64 switch params.Period { case "monthly": countPeriod = params.CountMonths cost = plan.MonthlyPrice * float64(countPeriod) case "seasonally": countPeriod = params.CountSeasons cost = plan.SeasonallyPrice * float64(countPeriod) case "yearly": countPeriod = params.CountYears cost = plan.YearlyPrice * float64(countPeriod) default: this.Fail("invalid period '" + params.Period + "'") } // 账号 accountResp, err := this.RPC().UserAccountRPC().FindEnabledUserAccountWithUserId(this.AdminContext(), &pb.FindEnabledUserAccountWithUserIdRequest{UserId: params.UserId}) if err != nil { this.ErrorPage(err) return } var account = accountResp.UserAccount if account == nil { this.Fail("找不到用户'" + types.String(params.UserId) + "'的账户") } if account.Total < cost { this.Fail("用户账户余额不足") } } else if plan.PriceType == serverconfigs.PlanPriceTypeTraffic { params.Must.Field("dayTo", params.DayTo). Match(`^\d+-\d+-\d+$`, "请输入正确的结束日期") } else if plan.PriceType == serverconfigs.PlanPriceTypeBandwidth { params.Must.Field("dayTo", params.DayTo). Match(`^\d+-\d+-\d+$`, "请输入正确的结束日期") } else { this.Fail("不支持的付款方式:" + plan.PriceType) } _, err = this.RPC().UserPlanRPC().BuyUserPlan(this.AdminContext(), &pb.BuyUserPlanRequest{ UserId: params.UserId, PlanId: params.PlanId, Name: params.Name, DayTo: params.DayTo, Period: params.Period, CountPeriod: countPeriod, }) if err != nil { this.ErrorPage(err) return } this.Success() }