Initial commit (code only without large binaries)
This commit is contained in:
192
EdgeAdmin/internal/web/actions/default/plans/create.go
Normal file
192
EdgeAdmin/internal/web/actions/default/plans/create.go
Normal file
@@ -0,0 +1,192 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build plus
|
||||
|
||||
package plans
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"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"
|
||||
)
|
||||
|
||||
type CreateAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *CreateAction) Init() {
|
||||
this.Nav("", "", "create")
|
||||
}
|
||||
|
||||
func (this *CreateAction) RunGet(params struct{}) {
|
||||
// features
|
||||
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{}
|
||||
for _, feature := range allFeatures {
|
||||
if !feature.SupportPlan {
|
||||
continue
|
||||
}
|
||||
featureMaps = append(featureMaps, maps.Map{
|
||||
"name": feature.Name,
|
||||
"code": feature.Code,
|
||||
"description": feature.Description,
|
||||
"isChecked": true,
|
||||
})
|
||||
}
|
||||
this.Data["features"] = featureMaps
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *CreateAction) RunPost(params struct {
|
||||
Name string
|
||||
Description string
|
||||
ClusterId int64
|
||||
TrafficLimitJSON []byte
|
||||
BandwidthLimitPerNodeJSON []byte
|
||||
PriceType string
|
||||
MonthlyPrice float32
|
||||
SeasonallyPrice float32
|
||||
YearlyPrice float32
|
||||
TrafficPriceJSON []byte
|
||||
BandwidthPriceJSON []byte
|
||||
TotalServers int32
|
||||
TotalServerNames int32
|
||||
TotalServerNamesPerServer int32
|
||||
DailyRequests int64
|
||||
MonthlyRequests int64
|
||||
DailyWebsocketConnections int64
|
||||
MonthlyWebsocketConnections int64
|
||||
MaxUploadSizeJSON []byte
|
||||
|
||||
HasFullFeatures bool
|
||||
FeatureCodes []string
|
||||
|
||||
Must *actions.Must
|
||||
CSRF *actionutils.CSRF
|
||||
}) {
|
||||
var planId = int64(0)
|
||||
defer func() {
|
||||
this.CreateLogInfo(codes.Plan_LogCreatePlan, planId)
|
||||
}()
|
||||
|
||||
params.Must.
|
||||
Field("name", params.Name).
|
||||
Require("请输入套餐名称").
|
||||
Field("clusterId", params.ClusterId).
|
||||
Gt(0, "请选择关联集群")
|
||||
|
||||
if params.MonthlyPrice <= 0 {
|
||||
params.MonthlyPrice = 0
|
||||
}
|
||||
if params.SeasonallyPrice <= 0 {
|
||||
params.SeasonallyPrice = 0
|
||||
}
|
||||
if params.YearlyPrice <= 0 {
|
||||
params.YearlyPrice = 0
|
||||
}
|
||||
|
||||
switch params.PriceType {
|
||||
case serverconfigs.PlanPriceTypePeriod:
|
||||
if params.MonthlyPrice == 0 && params.SeasonallyPrice == 0 && params.YearlyPrice == 0 {
|
||||
this.Fail("月度、季度、年度至少需要设置一个价格。")
|
||||
return
|
||||
}
|
||||
|
||||
if params.MonthlyPrice > 0 {
|
||||
if params.SeasonallyPrice == 0 {
|
||||
this.Fail("由于你设置了月度价格,所以必须设置季度价格")
|
||||
return
|
||||
}
|
||||
if params.YearlyPrice == 0 {
|
||||
this.Fail("由于你设置了月度价格,所以必须设置年度价格")
|
||||
return
|
||||
}
|
||||
}
|
||||
if params.SeasonallyPrice > 0 {
|
||||
if params.YearlyPrice == 0 {
|
||||
this.Fail("由于你设置了季度价格,所以必须设置年度价格")
|
||||
return
|
||||
}
|
||||
}
|
||||
case serverconfigs.PlanPriceTypeTraffic:
|
||||
if len(params.TrafficPriceJSON) == 0 {
|
||||
this.Fail("请设置流量价格")
|
||||
}
|
||||
var config = &serverconfigs.PlanTrafficPriceConfig{}
|
||||
err := json.Unmarshal(params.TrafficPriceJSON, config)
|
||||
if err != nil {
|
||||
this.Fail("流量价格设置错误:" + err.Error())
|
||||
}
|
||||
if config.Base <= 0 {
|
||||
this.Fail("基础价格必须大于0")
|
||||
}
|
||||
case serverconfigs.PlanPriceTypeBandwidth:
|
||||
if len(params.BandwidthPriceJSON) == 0 {
|
||||
this.Fail("请设置带宽价格")
|
||||
}
|
||||
var config = &serverconfigs.PlanBandwidthPriceConfig{}
|
||||
err := json.Unmarshal(params.BandwidthPriceJSON, config)
|
||||
if err != nil {
|
||||
this.Fail("带宽价格设置错误:" + err.Error())
|
||||
}
|
||||
if config.Percentile <= 0 {
|
||||
this.Fail("百分位必须大于0")
|
||||
}
|
||||
if len(config.Ranges) == 0 {
|
||||
this.Fail("请添加带宽价格")
|
||||
}
|
||||
}
|
||||
|
||||
// features
|
||||
var featureCodes = []string{}
|
||||
if !params.HasFullFeatures && params.FeatureCodes != nil {
|
||||
featureCodes = params.FeatureCodes
|
||||
}
|
||||
|
||||
featureCodesJSON, err := json.Marshal(featureCodes)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
createResp, err := this.RPC().PlanRPC().CreatePlan(this.AdminContext(), &pb.CreatePlanRequest{
|
||||
Name: params.Name,
|
||||
Description: params.Description,
|
||||
ClusterId: params.ClusterId,
|
||||
TrafficLimitJSON: params.TrafficLimitJSON,
|
||||
BandwidthLimitPerNodeJSON: params.BandwidthLimitPerNodeJSON,
|
||||
HasFullFeatures: params.HasFullFeatures,
|
||||
FeaturesJSON: featureCodesJSON,
|
||||
PriceType: params.PriceType,
|
||||
TrafficPriceJSON: params.TrafficPriceJSON,
|
||||
BandwidthPriceJSON: params.BandwidthPriceJSON,
|
||||
MonthlyPrice: params.MonthlyPrice,
|
||||
SeasonallyPrice: params.SeasonallyPrice,
|
||||
YearlyPrice: params.YearlyPrice,
|
||||
TotalServers: params.TotalServers,
|
||||
TotalServerNames: params.TotalServerNames,
|
||||
TotalServerNamesPerServer: params.TotalServerNamesPerServer,
|
||||
DailyRequests: params.DailyRequests,
|
||||
MonthlyRequests: params.MonthlyRequests,
|
||||
DailyWebsocketConnections: params.DailyWebsocketConnections,
|
||||
MonthlyWebsocketConnections: params.MonthlyWebsocketConnections,
|
||||
MaxUploadSizeJSON: params.MaxUploadSizeJSON,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
planId = createResp.PlanId
|
||||
|
||||
this.Success()
|
||||
}
|
||||
146
EdgeAdmin/internal/web/actions/default/plans/index.go
Normal file
146
EdgeAdmin/internal/web/actions/default/plans/index.go
Normal file
@@ -0,0 +1,146 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build plus
|
||||
|
||||
package plans
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/finance/financeutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *IndexAction) Init() {
|
||||
this.Nav("", "", "index")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct{}) {
|
||||
countResp, err := this.RPC().PlanRPC().CountAllEnabledPlans(this.AdminContext(), &pb.CountAllEnabledPlansRequest{})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
var count = countResp.Count
|
||||
var page = this.NewPage(count, 100)
|
||||
|
||||
this.Data["page"] = page.AsHTML()
|
||||
|
||||
plansResp, err := this.RPC().PlanRPC().ListEnabledPlans(this.AdminContext(), &pb.ListEnabledPlansRequest{
|
||||
Offset: page.Offset,
|
||||
Size: page.Size,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
var planMaps = []maps.Map{}
|
||||
for _, plan := range plansResp.Plans {
|
||||
// 集群
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
planMaps = append(planMaps, maps.Map{
|
||||
"id": plan.Id,
|
||||
"name": plan.Name,
|
||||
"description": plan.Description,
|
||||
"isOn": plan.IsOn,
|
||||
"cluster": clusterMap,
|
||||
"priceType": plan.PriceType,
|
||||
"monthlyPrice": plan.MonthlyPrice,
|
||||
"seasonallyPrice": plan.SeasonallyPrice,
|
||||
"yearlyPrice": plan.YearlyPrice,
|
||||
"trafficPrice": trafficPrice,
|
||||
"bandwidthPrice": bandwidthPrice,
|
||||
"trafficLimit": trafficLimit,
|
||||
"bandwidthLimitPerNode": bandwidthLimitPerNode,
|
||||
"totalServers": plan.TotalServers,
|
||||
"dailyRequests": plan.DailyRequests,
|
||||
"monthlyRequests": plan.MonthlyRequests,
|
||||
"dailyWebsocketConnections": plan.DailyWebsocketConnections,
|
||||
"monthlyWebsocketConnections": plan.MonthlyWebsocketConnections,
|
||||
"maxUploadSize": maxUploadSize,
|
||||
})
|
||||
}
|
||||
this.Data["plans"] = planMaps
|
||||
|
||||
// 价格设置
|
||||
priceConfig, err := financeutils.ReadPriceConfig(this.AdminContext())
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
this.Data["canUsePlans"] = priceConfig != nil && priceConfig.EnablePlans
|
||||
|
||||
this.Show()
|
||||
}
|
||||
52
EdgeAdmin/internal/web/actions/default/plans/init.go
Normal file
52
EdgeAdmin/internal/web/actions/default/plans/init.go
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build plus
|
||||
|
||||
package plans
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/plus"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/ns/users"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/plans/plan"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/plans/userPlans"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/helpers"
|
||||
"github.com/iwind/TeaGo"
|
||||
)
|
||||
|
||||
func init() {
|
||||
TeaGo.BeforeStart(func(server *TeaGo.Server) {
|
||||
server.
|
||||
Helper(plus.NewHelper(plus.ComponentCodePlan)).
|
||||
Helper(helpers.NewUserMustAuth(configloaders.AdminModuleCodePlan)).
|
||||
Data("teaMenu", "plans").
|
||||
|
||||
// 套餐列表
|
||||
Prefix("/plans").
|
||||
Data("teaSubMenu", "plans").
|
||||
Get("", new(IndexAction)).
|
||||
GetPost("/create", new(CreateAction)).
|
||||
Post("/sort", new(SortAction)).
|
||||
|
||||
// 套餐详情
|
||||
Prefix("/plans/plan").
|
||||
Get("", new(plan.IndexAction)).
|
||||
GetPost("/update", new(plan.UpdateAction)).
|
||||
Post("/delete", new(plan.DeleteAction)).
|
||||
|
||||
// 用户套餐
|
||||
Prefix("/plans/userPlans").
|
||||
Data("teaSubMenu", "userPlans").
|
||||
Get("", new(userPlans.IndexAction)).
|
||||
GetPost("/createPopup", new(userPlans.CreatePopupAction)).
|
||||
Post("/delete", new(userPlans.DeleteAction)).
|
||||
GetPost("/renewPopup", new(userPlans.RenewPopupAction)).
|
||||
Post("/userAccount", new(userPlans.UserAccountAction)).
|
||||
|
||||
// 用户选项
|
||||
Prefix("/plans/users").
|
||||
Post("/options", new(users.OptionsAction)).
|
||||
|
||||
//
|
||||
EndAll()
|
||||
})
|
||||
}
|
||||
30
EdgeAdmin/internal/web/actions/default/plans/plan/delete.go
Normal file
30
EdgeAdmin/internal/web/actions/default/plans/plan/delete.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build plus
|
||||
|
||||
package plan
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
type DeleteAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *DeleteAction) RunPost(params struct {
|
||||
PlanId int64
|
||||
}) {
|
||||
defer this.CreateLogInfo(codes.Plan_LogDeletePlan, params.PlanId)
|
||||
|
||||
// TODO 检查套餐是否正在使用
|
||||
|
||||
_, err := this.RPC().PlanRPC().DeletePlan(this.AdminContext(), &pb.DeletePlanRequest{PlanId: params.PlanId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
156
EdgeAdmin/internal/web/actions/default/plans/plan/index.go
Normal file
156
EdgeAdmin/internal/web/actions/default/plans/plan/index.go
Normal file
@@ -0,0 +1,156 @@
|
||||
// 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()
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build plus
|
||||
|
||||
package planutils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
func InitPlan(parentAction *actionutils.ParentAction, planId int64) (*pb.Plan, error) {
|
||||
planResp, err := parentAction.RPC().PlanRPC().FindEnabledPlan(parentAction.AdminContext(), &pb.FindEnabledPlanRequest{PlanId: planId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var plan = planResp.Plan
|
||||
if plan == nil {
|
||||
return nil, errors.New("not found")
|
||||
}
|
||||
|
||||
parentAction.Data["plan"] = maps.Map{
|
||||
"id": plan.Id,
|
||||
"name": plan.Name,
|
||||
}
|
||||
return plan, nil
|
||||
}
|
||||
307
EdgeAdmin/internal/web/actions/default/plans/plan/update.go
Normal file
307
EdgeAdmin/internal/web/actions/default/plans/plan/update.go
Normal file
@@ -0,0 +1,307 @@
|
||||
// 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/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type UpdateAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *UpdateAction) Init() {
|
||||
this.Nav("", "", "update")
|
||||
}
|
||||
|
||||
func (this *UpdateAction) 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
|
||||
}
|
||||
}
|
||||
|
||||
// features
|
||||
var selectedFeatureCodes []string
|
||||
if !plan.HasFullFeatures {
|
||||
if len(plan.FeaturesJSON) > 0 {
|
||||
err = json.Unmarshal(plan.FeaturesJSON, &selectedFeatureCodes)
|
||||
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{}
|
||||
for _, feature := range allFeatures {
|
||||
if !feature.SupportPlan {
|
||||
continue
|
||||
}
|
||||
var isChecked = plan.HasFullFeatures || lists.ContainsString(selectedFeatureCodes, feature.Code)
|
||||
|
||||
featureMaps = append(featureMaps, maps.Map{
|
||||
"name": feature.Name,
|
||||
"code": feature.Code,
|
||||
"description": feature.Description,
|
||||
"isChecked": isChecked,
|
||||
})
|
||||
}
|
||||
this.Data["features"] = featureMaps
|
||||
|
||||
// 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,
|
||||
}
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *UpdateAction) RunPost(params struct {
|
||||
PlanId int64
|
||||
Name string
|
||||
Description string
|
||||
IsOn bool
|
||||
ClusterId int64
|
||||
TrafficLimitJSON []byte
|
||||
BandwidthLimitPerNodeJSON []byte
|
||||
PriceType string
|
||||
MonthlyPrice float32
|
||||
SeasonallyPrice float32
|
||||
YearlyPrice float32
|
||||
TrafficPriceJSON []byte
|
||||
BandwidthPriceJSON []byte
|
||||
TotalServers int32
|
||||
TotalServerNames int32
|
||||
TotalServerNamesPerServer int32
|
||||
DailyRequests int64
|
||||
MonthlyRequests int64
|
||||
DailyWebsocketConnections int64
|
||||
MonthlyWebsocketConnections int64
|
||||
MaxUploadSizeJSON []byte
|
||||
|
||||
HasFullFeatures bool
|
||||
FeatureCodes []string
|
||||
|
||||
Must *actions.Must
|
||||
CSRF *actionutils.CSRF
|
||||
}) {
|
||||
this.CreateLogInfo(codes.Plan_LogUpdatePlan, params.PlanId)
|
||||
|
||||
params.Must.
|
||||
Field("name", params.Name).
|
||||
Require("请输入套餐名称").
|
||||
Field("clusterId", params.ClusterId).
|
||||
Gt(0, "请选择关联集群")
|
||||
|
||||
if params.MonthlyPrice <= 0 {
|
||||
params.MonthlyPrice = 0
|
||||
}
|
||||
if params.SeasonallyPrice <= 0 {
|
||||
params.SeasonallyPrice = 0
|
||||
}
|
||||
if params.YearlyPrice <= 0 {
|
||||
params.YearlyPrice = 0
|
||||
}
|
||||
|
||||
switch params.PriceType {
|
||||
case serverconfigs.PlanPriceTypePeriod:
|
||||
if params.MonthlyPrice == 0 && params.SeasonallyPrice == 0 && params.YearlyPrice == 0 {
|
||||
this.Fail("月度、季度、年度至少需要设置一个价格。")
|
||||
return
|
||||
}
|
||||
|
||||
if params.MonthlyPrice > 0 {
|
||||
if params.SeasonallyPrice == 0 {
|
||||
this.Fail("由于你设置了月度价格,所以必须设置季度价格")
|
||||
return
|
||||
}
|
||||
if params.YearlyPrice == 0 {
|
||||
this.Fail("由于你设置了月度价格,所以必须设置年度价格")
|
||||
return
|
||||
}
|
||||
}
|
||||
if params.SeasonallyPrice > 0 {
|
||||
if params.YearlyPrice == 0 {
|
||||
this.Fail("由于你设置了季度价格,所以必须设置年度价格")
|
||||
return
|
||||
}
|
||||
}
|
||||
case serverconfigs.PlanPriceTypeTraffic:
|
||||
if len(params.TrafficPriceJSON) == 0 {
|
||||
this.Fail("请设置流量价格")
|
||||
}
|
||||
var config = &serverconfigs.PlanTrafficPriceConfig{}
|
||||
err := json.Unmarshal(params.TrafficPriceJSON, config)
|
||||
if err != nil {
|
||||
this.Fail("流量价格设置错误:" + err.Error())
|
||||
}
|
||||
if config.Base <= 0 {
|
||||
this.Fail("基础价格必须大于0")
|
||||
}
|
||||
case serverconfigs.PlanPriceTypeBandwidth:
|
||||
if len(params.BandwidthPriceJSON) == 0 {
|
||||
this.Fail("请设置带宽价格")
|
||||
}
|
||||
var config = &serverconfigs.PlanBandwidthPriceConfig{}
|
||||
err := json.Unmarshal(params.BandwidthPriceJSON, config)
|
||||
if err != nil {
|
||||
this.Fail("带宽价格设置错误:" + err.Error())
|
||||
}
|
||||
if config.Percentile <= 0 {
|
||||
this.Fail("百分位必须大于0")
|
||||
}
|
||||
if len(config.Ranges) == 0 {
|
||||
this.Fail("请添加带宽价格")
|
||||
}
|
||||
}
|
||||
|
||||
// features
|
||||
var featureCodes = []string{}
|
||||
if !params.HasFullFeatures && params.FeatureCodes != nil {
|
||||
featureCodes = params.FeatureCodes
|
||||
}
|
||||
|
||||
featureCodesJSON, err := json.Marshal(featureCodes)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = this.RPC().PlanRPC().UpdatePlan(this.AdminContext(), &pb.UpdatePlanRequest{
|
||||
PlanId: params.PlanId,
|
||||
Name: params.Name,
|
||||
Description: params.Description,
|
||||
IsOn: params.IsOn,
|
||||
ClusterId: params.ClusterId,
|
||||
TrafficLimitJSON: params.TrafficLimitJSON,
|
||||
BandwidthLimitPerNodeJSON: params.BandwidthLimitPerNodeJSON,
|
||||
HasFullFeatures: params.HasFullFeatures,
|
||||
FeaturesJSON: featureCodesJSON,
|
||||
PriceType: params.PriceType,
|
||||
TrafficPriceJSON: params.TrafficPriceJSON,
|
||||
BandwidthPriceJSON: params.BandwidthPriceJSON,
|
||||
MonthlyPrice: params.MonthlyPrice,
|
||||
SeasonallyPrice: params.SeasonallyPrice,
|
||||
YearlyPrice: params.YearlyPrice,
|
||||
TotalServers: params.TotalServers,
|
||||
TotalServerNames: params.TotalServerNames,
|
||||
TotalServerNamesPerServer: params.TotalServerNamesPerServer,
|
||||
DailyRequests: params.DailyRequests,
|
||||
MonthlyRequests: params.MonthlyRequests,
|
||||
DailyWebsocketConnections: params.DailyWebsocketConnections,
|
||||
MonthlyWebsocketConnections: params.MonthlyWebsocketConnections,
|
||||
MaxUploadSizeJSON: params.MaxUploadSizeJSON,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
28
EdgeAdmin/internal/web/actions/default/plans/sort.go
Normal file
28
EdgeAdmin/internal/web/actions/default/plans/sort.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build plus
|
||||
|
||||
package plans
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
type SortAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *SortAction) RunPost(params struct {
|
||||
Ids []int64
|
||||
}) {
|
||||
defer this.CreateLogInfo(codes.Plan_LogSortPlans)
|
||||
|
||||
_, err := this.RPC().PlanRPC().SortPlans(this.AdminContext(), &pb.SortPlansRequest{PlanIds: params.Ids})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
// 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()
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// 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"
|
||||
)
|
||||
|
||||
type DeleteAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *DeleteAction) RunPost(params struct {
|
||||
UserPlanId int64
|
||||
}) {
|
||||
defer this.CreateLogInfo(codes.UserPlan_LogDeleteUserPlan, params.UserPlanId)
|
||||
|
||||
_, err := this.RPC().UserPlanRPC().DeleteUserPlan(this.AdminContext(), &pb.DeleteUserPlanRequest{UserPlanId: params.UserPlanId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
195
EdgeAdmin/internal/web/actions/default/plans/userPlans/index.go
Normal file
195
EdgeAdmin/internal/web/actions/default/plans/userPlans/index.go
Normal file
@@ -0,0 +1,195 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build plus
|
||||
|
||||
package userPlans
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/finance/financeutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *IndexAction) Init() {
|
||||
this.Nav("", "", "")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct {
|
||||
Type string
|
||||
UserId int64
|
||||
}) {
|
||||
if len(params.Type) == 0 {
|
||||
params.Type = "available"
|
||||
}
|
||||
this.Data["type"] = params.Type
|
||||
|
||||
var total int64 = 0
|
||||
|
||||
// 有效
|
||||
{
|
||||
countResp, err := this.RPC().UserPlanRPC().CountAllEnabledUserPlans(this.AdminContext(), &pb.CountAllEnabledUserPlansRequest{
|
||||
IsAvailable: true,
|
||||
IsExpired: false,
|
||||
ExpiringDays: 0,
|
||||
UserId: params.UserId,
|
||||
})
|
||||
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.AdminContext(), &pb.CountAllEnabledUserPlansRequest{
|
||||
IsAvailable: false,
|
||||
IsExpired: true,
|
||||
ExpiringDays: 0,
|
||||
UserId: params.UserId,
|
||||
})
|
||||
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.AdminContext(), &pb.CountAllEnabledUserPlansRequest{
|
||||
IsAvailable: false,
|
||||
IsExpired: false,
|
||||
ExpiringDays: 7,
|
||||
UserId: params.UserId,
|
||||
})
|
||||
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.AdminContext(), &pb.CountAllEnabledUserPlansRequest{
|
||||
IsAvailable: false,
|
||||
IsExpired: false,
|
||||
ExpiringDays: 30,
|
||||
UserId: params.UserId,
|
||||
})
|
||||
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.AdminContext(), &pb.ListEnabledUserPlansRequest{
|
||||
IsAvailable: params.Type == "available",
|
||||
IsExpired: params.Type == "expired",
|
||||
ExpiringDays: expiringDays,
|
||||
UserId: params.UserId,
|
||||
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}
|
||||
if userPlan.Plan != nil {
|
||||
planMap = maps.Map{
|
||||
"id": userPlan.Plan.Id,
|
||||
"name": userPlan.Plan.Name,
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
// 价格设置
|
||||
priceConfig, err := financeutils.ReadPriceConfig(this.AdminContext())
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
this.Data["canUsePlans"] = priceConfig != nil && priceConfig.EnablePlans
|
||||
|
||||
this.Show()
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
// 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"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RenewPopupAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *RenewPopupAction) Init() {
|
||||
this.Nav("", "", "")
|
||||
}
|
||||
|
||||
func (this *RenewPopupAction) RunGet(params struct {
|
||||
UserPlanId int64
|
||||
}) {
|
||||
userPlanResp, err := this.RPC().UserPlanRPC().FindEnabledUserPlan(this.AdminContext(), &pb.FindEnabledUserPlanRequest{UserPlanId: params.UserPlanId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var userPlan = userPlanResp.UserPlan
|
||||
if userPlan == nil {
|
||||
this.Fail("找不到要修改的套餐")
|
||||
}
|
||||
var planId = userPlan.PlanId
|
||||
var userId = userPlan.UserId
|
||||
|
||||
this.Data["userPlanId"] = params.UserPlanId
|
||||
|
||||
// 套餐
|
||||
planResp, err := this.RPC().PlanRPC().FindEnabledPlan(this.AdminContext(), &pb.FindEnabledPlanRequest{PlanId: planId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var plan = planResp.Plan
|
||||
if plan == nil {
|
||||
this.Fail("找不到要购买的套餐")
|
||||
}
|
||||
this.Data["plan"] = maps.Map{
|
||||
"id": plan.Id,
|
||||
"name": plan.Name,
|
||||
"priceType": plan.PriceType,
|
||||
"monthlyPrice": plan.MonthlyPrice,
|
||||
"seasonallyPrice": plan.SeasonallyPrice,
|
||||
"yearlyPrice": plan.YearlyPrice,
|
||||
}
|
||||
|
||||
// 用户
|
||||
userResp, err := this.RPC().UserRPC().FindEnabledUser(this.AdminContext(), &pb.FindEnabledUserRequest{UserId: userId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var user = userResp.User
|
||||
if user == nil {
|
||||
this.Fail("找不到套餐所属用户")
|
||||
}
|
||||
this.Data["user"] = maps.Map{
|
||||
"id": user.Id,
|
||||
"username": user.Username,
|
||||
"fullname": user.Fullname,
|
||||
}
|
||||
|
||||
// 账户
|
||||
accountResp, err := this.RPC().UserAccountRPC().FindEnabledUserAccountWithUserId(this.AdminContext(), &pb.FindEnabledUserAccountWithUserIdRequest{UserId: userId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var account = accountResp.UserAccount
|
||||
if account == nil {
|
||||
this.Fail("找不到用户'" + types.String(userId) + "'的账户")
|
||||
}
|
||||
this.Data["userAccount"] = maps.Map{"total": account.Total}
|
||||
|
||||
// 默认结束日期
|
||||
var hasDefaultDayTo = false
|
||||
if len(userPlan.DayTo) > 0 {
|
||||
// 如果已经有日期,则自动增加1年
|
||||
var reg = regexp.MustCompile(`^(\d+)-(\d+)-(\d+)$`)
|
||||
if reg.MatchString(userPlan.DayTo) {
|
||||
var matches = reg.FindStringSubmatch(userPlan.DayTo)
|
||||
var year = types.Int(matches[1])
|
||||
var month = types.Int(matches[2])
|
||||
var day = types.Int(matches[3])
|
||||
if year > 1970 {
|
||||
this.Data["defaultDayTo"] = timeutil.Format("Y-m-d", time.Date(year+1, time.Month(month), day, 0, 0, 0, 0, time.Local))
|
||||
hasDefaultDayTo = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if !hasDefaultDayTo {
|
||||
this.Data["defaultDayTo"] = timeutil.Format("Y-m-d", time.Now().AddDate(20, 0, 0))
|
||||
}
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *RenewPopupAction) RunPost(params struct {
|
||||
UserPlanId int64
|
||||
|
||||
Period string
|
||||
CountMonths int32
|
||||
CountSeasons int32
|
||||
CountYears int32
|
||||
|
||||
DayTo string
|
||||
|
||||
Must *actions.Must
|
||||
CSRF *actionutils.CSRF
|
||||
}) {
|
||||
defer this.CreateLogInfo(codes.UserPlan_LogRenewUserPlan, params.UserPlanId)
|
||||
|
||||
userPlanResp, err := this.RPC().UserPlanRPC().FindEnabledUserPlan(this.AdminContext(), &pb.FindEnabledUserPlanRequest{UserPlanId: params.UserPlanId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var userPlan = userPlanResp.UserPlan
|
||||
if userPlan == nil {
|
||||
this.Fail("找不到要修改的套餐")
|
||||
}
|
||||
|
||||
var planId = userPlan.PlanId
|
||||
var userId = userPlan.UserId
|
||||
|
||||
// 检查余额
|
||||
planResp, err := this.RPC().PlanRPC().FindEnabledPlan(this.AdminContext(), &pb.FindEnabledPlanRequest{PlanId: 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: userId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var account = accountResp.UserAccount
|
||||
if account == nil {
|
||||
this.Fail("找不到用户'" + types.String(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().RenewUserPlan(this.AdminContext(), &pb.RenewUserPlanRequest{
|
||||
UserPlanId: params.UserPlanId,
|
||||
DayTo: params.DayTo,
|
||||
Period: params.Period,
|
||||
CountPeriod: countPeriod,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
// 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/rpc/pb"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type UserAccountAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *UserAccountAction) RunPost(params struct {
|
||||
UserId int64
|
||||
}) {
|
||||
resp, err := this.RPC().UserAccountRPC().FindEnabledUserAccountWithUserId(this.AdminContext(), &pb.FindEnabledUserAccountWithUserIdRequest{UserId: params.UserId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
var account = resp.UserAccount
|
||||
if account == nil {
|
||||
account = &pb.UserAccount{}
|
||||
}
|
||||
this.Data["account"] = maps.Map{
|
||||
"total": account.Total,
|
||||
"totalFrozen": account.TotalFrozen,
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
//go:build plus
|
||||
|
||||
package users
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type OptionsAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *OptionsAction) RunPost(params struct {
|
||||
Keyword string
|
||||
}) {
|
||||
usersResp, err := this.RPC().UserRPC().ListEnabledUsers(this.AdminContext(), &pb.ListEnabledUsersRequest{
|
||||
Keyword: params.Keyword,
|
||||
Offset: 0,
|
||||
Size: 100,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
var userMaps = []maps.Map{}
|
||||
for _, user := range usersResp.Users {
|
||||
userMaps = append(userMaps, maps.Map{
|
||||
"id": user.Id,
|
||||
"fullname": user.Fullname,
|
||||
"username": user.Username,
|
||||
"name": user.Fullname + "(" + user.Username + ")",
|
||||
})
|
||||
}
|
||||
this.Data["users"] = userMaps
|
||||
|
||||
this.Success()
|
||||
}
|
||||
Reference in New Issue
Block a user