This commit is contained in:
unknown
2026-02-04 20:27:13 +08:00
commit 3b042d1dad
9410 changed files with 1488147 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package antiddos
import "github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "")
}
func (this *IndexAction) RunGet(params struct{}) {
this.RedirectURL("/anti-ddos/instances")
}

View File

@@ -0,0 +1,40 @@
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package antiddos
import (
"github.com/TeaOSLab/EdgeUser/internal/web/actions/default/anti-ddos/instances"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/default/anti-ddos/packages"
"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", "anti-ddos").
Prefix("/anti-ddos").
Get("", new(IndexAction)).
// 实例
Prefix("/anti-ddos/instances").
Data("teaSubMenu", "instance").
Get("", new(instances.IndexAction)).
Post("/delete", new(instances.DeleteAction)).
GetPost("/updateObjectsPopup", new(instances.UpdateObjectsPopupAction)).
Post("/userServers", new(instances.UserServersAction)).
GetPost("/renew", new(instances.RenewAction)).
GetPost("/renewConfirm", new(instances.RenewConfirmAction)).
// 产品
Prefix("/anti-ddos/packages").
Data("teaSubMenu", "package").
Get("", new(packages.IndexAction)).
Post("/price", new(packages.PriceAction)).
GetPost("/confirm", new(packages.ConfirmAction)).
//
EndAll()
})
}

View File

@@ -0,0 +1,27 @@
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package instances
import (
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
)
type DeleteAction struct {
actionutils.ParentAction
}
func (this *DeleteAction) RunPost(params struct {
UserInstanceId int64
}) {
defer this.CreateLogInfo(codes.UserADInstance_LogDeleteUserADInstance, params.UserInstanceId)
_, err := this.RPC().UserADInstanceRPC().DeleteUserADInstance(this.UserContext(), &pb.DeleteUserADInstanceRequest{UserADInstanceId: params.UserInstanceId})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,102 @@
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package instances
import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"github.com/TeaOSLab/EdgeUser/internal/utils/dateutils"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "")
}
func (this *IndexAction) RunGet(params struct {
NetworkId int64
PeriodId int64
}) {
countResp, err := this.RPC().UserADInstanceRPC().CountUserADInstances(this.UserContext(), &pb.CountUserADInstancesRequest{
AdNetworkId: params.NetworkId,
ExpiresDay: "",
AdPackagePeriodId: params.PeriodId,
})
if err != nil {
this.ErrorPage(err)
return
}
var count = countResp.Count
var page = this.NewPage(count)
this.Data["page"] = page.AsHTML()
userInstancesResp, err := this.RPC().UserADInstanceRPC().ListUserADInstances(this.UserContext(), &pb.ListUserADInstancesRequest{
AdNetworkId: params.NetworkId,
AdPackagePeriodId: params.PeriodId,
ExpiresDay: "",
AvailableOnly: false,
Offset: page.Offset,
Size: page.Size,
})
if err != nil {
this.ErrorPage(err)
return
}
var userInstanceMaps = []maps.Map{}
for _, userInstance := range userInstancesResp.UserADInstances {
// 实例
var instanceMap = maps.Map{
"id": 0,
"userInstanceId": 0,
}
if userInstance.AdPackageInstance != nil {
instanceMap = maps.Map{
"id": userInstance.AdPackageInstance.Id,
"userInstanceId": userInstance.AdPackageInstance.UserInstanceId,
}
}
// 产品
var packageMap = maps.Map{
"id": 0,
}
if userInstance.AdPackageInstance != nil && userInstance.AdPackageInstance.AdPackage != nil {
packageMap = maps.Map{
"id": userInstance.AdPackageInstance.AdPackage.Id,
"summary": userInstance.AdPackageInstance.AdPackage.Summary,
}
}
// 实例
var ipAddresses = []string{}
if userInstance.AdPackageInstance != nil && len(userInstance.AdPackageInstance.IpAddresses) > 0 {
ipAddresses = userInstance.AdPackageInstance.IpAddresses
}
userInstanceMaps = append(userInstanceMaps, maps.Map{
"id": userInstance.Id,
"dayFrom": dateutils.SplitYmd(userInstance.DayFrom),
"dayTo": dateutils.SplitYmd(userInstance.DayTo),
"instance": instanceMap,
"package": packageMap,
"ipAddresses": ipAddresses,
"createdTime": timeutil.FormatTime("Y-m-d H:i:s", userInstance.CreatedAt),
"periodCount": userInstance.AdPackagePeriodCount,
"periodUnitName": userconfigs.ADPackagePeriodUnitName(userInstance.AdPackagePeriodUnit),
"canDelete": true,
"isExpired": userInstance.DayTo < timeutil.Format("Ymd"),
"isAvailable": userInstance.IsAvailable,
"countObjects": userInstance.CountObjects,
})
}
this.Data["userInstances"] = userInstanceMaps
this.Show()
}

