205 lines
5.5 KiB
Go
205 lines
5.5 KiB
Go
// 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()
|
|
}
|