1.4.5.2
This commit is contained in:
318
EdgeUser/internal/web/actions/default/plans/buy.go
Normal file
318
EdgeUser/internal/web/actions/default/plans/buy.go
Normal file
@@ -0,0 +1,318 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package plans
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"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/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"time"
|
||||
)
|
||||
|
||||
type BuyAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *BuyAction) Init() {
|
||||
this.Nav("", "", "buy")
|
||||
}
|
||||
|
||||
func (this *BuyAction) RunGet(params struct {
|
||||
PlanId int64
|
||||
}) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
this.Data["planId"] = params.PlanId
|
||||
this.Data["type"] = ""
|
||||
|
||||
// 菜单统计数据
|
||||
// 有效
|
||||
{
|
||||
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
|
||||
}
|
||||
|
||||
// 过期
|
||||
{
|
||||
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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 所有功能
|
||||
var allFeatureMaps = []maps.Map{}
|
||||
for _, feature := range userconfigs.FindAllUserFeatures() {
|
||||
if feature.SupportPlan {
|
||||
allFeatureMaps = append(allFeatureMaps, maps.Map{
|
||||
"name": feature.Name,
|
||||
"code": feature.Code,
|
||||
})
|
||||
}
|
||||
}
|
||||
this.Data["allFeatures"] = allFeatureMaps
|
||||
|
||||
// 所有套餐
|
||||
planResp, err := this.RPC().PlanRPC().FindAllAvailablePlans(this.UserContext(), &pb.FindAllAvailablePlansRequest{})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var planMaps = []maps.Map{}
|
||||
for _, plan := range planResp.Plans {
|
||||
// 流量限制
|
||||
var trafficLimitConfig = &serverconfigs.TrafficLimitConfig{}
|
||||
if len(plan.TrafficLimitJSON) > 0 {
|
||||
err = json.Unmarshal(plan.TrafficLimitJSON, trafficLimitConfig)
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// 功能
|
||||
var featureCodes = []string{}
|
||||
if len(plan.FeaturesJSON) > 0 {
|
||||
err = json.Unmarshal(plan.FeaturesJSON, &featureCodes)
|
||||
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,
|
||||
"priceType": plan.PriceType,
|
||||
"monthlyPrice": plan.MonthlyPrice,
|
||||
"seasonallyPrice": plan.SeasonallyPrice,
|
||||
"yearlyPrice": plan.YearlyPrice,
|
||||
"totalServers": plan.TotalServers,
|
||||
"totalServerNames": plan.TotalServerNames,
|
||||
"totalServerNamesPerServer": plan.TotalServerNamesPerServer,
|
||||
"dailyRequests": plan.DailyRequests,
|
||||
"monthlyRequests": plan.MonthlyRequests,
|
||||
"dailyWebsocketConnections": plan.DailyWebsocketConnections,
|
||||
"monthlyWebsocketConnections": plan.MonthlyWebsocketConnections,
|
||||
"trafficLimit": trafficLimitConfig,
|
||||
"bandwidthLimitPerNode": bandwidthLimitPerNode,
|
||||
"maxUploadSize": maxUploadSize,
|
||||
"hasFullFeatures": plan.HasFullFeatures,
|
||||
"featureCodes": featureCodes,
|
||||
})
|
||||
}
|
||||
this.Data["plans"] = planMaps
|
||||
|
||||
// 账户
|
||||
accountResp, err := this.RPC().UserAccountRPC().FindEnabledUserAccountWithUserId(this.UserContext(), &pb.FindEnabledUserAccountWithUserIdRequest{UserId: this.UserId()})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var account = accountResp.UserAccount
|
||||
if account == nil {
|
||||
this.Fail("找不到用户'" + types.String(this.UserId()) + "'的账户")
|
||||
}
|
||||
this.Data["userAccount"] = maps.Map{"total": account.Total}
|
||||
|
||||
// 默认结束日期
|
||||
this.Data["defaultDayTo"] = timeutil.Format("Y-m-d", time.Now().AddDate(20, 0, 0))
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *BuyAction) RunPost(params struct {
|
||||
PlanId int64
|
||||
Name string
|
||||
|
||||
Period string
|
||||
CountMonths int32
|
||||
CountSeasons int32
|
||||
CountYears int32
|
||||
|
||||
DayTo string
|
||||
|
||||
Must *actions.Must
|
||||
CSRF *actionutils.CSRF
|
||||
}) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
defer this.CreateLogInfo(codes.UserPlan_LogBuyUserPlan, this.UserId())
|
||||
|
||||
params.Must.
|
||||
Field("planId", params.PlanId).
|
||||
Gt(0, "请选择套餐")
|
||||
|
||||
// 检查余额
|
||||
planResp, err := this.RPC().PlanRPC().FindEnabledPlan(this.UserContext(), &pb.FindEnabledPlanRequest{PlanId: params.PlanId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var plan = planResp.Plan
|
||||
if plan == nil {
|
||||
this.Fail("找不到要购买的套餐")
|
||||
}
|
||||
if len(params.Name) == 0 {
|
||||
params.Name = plan.Name
|
||||
}
|
||||
|
||||
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.UserContext(), &pb.FindEnabledUserAccountWithUserIdRequest{UserId: this.UserId()})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var account = accountResp.UserAccount
|
||||
if account == nil {
|
||||
this.Fail("找不到用户'" + types.String(this.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.UserContext(), &pb.BuyUserPlanRequest{
|
||||
UserId: this.UserId(),
|
||||
PlanId: params.PlanId,
|
||||
Name: params.Name,
|
||||
DayTo: params.DayTo,
|
||||
Period: params.Period,
|
||||
CountPeriod: countPeriod,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
46
EdgeUser/internal/web/actions/default/plans/delete.go
Normal file
46
EdgeUser/internal/web/actions/default/plans/delete.go
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package plans
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
|
||||
)
|
||||
|
||||
type DeleteAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *DeleteAction) RunPost(params struct {
|
||||
UserPlanId int64
|
||||
}) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
defer this.CreateLogInfo(codes.UserPlan_LogDeleteUserPlan, params.UserPlanId)
|
||||
|
||||
_, err := this.RPC().UserPlanRPC().DeleteUserPlan(this.UserContext(), &pb.DeleteUserPlanRequest{UserPlanId: params.UserPlanId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
253
EdgeUser/internal/web/actions/default/plans/index.go
Normal file
253
EdgeUser/internal/web/actions/default/plans/index.go
Normal file
@@ -0,0 +1,253 @@
|
||||
// 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()
|
||||
}
|
||||
20
EdgeUser/internal/web/actions/default/plans/init.go
Normal file
20
EdgeUser/internal/web/actions/default/plans/init.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package plans
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeUser/internal/web/helpers"
|
||||
"github.com/iwind/TeaGo"
|
||||
)
|
||||
|
||||
func init() {
|
||||
TeaGo.BeforeStart(func(server *TeaGo.Server) {
|
||||
server.
|
||||
Helper(helpers.NewUserMustAuth("")).
|
||||
Data("teaMenu", "plans").
|
||||
Prefix("/plans").
|
||||
GetPost("", new(IndexAction)).
|
||||
GetPost("/buy", new(BuyAction)).
|
||||
Post("/delete", new(DeleteAction)).
|
||||
GetPost("/renewPopup", new(RenewPopupAction)).
|
||||
EndAll()
|
||||
})
|
||||
}
|
||||
225
EdgeUser/internal/web/actions/default/plans/renewPopup.go
Normal file
225
EdgeUser/internal/web/actions/default/plans/renewPopup.go
Normal file
@@ -0,0 +1,225 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package plans
|
||||
|
||||
import (
|
||||
"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/userconfigs"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
|
||||
"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
|
||||
}) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
userPlanResp, err := this.RPC().UserPlanRPC().FindEnabledUserPlan(this.UserContext(), &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.UserContext(), &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,
|
||||
}
|
||||
|
||||
// 账户
|
||||
accountResp, err := this.RPC().UserAccountRPC().FindEnabledUserAccountWithUserId(this.UserContext(), &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
|
||||
}) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
defer this.CreateLogInfo(codes.UserPlan_LogRenewUserPlan, params.UserPlanId)
|
||||
|
||||
userPlanResp, err := this.RPC().UserPlanRPC().FindEnabledUserPlan(this.UserContext(), &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.UserContext(), &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.UserContext(), &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.UserContext(), &pb.RenewUserPlanRequest{
|
||||
UserPlanId: params.UserPlanId,
|
||||
DayTo: params.DayTo,
|
||||
Period: params.Period,
|
||||
CountPeriod: countPeriod,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
Reference in New Issue
Block a user