View File

@@ -0,0 +1,114 @@
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package instances
import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"github.com/TeaOSLab/EdgeUser/internal/utils/dateutils"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
)
type RenewAction struct {
actionutils.ParentAction
}
func (this *RenewAction) RunGet(params struct {
UserInstanceId int64
}) {
userInstanceResp, err := this.RPC().UserADInstanceRPC().FindUserADInstance(this.UserContext(), &pb.FindUserADInstanceRequest{UserADInstanceId: params.UserInstanceId})
if err != nil {
this.ErrorPage(err)
return
}
var userInstance = userInstanceResp.UserADInstance
if userInstance == nil {
this.NotFound("userInstance", params.UserInstanceId)
return
}
// 实例
var instanceMap = maps.Map{
"id": 0,
}
if userInstance.AdPackageInstance != nil {
if userInstance.AdPackageInstance.IpAddresses == nil {
userInstance.AdPackageInstance.IpAddresses = []string{}
}
instanceMap = maps.Map{
"id": userInstance.AdPackageInstance.Id,
"ipAddresses": userInstance.AdPackageInstance.IpAddresses,
}
}
// 产品
var packageMap = maps.Map{
"id": 0,
}
var packageId int64
if userInstance.AdPackageInstance != nil && userInstance.AdPackageInstance.AdPackage != nil {
packageId = userInstance.AdPackageInstance.AdPackage.Id
packageMap = maps.Map{
"id": userInstance.AdPackageInstance.AdPackage.Id,
"summary": userInstance.AdPackageInstance.AdPackage.Summary,
}
}
this.Data["userInstance"] = maps.Map{
"id": userInstance.Id,
"periodId": userInstance.AdPackagePeriodId,
"dayTo": dateutils.SplitYmd(userInstance.DayTo),
"today": timeutil.Format("Y-m-d"),
"isExpired": len(userInstance.DayTo) == 0 || userInstance.DayTo < timeutil.Format("Ymd"),
"isAvailable": userInstance.IsAvailable,
"instance": instanceMap,
"package": packageMap,
}
// 价格选项
if packageId > 0 {
pricesResp, err := this.RPC().ADPackagePriceRPC().FindADPackagePrices(this.UserContext(), &pb.FindADPackagePricesRequest{AdPackageId: packageId})
if err != nil {
this.ErrorPage(err)
return
}
var priceMaps = []maps.Map{}
var allValidPeriodIds = []int64{}
for _, price := range pricesResp.AdPackagePrices {
allValidPeriodIds = append(allValidPeriodIds, price.AdPackagePeriodId)
priceMaps = append(priceMaps, maps.Map{
"periodId": price.AdPackagePeriodId,
"price": price.Price,
})
}
this.Data["prices"] = priceMaps
// 有效期选项
var periodMaps = []maps.Map{}
periodResp, err := this.RPC().ADPackagePeriodRPC().FindAllAvailableADPackagePeriods(this.UserContext(), &pb.FindAllAvailableADPackagePeriodsRequest{})
if err != nil {
this.ErrorPage(err)
return
}
for _, period := range periodResp.AdPackagePeriods {
if !lists.ContainsInt64(allValidPeriodIds, period.Id) {
continue
}
periodMaps = append(periodMaps, maps.Map{
"id": period.Id,
"count": period.Count,
"unit": period.Unit,
"unitName": userconfigs.ADPackagePeriodUnitName(period.Unit),
})
}
this.Data["allPeriods"] = periodMaps
} else {
this.Data["allPeriods"] = []maps.Map{}
this.Data["prices"] = []maps.Map{}
}
this.Show()
}

View File

