Files
waf-platform/EdgeAdmin/internal/web/actions/default/plans/plan/index.go
2026-02-04 20:27:13 +08:00

157 lines
4.4 KiB
Go

// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
//go:build plus
package plan
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/plans/plan/planutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "index")
}
func (this *IndexAction) RunGet(params struct {
PlanId int64
}) {
plan, err := planutils.InitPlan(this.Parent(), params.PlanId)
if err != nil {
this.ErrorPage(err)
return
}
// 集群
clusterResp, err := this.RPC().NodeClusterRPC().FindEnabledNodeCluster(this.AdminContext(), &pb.FindEnabledNodeClusterRequest{NodeClusterId: plan.ClusterId})
if err != nil {
this.ErrorPage(err)
return
}
var clusterMap = maps.Map{
"id": 0,
"name": "",
}
if clusterResp.NodeCluster != nil {
clusterMap["id"] = clusterResp.NodeCluster.Id
clusterMap["name"] = clusterResp.NodeCluster.Name
}
// 流量价格
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.BitSizeCapacity{}
if len(plan.BandwidthLimitPerNodeJSON) > 0 {
err = json.Unmarshal(plan.BandwidthLimitPerNodeJSON, bandwidthLimitPerNode)
if err != nil {
this.ErrorPage(err)
return
}
}
// 功能
featuresResp, err := this.RPC().UserRPC().FindAllUserFeatureDefinitions(this.AdminContext(), &pb.FindAllUserFeatureDefinitionsRequest{})
if err != nil {
this.ErrorPage(err)
return
}
var allFeatures = featuresResp.Features
var featureMaps = []maps.Map{}
if len(plan.FeaturesJSON) > 0 {
var selectedFeatureCodes []string
err = json.Unmarshal(plan.FeaturesJSON, &selectedFeatureCodes)
if err != nil {
this.ErrorPage(err)
return
}
for _, feature := range allFeatures {
if !feature.SupportPlan {
continue
}
featureMaps = append(featureMaps, maps.Map{
"name": feature.Name,
"description": feature.Description,
"code": feature.Code,
"isChecked": plan.HasFullFeatures || lists.ContainsString(selectedFeatureCodes, feature.Code),
})
}
}
// max upload size
var maxUploadSize = &shared.SizeCapacity{}
if len(plan.MaxUploadSizeJSON) > 0 {
err = json.Unmarshal(plan.MaxUploadSizeJSON, maxUploadSize)
if err != nil {
this.ErrorPage(err)
return
}
}
this.Data["plan"] = maps.Map{
"id": plan.Id,
"name": plan.Name,
"description": plan.Description,
"isOn": plan.IsOn,
"cluster": clusterMap,
"priceType": plan.PriceType,
"trafficPrice": trafficPrice,
"bandwidthPrice": bandwidthPrice,
"monthlyPrice": plan.MonthlyPrice,
"seasonallyPrice": plan.SeasonallyPrice,
"yearlyPrice": plan.YearlyPrice,
"trafficLimit": trafficLimit,
"bandwidthLimitPerNode": bandwidthLimitPerNode,
"totalServers": plan.TotalServers,
"totalServerNames": plan.TotalServerNames,
"totalServerNamesPerServer": plan.TotalServerNamesPerServer,
"dailyRequests": plan.DailyRequests,
"monthlyRequests": plan.MonthlyRequests,
"dailyWebsocketConnections": plan.DailyWebsocketConnections,
"monthlyWebsocketConnections": plan.MonthlyWebsocketConnections,
"maxUploadSize": maxUploadSize,
"hasFullFeatures": plan.HasFullFeatures,
"features": featureMaps,
}
this.Show()
}