Initial commit (code only without large binaries)
This commit is contained in:
@@ -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()
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user