191 lines
4.9 KiB
Go
191 lines
4.9 KiB
Go
// 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()
|
|
}
|