@@ -0,0 +1,204 @@
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package instances
import (
"encoding/json"
"fmt"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"github.com/TeaOSLab/EdgeUser/internal/utils/dateutils"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/default/finance/financeutils"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
)
type RenewConfirmAction struct {
actionutils.ParentAction
}
func (this *RenewConfirmAction) Init() {
this.Nav("", "", "")
}
func (this *RenewConfirmAction) RunGet(params struct {
UserInstanceId int64
PeriodId int64
}) {
this.Data["userInstanceId"] = params.UserInstanceId
this.Data["periodId"] = params.PeriodId
periodResp, err := this.RPC().ADPackagePeriodRPC().FindADPackagePeriod(this.UserContext(), &pb.FindADPackagePeriodRequest{AdPackagePeriodId: params.PeriodId})
if err != nil {
this.ErrorPage(err)
return
}
var period = periodResp.AdPackagePeriod
if period == nil {
this.ErrorText("period not found")
return
}
this.Data["periodName"] = types.String(period.Count) + userconfigs.ADPackagePeriodUnitName(period.Unit)
userInstanceResp, err := this.RPC().UserADInstanceRPC().FindUserADInstance(this.UserContext(), &pb.FindUserADInstanceRequest{UserADInstanceId: params.UserInstanceId})
if err != nil {
this.ErrorPage(err)
return
}
var userInstance = userInstanceResp.UserADInstance
if userInstance == nil {
this.NotFound("userInstance", params.UserInstanceId)
return
}
this.Data["dayTo"] = dateutils.SplitYmd(userInstance.DayTo)
// 实例
if userInstance.AdPackageInstance == nil {
this.ErrorText("adPackageInstance not found")
return
}
if userInstance.AdPackageInstance.IpAddresses == nil {
userInstance.AdPackageInstance.IpAddresses = []string{}
}
this.Data["ipAddresses"] = userInstance.AdPackageInstance.IpAddresses
// 产品
if userInstance.AdPackageInstance.AdPackage == nil {
this.ErrorText("adPackage not found")
return
}
var packageId = userInstance.AdPackageInstance.AdPackage.Id
this.Data["packageSummary"] = userInstance.AdPackageInstance.AdPackage.Summary
// 价格
priceResp, err := this.RPC().ADPackagePriceRPC().FindADPackagePrice(this.UserContext(), &pb.FindADPackagePriceRequest{
AdPackageId: packageId,
AdPackagePeriodId: params.PeriodId,
Count: 1,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["amount"] = priceResp.Amount
this.Show()
}
func (this *RenewConfirmAction) RunPost(params struct {
UserInstanceId int64
PeriodId int64
MethodCode string
Must *actions.Must
CSRF *actionutils.CSRF
}) {
if params.UserInstanceId <= 0 || params.PeriodId <= 0 {
this.Fail("参数错误,请重新下单")
return
}
userInstanceResp, err := this.RPC().UserADInstanceRPC().FindUserADInstance(this.UserContext(), &pb.FindUserADInstanceRequest{UserADInstanceId: params.UserInstanceId})
if err != nil {
this.ErrorPage(err)
return
}
var userInstance = userInstanceResp.UserADInstance
if userInstance == nil {
this.NotFound("userInstance", params.UserInstanceId)
return
}
if userInstance.AdPackageInstance == nil {
this.ErrorText("invalid adPackageInstance")
return
}
var packageId = userInstance.AdPackageInstance.AdPackageId
if !userInstance.IsAvailable {
this.ErrorText("invalid user instance")
return
}
periodResp, err := this.RPC().ADPackagePeriodRPC().FindADPackagePeriod(this.UserContext(), &pb.FindADPackagePeriodRequest{AdPackagePeriodId: params.PeriodId})
if err != nil {
this.ErrorPage(err)
return
}
var period = periodResp.AdPackagePeriod
if period == nil {
this.Fail("invalid 'periodId'")
return
}
priceResp, err := this.RPC().ADPackagePriceRPC().FindADPackagePrice(this.UserContext(), &pb.FindADPackagePriceRequest{
AdPackageId: packageId,
AdPackagePeriodId: params.PeriodId,
Count: 1,
})
if err != nil {
this.ErrorPage(err)
return
}
var amount = priceResp.Amount
if amount <= 0 {
this.Fail("invalid 'amount': " + types.String(amount))
return
}
// 使用余额购买
this.Data["success"] = false
this.Data["orderCode"] = ""
if params.MethodCode == "@balance" {
balance, err := financeutils.FindUserBalance(this.UserContext())
if err != nil {
this.ErrorPage(err)
return
}
if amount > balance {
this.Fail("当前余额不足,需要:" + fmt.Sprintf("%.2f元", amount) + ",现有:" + fmt.Sprintf("%.2f元", balance) + " ,请充值后再试")
return
}
// 直接购买
_, err = this.RPC().UserADInstanceRPC().RenewUserADInstance(this.UserContext(), &pb.RenewUserADInstanceRequest{
UserADInstanceId: params.UserInstanceId,
AdPackagePeriodId: params.PeriodId,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["success"] = true
this.Success()
return
}
// 生成订单
var orderParams = &userconfigs.OrderTypeRenewAntiDDoSInstanceParams{
UserInstanceId: params.UserInstanceId,
PeriodId: params.PeriodId,
}
orderParamsJSON, err := json.Marshal(orderParams)
if err != nil {
this.ErrorPage(err)
return
}
resp, err := this.RPC().UserOrderRPC().CreateUserOrder(this.UserContext(), &pb.CreateUserOrderRequest{
Type: userconfigs.OrderTypeRenewAntiDDoSInstance,
OrderMethodCode: params.MethodCode,
Amount: amount,
ParamsJSON: orderParamsJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["orderCode"] = resp.Code
this.Success()
}

View File

@@ -0,0 +1,129 @@
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package instances
import (
"encoding/json"
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeUser/internal/utils/dateutils"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
)
type UpdateObjectsPopupAction struct {
actionutils.ParentAction
}
func (this *UpdateObjectsPopupAction) Init() {
this.Nav("", "", "")
}
func (this *UpdateObjectsPopupAction) RunGet(params struct {
UserInstanceId int64
}) {
userInstanceResp, err := this.RPC().UserADInstanceRPC().FindUserADInstance(this.UserContext(), &pb.FindUserADInstanceRequest{UserADInstanceId: params.UserInstanceId})
if err != nil {
this.ErrorPage(err)
return
}
var userInstance = userInstanceResp.UserADInstance
if userInstance == nil {
this.NotFound("userInstance", params.UserInstanceId)
return
}
var objectMaps = []maps.Map{}
if len(userInstance.ObjectsJSON) > 0 {
err = json.Unmarshal(userInstance.ObjectsJSON, &objectMaps)
if err != nil {
this.ErrorPage(err)
return
}
}
// 用户
var userMap = maps.Map{
"id": 0,
}
if userInstance.User != nil {
userMap = maps.Map{
"id": userInstance.User.Id,
"fullname": userInstance.User.Fullname,
"username": userInstance.User.Username,
}
}
// 实例
var instanceMap = maps.Map{
"id": 0,
}
if userInstance.AdPackageInstance != nil {
if userInstance.AdPackageInstance.IpAddresses == nil {
userInstance.AdPackageInstance.IpAddresses = []string{}
}
instanceMap = maps.Map{
"id": userInstance.AdPackageInstance.Id,
"ipAddresses": userInstance.AdPackageInstance.IpAddresses,
}
}
// 产品
var packageMap = maps.Map{
"id": 0,
}
if userInstance.AdPackageInstance != nil && userInstance.AdPackageInstance.AdPackage != nil {
packageMap = maps.Map{
"id": userInstance.AdPackageInstance.AdPackage.Id,
"summary": userInstance.AdPackageInstance.AdPackage.Summary,
}
}
this.Data["userInstance"] = maps.Map{
"id": userInstance.Id,
"periodId": userInstance.AdPackagePeriodId,
"isAvailable": userInstance.IsAvailable,
"objects": objectMaps,
"dayTo": dateutils.SplitYmd(userInstance.DayTo),
"today": timeutil.Format("Y-m-d"),
"isExpired": len(userInstance.DayTo) == 0 || userInstance.DayTo < timeutil.Format("Ymd"),
"user": userMap,
"instance": instanceMap,
"package": packageMap,
}
this.Show()
}
func (this *UpdateObjectsPopupAction) RunPost(params struct {
UserInstanceId int64
ObjectCodesJSON []byte
Must *actions.Must
CSRF *actionutils.CSRF
}) {
defer this.CreateLogInfo(codes.UserADInstance_LogUpdateUserADInstanceObjects, params.UserInstanceId)
var objectCodes = []string{}
if len(params.ObjectCodesJSON) > 0 {
err := json.Unmarshal(params.ObjectCodesJSON, &objectCodes)
if err != nil {
this.ErrorPage(err)
return
}
}
// TODO 检查有没有超出最大防护对象数量
_, err := this.RPC().UserADInstanceRPC().UpdateUserADInstanceObjects(this.UserContext(), &pb.UpdateUserADInstanceObjectsRequest{
UserADInstanceId: params.UserInstanceId,
ObjectCodes: objectCodes,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,57 @@
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package instances
import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/maps"
)
type UserServersAction struct {
actionutils.ParentAction
}
func (this *UserServersAction) RunPost(params struct {
Page int32
}) {
var size int64 = 10
// 数量
countResp, err := this.RPC().ServerRPC().CountAllEnabledServersMatch(this.UserContext(), &pb.CountAllEnabledServersMatchRequest{
UserId: this.UserId(),
})
if err != nil {
this.ErrorPage(err)
return
}
var count = countResp.Count
var page = this.NewPage(count, size)
// 列表
serversResp, err := this.RPC().ServerRPC().ListEnabledServersMatch(this.UserContext(), &pb.ListEnabledServersMatchRequest{
Offset: page.Offset,
Size: page.Size,
UserId: this.UserId(),
IgnoreServerNames: true,
IgnoreSSLCerts: true,
})
if err != nil {
this.ErrorPage(err)
return
}
var serverMaps = []maps.Map{}
for _, server := range serversResp.Servers {
serverMaps = append(serverMaps, maps.Map{
"id": server.Id,
"name": server.Name,
})
}
this.Data["servers"] = serverMaps
this.Data["page"] = maps.Map{
"max": page.Max,
}
this.Success()
}

View File

@@ -0,0 +1,190 @@
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package packages
import (
"encoding/json"
"fmt"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/default/finance/financeutils"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
)
type ConfirmAction struct {
actionutils.ParentAction
}
func (this *ConfirmAction) Init() {
this.Nav("", "", "")
}
func (this *ConfirmAction) RunGet(params struct {
PackageId int64
PeriodId int64
Count int32
}) {
this.Data["packageId"] = params.PackageId
this.Data["periodId"] = params.PeriodId
this.Data["count"] = params.Count
this.Data["packageSummary"] = ""
this.Data["periodName"] = ""
this.Data["amount"] = 0
if params.PackageId <= 0 || params.PeriodId <= 0 || params.Count <= 0 {
this.Show()
return
}
// 产品信息
packageResp, err := this.RPC().ADPackageRPC().FindADPackage(this.UserContext(), &pb.FindADPackageRequest{AdPackageId: params.PackageId})
if err != nil {
this.ErrorPage(err)
return
}
var adPackage = packageResp.AdPackage
if adPackage == nil {
this.NotFound("adPackage", params.PackageId)
return
}
this.Data["packageSummary"] = adPackage.Summary
// 日期信息
periodResp, err := this.RPC().ADPackagePeriodRPC().FindADPackagePeriod(this.UserContext(), &pb.FindADPackagePeriodRequest{AdPackagePeriodId: params.PeriodId})
if err != nil {
this.ErrorPage(err)
return
}
var period = periodResp.AdPackagePeriod
if period == nil {
this.NotFound("adPackagePeriod", params.PeriodId)
return
}
this.Data["periodName"] = types.String(period.Count) + userconfigs.ADPackagePeriodUnitName(period.Unit)
// 数量
countInstancesResp, err := this.RPC().ADPackageInstanceRPC().CountIdleADPackageInstances(this.UserContext(), &pb.CountIdleADPackageInstancesRequest{AdPackageId: params.PackageId})
if err != nil {
this.ErrorPage(err)
return
}
var countInstances = types.Int32(countInstancesResp.Count)
if countInstances < params.Count {
this.Show()
return
}
resp, err := this.RPC().ADPackagePriceRPC().FindADPackagePrice(this.UserContext(), &pb.FindADPackagePriceRequest{
AdPackageId: params.PackageId,
AdPackagePeriodId: params.PeriodId,
Count: params.Count,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["amount"] = resp.Amount
this.Show()
}
func (this *ConfirmAction) RunPost(params struct {
PackageId int64
PeriodId int64
Count int32
MethodCode string
Must *actions.Must
CSRF *actionutils.CSRF
}) {
if params.PackageId <= 0 || params.PeriodId <= 0 || params.Count <= 0 {
this.Fail("参数错误,请重新下单")
return
}
periodResp, err := this.RPC().ADPackagePeriodRPC().FindADPackagePeriod(this.UserContext(), &pb.FindADPackagePeriodRequest{AdPackagePeriodId: params.PeriodId})
if err != nil {
this.ErrorPage(err)
return
}
var period = periodResp.AdPackagePeriod
if period == nil {
this.Fail("invalid 'periodId'")
return
}
priceResp, err := this.RPC().ADPackagePriceRPC().FindADPackagePrice(this.UserContext(), &pb.FindADPackagePriceRequest{
AdPackageId: params.PackageId,
AdPackagePeriodId: params.PeriodId,
Count: params.Count,
})
if err != nil {
this.ErrorPage(err)
return
}
var amount = priceResp.Amount
// 使用余额购买
this.Data["success"] = false
this.Data["orderCode"] = ""
if params.MethodCode == "@balance" {
balance, err := financeutils.FindUserBalance(this.UserContext())
if err != nil {
this.ErrorPage(err)
return
}
if amount > balance {
this.Fail("当前余额不足,需要:" + fmt.Sprintf("%.2f元", amount) + ",现有:" + fmt.Sprintf("%.2f元", balance) + " ,请充值后再试")
return
}
// 直接购买
_, err = this.RPC().UserADInstanceRPC().BuyUserADInstance(this.UserContext(), &pb.BuyUserADInstanceRequest{
UserId: this.UserId(),
AdPackageId: params.PackageId,
AdPackagePeriodId: params.PeriodId,
Count: params.Count,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["success"] = true
this.Success()
return
}
// 生成订单
var orderParams = &userconfigs.OrderTypeBuyAntiDDoSInstanceParams{
PackageId: params.PackageId,
PeriodId: params.PeriodId,
PeriodCount: period.Count,
PeriodUnit: period.Unit,
Count: params.Count,
}
orderParamsJSON, err := json.Marshal(orderParams)
if err != nil {
this.ErrorPage(err)
return
}
resp, err := this.RPC().UserOrderRPC().CreateUserOrder(this.UserContext(), &pb.CreateUserOrderRequest{
Type: userconfigs.OrderTypeBuyAntiDDoSInstance,
OrderMethodCode: params.MethodCode,
Amount: amount,
ParamsJSON: orderParamsJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["orderCode"] = resp.Code
this.Success()
}

View File

@@ -0,0 +1,150 @@
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package packages
import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "")
}
func (this *IndexAction) RunGet(params struct{}) {
// 高防产品
var allPackageMap = map[int64]*pb.ADPackage{} // packageId => *pb.ADPackage
packagesResp, err := this.RPC().ADPackageRPC().FindAllIdleADPackages(this.UserContext(), &pb.FindAllIdleADPackagesRequest{})
if err != nil {
this.ErrorPage(err)
return
}
for _, p := range packagesResp.AdPackages {
allPackageMap[p.Id] = p
}
// 价格
pricesResp, err := this.RPC().ADPackagePriceRPC().FindAllADPackagePrices(this.UserContext(), &pb.FindAllADPackagePricesRequest{})
if err != nil {
this.ErrorPage(err)
return
}
var priceMaps = []maps.Map{}
for _, price := range pricesResp.AdPackagePrices {
if price.Price > 0 {
var packageId = price.AdPackageId
var periodId = price.AdPackagePeriodId
p, ok := allPackageMap[packageId]
if !ok {
continue
}
var networkId = p.AdNetworkId
var countIdleInstances = p.CountIdleADPackageInstances
if packageId > 0 && periodId > 0 && networkId > 0 && countIdleInstances > 0 {
priceMaps = append(priceMaps, maps.Map{
"networkId": networkId,
"packageId": packageId,
"protectionBandwidth": types.String(p.ProtectionBandwidthSize) + userconfigs.ADPackageSizeFullUnit(p.ProtectionBandwidthUnit),
"serverBandwidth": types.String(p.ServerBandwidthSize) + userconfigs.ADPackageSizeFullUnit(p.ServerBandwidthUnit),
"periodId": periodId,
"price": price.Price,
"maxInstances": countIdleInstances,
})
}
}
}
this.Data["prices"] = priceMaps
// 重新处理防护带宽和服务带宽
var allProtectionBandwidthSizes = []string{}
var allServerBandwidthSizes = []string{}
for _, p := range packagesResp.AdPackages {
var protectionSize = types.String(p.ProtectionBandwidthSize) + userconfigs.ADPackageSizeFullUnit(p.ProtectionBandwidthUnit)
var serverSize = types.String(p.ServerBandwidthSize) + userconfigs.ADPackageSizeFullUnit(p.ServerBandwidthUnit)
if !lists.ContainsString(allProtectionBandwidthSizes, protectionSize) {
var found = false
for _, price := range priceMaps {
if types.String(price["protectionBandwidth"]) == protectionSize {
found = true
}
}
if found {
allProtectionBandwidthSizes = append(allProtectionBandwidthSizes, protectionSize)
}
}
if !lists.ContainsString(allServerBandwidthSizes, serverSize) {
var found = false
for _, price := range priceMaps {
if types.String(price["serverBandwidth"]) == serverSize {
found = true
}
}
if found {
allServerBandwidthSizes = append(allServerBandwidthSizes, serverSize)
}
}
}
this.Data["allProtectionBandwidthSizes"] = allProtectionBandwidthSizes
this.Data["allServerBandwidthSizes"] = allServerBandwidthSizes
// 线路
var networkMaps = []maps.Map{}
networkResp, err := this.RPC().ADNetworkRPC().FindAllAvailableADNetworks(this.UserContext(), &pb.FindAllAvailableADNetworksRequest{})
if err != nil {
this.ErrorPage(err)
return
}
for _, network := range networkResp.AdNetworks {
var found = false
for _, price := range priceMaps {
if types.Int64(price["networkId"]) == network.Id {
found = true
}
}
if found {
networkMaps = append(networkMaps, maps.Map{
"id": network.Id,
"name": network.Name,
"description": network.Description,
})
}
}
this.Data["allNetworks"] = networkMaps
// 周期
var periodMaps = []maps.Map{}
periodResp, err := this.RPC().ADPackagePeriodRPC().FindAllAvailableADPackagePeriods(this.UserContext(), &pb.FindAllAvailableADPackagePeriodsRequest{})
if err != nil {
this.ErrorPage(err)
return
}
for _, period := range periodResp.AdPackagePeriods {
var found = false
for _, price := range priceMaps {
if types.Int64(price["periodId"]) == period.Id {
found = true
}
}
if found {
periodMaps = append(periodMaps, maps.Map{
"id": period.Id,
"count": period.Count,
"unit": period.Unit,
"unitName": userconfigs.ADPackagePeriodUnitName(period.Unit),
})
}
}
this.Data["allPeriods"] = periodMaps
this.Show()
}

View File

@@ -0,0 +1,54 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package packages
import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/types"
)
type PriceAction struct {
actionutils.ParentAction
}
func (this *PriceAction) RunPost(params struct {
PackageId int64
PeriodId int64
Count int32
}) {
if params.PackageId <= 0 || params.PeriodId <= 0 || params.Count <= 0 {
this.Data["price"] = 0
this.Data["amount"] = 0
this.Success()
return
}
// 数量
countInstancesResp, err := this.RPC().ADPackageInstanceRPC().CountIdleADPackageInstances(this.UserContext(), &pb.CountIdleADPackageInstancesRequest{AdPackageId: params.PackageId})
if err != nil {
this.ErrorPage(err)
return
}
var countInstances = types.Int32(countInstancesResp.Count)
if countInstances < params.Count {
this.Data["price"] = 0
this.Data["amount"] = 0
this.Success()
return
}
resp, err := this.RPC().ADPackagePriceRPC().FindADPackagePrice(this.UserContext(), &pb.FindADPackagePriceRequest{
AdPackageId: params.PackageId,
AdPackagePeriodId: params.PeriodId,
Count: params.Count,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["price"] = resp.Price
this.Data["amount"] = resp.Amount
this.Success()
}