// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. package plans import ( "encoding/json" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "github.com/TeaOSLab/EdgeCommon/pkg/userconfigs" "github.com/TeaOSLab/EdgeUser/internal/configloaders" "github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils" "github.com/iwind/TeaGo/maps" ) type IndexAction struct { actionutils.ParentAction } func (this *IndexAction) Init() { this.Nav("index", "", "") } func (this *IndexAction) RunGet(params struct { Type string }) { if !this.ValidateFeature(userconfigs.UserFeatureCodePlan, 0) { return } // 检查是否可以操作套餐 { priceConfig, err := configloaders.LoadUserPriceConfig() if err != nil { this.ErrorPage(err) return } if priceConfig != nil && !priceConfig.ShowPlansInUserSystem { this.ForbidPage() return } } if len(params.Type) == 0 { params.Type = "available" } this.Data["type"] = params.Type var total int64 = 0 // 有效 { countResp, err := this.RPC().UserPlanRPC().CountAllEnabledUserPlans(this.UserContext(), &pb.CountAllEnabledUserPlansRequest{ IsAvailable: true, IsExpired: false, ExpiringDays: 0, }) if err != nil { this.ErrorPage(err) return } this.Data["countAvailable"] = countResp.Count if params.Type == "available" { total = countResp.Count } } // 过期 { countResp, err := this.RPC().UserPlanRPC().CountAllEnabledUserPlans(this.UserContext(), &pb.CountAllEnabledUserPlansRequest{ IsAvailable: false, IsExpired: true, ExpiringDays: 0, }) if err != nil { this.ErrorPage(err) return } this.Data["countExpired"] = countResp.Count if params.Type == "expired" { total = countResp.Count } } // 7天过期 { countResp, err := this.RPC().UserPlanRPC().CountAllEnabledUserPlans(this.UserContext(), &pb.CountAllEnabledUserPlansRequest{ IsAvailable: false, IsExpired: false, ExpiringDays: 7, }) if err != nil { this.ErrorPage(err) return } this.Data["countExpiring7"] = countResp.Count if params.Type == "expiring7" { total = countResp.Count } } // 30天过期 { countResp, err := this.RPC().UserPlanRPC().CountAllEnabledUserPlans(this.UserContext(), &pb.CountAllEnabledUserPlansRequest{ IsAvailable: false, IsExpired: false, ExpiringDays: 30, }) if err != nil { this.ErrorPage(err) return } this.Data["countExpiring30"] = countResp.Count if params.Type == "expiring30" { total = countResp.Count } } // 列表 var page = this.NewPage(total) this.Data["page"] = page.AsHTML() var expiringDays = int32(0) if params.Type == "expiring7" { expiringDays = 7 } else if params.Type == "expiring30" { expiringDays = 30 } userPlansResp, err := this.RPC().UserPlanRPC().ListEnabledUserPlans(this.UserContext(), &pb.ListEnabledUserPlansRequest{ IsAvailable: params.Type == "available", IsExpired: params.Type == "expired", ExpiringDays: expiringDays, Offset: page.Offset, Size: page.Size, }) if err != nil { this.ErrorPage(err) return } var userPlanMaps = []maps.Map{} for _, userPlan := range userPlansResp.UserPlans { // user var userMap = maps.Map{"id": 0} if userPlan.User != nil { userMap = maps.Map{ "id": userPlan.User.Id, "fullname": userPlan.User.Fullname, "username": userPlan.User.Username, } } // plan var planMap = maps.Map{"id": 0} var plan = userPlan.Plan if plan != nil && plan.Id > 0 { // 流量价格 var trafficPrice = &serverconfigs.PlanTrafficPriceConfig{} if len(plan.TrafficPriceJSON) > 0 { err = json.Unmarshal(plan.TrafficPriceJSON, trafficPrice) if err != nil { this.ErrorPage(err) return } } // 带宽价格 var bandwidthPrice = &serverconfigs.PlanBandwidthPriceConfig{} if len(plan.BandwidthPriceJSON) > 0 { err = json.Unmarshal(plan.BandwidthPriceJSON, bandwidthPrice) if err != nil { this.ErrorPage(err) return } } // 流量限制 var trafficLimit = &serverconfigs.TrafficLimitConfig{} if len(plan.TrafficLimitJSON) > 0 { err = json.Unmarshal(plan.TrafficLimitJSON, trafficLimit) if err != nil { this.ErrorPage(err) return } } // 带宽限制 // bandwidth limit var bandwidthLimitPerNode = &shared.SizeCapacity{} if len(plan.BandwidthLimitPerNodeJSON) > 0 { err = json.Unmarshal(plan.BandwidthLimitPerNodeJSON, bandwidthLimitPerNode) if err != nil { this.ErrorPage(err) return } } planMap = maps.Map{ "id": plan.Id, "name": plan.Name, "priceType": plan.PriceType, "monthlyPrice": plan.MonthlyPrice, "seasonallyPrice": plan.SeasonallyPrice, "yearlyPrice": plan.YearlyPrice, "trafficPrice": trafficPrice, "bandwidthPrice": bandwidthPrice, "trafficLimit": trafficLimit, "bandwidthLimitPerNode": bandwidthLimitPerNode, "dailyRequests": plan.DailyRequests, "monthlyRequests": plan.MonthlyRequests, "dailyWebsocketConnections": plan.DailyWebsocketConnections, "monthlyWebsocketConnections": plan.MonthlyWebsocketConnections, } } // server var serverMaps = []maps.Map{} if len(userPlan.Servers) > 0 { for _, server := range userPlan.Servers { // 补足网站名称 if len(server.Name) == 0 && len(server.ServerNamesJSON) > 0 { var serverNames = []*serverconfigs.ServerNameConfig{} err = json.Unmarshal(server.ServerNamesJSON, &serverNames) if err == nil && len(serverNames) > 0 { server.Name = serverNames[0].FirstName() } } serverMaps = append(serverMaps, maps.Map{ "id": server.Id, "name": server.Name, "type": server.Type, }) } } userPlanMaps = append(userPlanMaps, maps.Map{ "id": userPlan.Id, "isOn": userPlan.IsOn, "name": userPlan.Name, "dayTo": userPlan.DayTo, "user": userMap, "plan": planMap, "servers": serverMaps, }) } this.Data["userPlans"] = userPlanMaps this.Show() }