252 lines
6.6 KiB
Go
252 lines
6.6 KiB
Go
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
|
|
package packages
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"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"
|
|
"github.com/TeaOSLab/EdgeUser/internal/web/actions/default/finance/financeutils"
|
|
"github.com/iwind/TeaGo/types"
|
|
"strings"
|
|
)
|
|
|
|
type ConfirmAction struct {
|
|
actionutils.ParentAction
|
|
}
|
|
|
|
func (this *ConfirmAction) Init() {
|
|
this.Nav("", "", "buy")
|
|
}
|
|
|
|
func (this *ConfirmAction) RunGet(params struct {
|
|
PackageId int64
|
|
RegionId int64
|
|
PeriodId int64
|
|
Count int32
|
|
}) {
|
|
// parameters
|
|
this.Data["packageId"] = params.PackageId
|
|
this.Data["regionId"] = params.RegionId
|
|
this.Data["periodId"] = params.PeriodId
|
|
|
|
// 检查权限
|
|
config, err := configloaders.LoadCacheableUserPriceConfig()
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
if config == nil || !config.IsOn || config.EnablePlans || !config.EnableTrafficPackages {
|
|
this.WriteString("管理员未开启相关配置")
|
|
return
|
|
}
|
|
|
|
// package
|
|
packageResp, err := this.RPC().TrafficPackageRPC().FindTrafficPackage(this.UserContext(), &pb.FindTrafficPackageRequest{TrafficPackageId: params.PackageId})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
var p = packageResp.TrafficPackage
|
|
if p == nil || !p.IsOn {
|
|
this.NotFound("TrafficPackage", params.PackageId)
|
|
return
|
|
}
|
|
this.Data["packageName"] = types.String(p.Size) + strings.ToUpper(p.Unit)
|
|
|
|
// region
|
|
regionResp, err := this.RPC().NodeRegionRPC().FindEnabledNodeRegion(this.UserContext(), &pb.FindEnabledNodeRegionRequest{NodeRegionId: params.RegionId})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
var region = regionResp.NodeRegion
|
|
if region == nil || !region.IsOn {
|
|
this.NotFound("NodeRegion", params.RegionId)
|
|
return
|
|
}
|
|
this.Data["regionName"] = region.Name
|
|
|
|
// period
|
|
periodResp, err := this.RPC().TrafficPackagePeriodRPC().FindTrafficPackagePeriod(this.UserContext(), &pb.FindTrafficPackagePeriodRequest{TrafficPackagePeriodId: params.PeriodId})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
var period = periodResp.TrafficPackagePeriod
|
|
if period == nil || !period.IsOn {
|
|
this.NotFound("TrafficPackagePeriod", params.PeriodId)
|
|
return
|
|
}
|
|
this.Data["periodName"] = types.String(period.Count) + userconfigs.TrafficPackagePeriodUnitName(period.Unit)
|
|
|
|
// count
|
|
if params.Count <= 0 {
|
|
this.ErrorPage(errors.New("invalid 'count' parameter"))
|
|
return
|
|
}
|
|
this.Data["count"] = params.Count
|
|
|
|
// price
|
|
priceResp, err := this.RPC().TrafficPackagePriceRPC().FindTrafficPackagePrice(this.UserContext(), &pb.FindTrafficPackagePriceRequest{
|
|
TrafficPackageId: params.PackageId,
|
|
NodeRegionId: params.RegionId,
|
|
TrafficPackagePeriodId: params.PeriodId,
|
|
Count: params.Count,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
if priceResp.Price == 0 {
|
|
this.ErrorPage(errors.New("invalid parameters: could not retrieve price"))
|
|
return
|
|
}
|
|
this.Data["amount"] = priceResp.Amount
|
|
|
|
this.Show()
|
|
}
|
|
|
|
func (this *ConfirmAction) RunPost(params struct {
|
|
PackageId int64
|
|
RegionId int64
|
|
PeriodId int64
|
|
Count int32
|
|
|
|
MethodCode string
|
|
}) {
|
|
// 检查权限
|
|
config, err := configloaders.LoadCacheableUserPriceConfig()
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
if config == nil || !config.IsOn || config.EnablePlans || !config.EnableTrafficPackages {
|
|
this.Fail("管理员未开启相关配置")
|
|
return
|
|
}
|
|
|
|
// 检查参数
|
|
if params.PackageId <= 0 {
|
|
this.Fail("请选择流量包规格")
|
|
return
|
|
}
|
|
if params.RegionId <= 0 {
|
|
this.Fail("请选择区域")
|
|
return
|
|
}
|
|
if params.PeriodId <= 0 {
|
|
this.Fail("请选择有效期")
|
|
return
|
|
}
|
|
if params.Count <= 0 {
|
|
this.Fail("请选择数量")
|
|
return
|
|
}
|
|
if params.Count > 20 {
|
|
this.Fail("一次性最多只能购买20个")
|
|
return
|
|
}
|
|
|
|
// period
|
|
periodResp, err := this.RPC().TrafficPackagePeriodRPC().FindTrafficPackagePeriod(this.UserContext(), &pb.FindTrafficPackagePeriodRequest{TrafficPackagePeriodId: params.PeriodId})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
var period = periodResp.TrafficPackagePeriod
|
|
if period == nil || !period.IsOn {
|
|
this.NotFound("TrafficPackagePeriod", params.PeriodId)
|
|
return
|
|
}
|
|
|
|
// 检查价格
|
|
priceResp, err := this.RPC().TrafficPackagePriceRPC().FindTrafficPackagePrice(this.UserContext(), &pb.FindTrafficPackagePriceRequest{
|
|
TrafficPackageId: params.PackageId,
|
|
NodeRegionId: params.RegionId,
|
|
TrafficPackagePeriodId: params.PeriodId,
|
|
Count: params.Count,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
if priceResp.Amount <= 0 {
|
|
this.Fail("当前流量包没有设定价格,无法购买")
|
|
return
|
|
}
|
|
var amount = priceResp.Amount
|
|
|
|
this.Data["packageId"] = params.PackageId
|
|
this.Data["regionId"] = params.RegionId
|
|
this.Data["periodId"] = params.PeriodId
|
|
this.Data["count"] = params.Count
|
|
|
|
// 使用余额购买
|
|
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().UserTrafficPackageRPC().BuyUserTrafficPackage(this.UserContext(), &pb.BuyUserTrafficPackageRequest{
|
|
UserId: this.UserId(),
|
|
TrafficPackageId: params.PackageId,
|
|
NodeRegionId: params.RegionId,
|
|
TrafficPackagePeriodId: params.PeriodId,
|
|
Count: params.Count,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
this.Data["success"] = true
|
|
this.Success()
|
|
return
|
|
}
|
|
|
|
// 生成订单
|
|
var orderParams = &userconfigs.OrderTypeBuyTrafficPackageParams{
|
|
PackageId: params.PackageId,
|
|
RegionId: params.RegionId,
|
|
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.OrderTypeBuyTrafficPackage,
|
|
OrderMethodCode: params.MethodCode,
|
|
Amount: amount,
|
|
ParamsJSON: orderParamsJSON,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
this.Data["orderCode"] = resp.Code
|
|
|
|
this.Success()
|
|
}
|