115 lines
3.3 KiB
Go
115 lines
3.3 KiB
Go
// 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/EdgeCommon/pkg/userconfigs"
|
|
"github.com/TeaOSLab/EdgeUser/internal/configloaders"
|
|
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
|
|
"github.com/iwind/TeaGo/maps"
|
|
"github.com/iwind/TeaGo/types"
|
|
)
|
|
|
|
type BuyAction struct {
|
|
actionutils.ParentAction
|
|
}
|
|
|
|
func (this *BuyAction) Init() {
|
|
this.Nav("", "", "buy")
|
|
}
|
|
|
|
func (this *BuyAction) RunGet(params struct{}) {
|
|
// 检查权限
|
|
config, err := configloaders.LoadCacheableUserPriceConfig()
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
if config == nil || !config.IsOn || config.EnablePlans || !config.EnableTrafficPackages {
|
|
this.WriteString("管理员未开启相关配置")
|
|
return
|
|
}
|
|
|
|
// 价格
|
|
pricesResp, err := this.RPC().TrafficPackagePriceRPC().FindAllTrafficPackagePrices(this.UserContext(), &pb.FindAllTrafficPackagePricesRequest{})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
var priceMap = map[string]float64{} // packageId@regionId@periodId => price
|
|
var allPackageIdMap = map[int64]bool{}
|
|
var allRegionIdMap = map[int64]bool{}
|
|
var allPeriodIdMap = map[int64]bool{}
|
|
for _, price := range pricesResp.TrafficPackagePrices {
|
|
if price.Price > 0 {
|
|
allPackageIdMap[price.TrafficPackageId] = true
|
|
allRegionIdMap[price.NodeRegionId] = true
|
|
allPeriodIdMap[price.TrafficPackagePeriodId] = true
|
|
|
|
var key = types.String(price.TrafficPackageId) + "@" + types.String(price.NodeRegionId) + "@" + types.String(price.TrafficPackagePeriodId)
|
|
priceMap[key] = price.Price
|
|
}
|
|
}
|
|
this.Data["prices"] = priceMap
|
|
|
|
// 流量包
|
|
var packageMaps = []maps.Map{}
|
|
packagesResp, err := this.RPC().TrafficPackageRPC().FindAllAvailableTrafficPackages(this.UserContext(), &pb.FindAllAvailableTrafficPackagesRequest{})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
for _, p := range packagesResp.TrafficPackages {
|
|
_, hasPrice := allPackageIdMap[p.Id]
|
|
if hasPrice {
|
|
packageMaps = append(packageMaps, maps.Map{
|
|
"id": p.Id,
|
|
"size": p.Size,
|
|
"unit": p.Unit,
|
|
})
|
|
}
|
|
}
|
|
this.Data["packages"] = packageMaps
|
|
|
|
// 区域
|
|
var regionMaps = []maps.Map{}
|
|
regionsResp, err := this.RPC().NodeRegionRPC().FindAllAvailableNodeRegions(this.UserContext(), &pb.FindAllAvailableNodeRegionsRequest{})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
for _, region := range regionsResp.NodeRegions {
|
|
_, hasPrice := allRegionIdMap[region.Id]
|
|
if hasPrice {
|
|
regionMaps = append(regionMaps, maps.Map{
|
|
"id": region.Id,
|
|
"name": region.Name,
|
|
})
|
|
}
|
|
}
|
|
this.Data["regions"] = regionMaps
|
|
|
|
// 周期
|
|
var periodMaps = []maps.Map{}
|
|
periodResp, err := this.RPC().TrafficPackagePeriodRPC().FindAllAvailableTrafficPackagePeriods(this.UserContext(), &pb.FindAllAvailableTrafficPackagePeriodsRequest{})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
for _, period := range periodResp.TrafficPackagePeriods {
|
|
_, hasPrice := allPeriodIdMap[period.Id]
|
|
if hasPrice {
|
|
periodMaps = append(periodMaps, maps.Map{
|
|
"id": period.Id,
|
|
"count": period.Count,
|
|
"unit": period.Unit,
|
|
"unitName": userconfigs.TrafficPackagePeriodUnitName(period.Unit),
|
|
})
|
|
}
|
|
}
|
|
this.Data["periods"] = periodMaps
|
|
|
|
this.Show()
|
|
}
|