Initial commit (code only without large binaries)
This commit is contained in:
204
EdgeUser/internal/web/actions/default/servers/fee/prices.go
Normal file
204
EdgeUser/internal/web/actions/default/servers/fee/prices.go
Normal file
@@ -0,0 +1,204 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package fee
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/utils/numberutils"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
)
|
||||
|
||||
type PricesAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *PricesAction) Init() {
|
||||
this.Nav("", "", "price")
|
||||
}
|
||||
|
||||
func (this *PricesAction) RunGet(params struct{}) {
|
||||
config, err := configloaders.LoadCacheableUserPriceConfig()
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
if config == nil || !config.IsOn || !config.UserUI.ShowPrices {
|
||||
this.WriteString("管理员未开启此配置")
|
||||
return
|
||||
}
|
||||
|
||||
this.Data["showPrices"] = true
|
||||
|
||||
// 初始化
|
||||
this.Data["regions"] = []maps.Map{}
|
||||
this.Data["bandwidthItems"] = []maps.Map{}
|
||||
this.Data["trafficItems"] = []maps.Map{}
|
||||
|
||||
var bandwidthItemIds = []int64{}
|
||||
var trafficItemIds = []int64{}
|
||||
|
||||
// 带宽价格
|
||||
var supportRegions = false
|
||||
var supportBandwidth = false
|
||||
if (config.UserCanChangePriceType || config.DefaultPriceType == userconfigs.PriceTypeBandwidth) && config.DefaultBandwidthPriceConfig != nil {
|
||||
supportBandwidth = true
|
||||
this.Data["bandwidthPrices"] = maps.Map{
|
||||
"percentile": config.DefaultBandwidthPriceConfig.Percentile,
|
||||
"base": config.DefaultBandwidthPriceConfig.Base,
|
||||
"ranges": config.DefaultBandwidthPriceConfig.Ranges,
|
||||
"supportRegions": config.DefaultBandwidthPriceConfig.SupportRegions,
|
||||
}
|
||||
|
||||
if config.DefaultBandwidthPriceConfig.SupportRegions {
|
||||
supportRegions = true
|
||||
|
||||
bandwidthItemIds, err = this.bandwidthPriceItems()
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
this.Data["supportBandwidth"] = supportBandwidth
|
||||
|
||||
// 流量价格
|
||||
var supportTraffic = false
|
||||
if (config.UserCanChangePriceType || config.DefaultPriceType == userconfigs.PriceTypeTraffic) && config.DefaultTrafficPriceConfig != nil {
|
||||
supportTraffic = true
|
||||
this.Data["trafficPrices"] = maps.Map{
|
||||
"base": config.DefaultTrafficPriceConfig.Base,
|
||||
"ranges": config.DefaultTrafficPriceConfig.Ranges,
|
||||
"supportRegions": config.DefaultTrafficPriceConfig.SupportRegions,
|
||||
}
|
||||
if config.DefaultTrafficPriceConfig.SupportRegions {
|
||||
supportRegions = true
|
||||
|
||||
trafficItemIds, err = this.trafficPriceItems()
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
this.Data["supportTraffic"] = supportTraffic
|
||||
|
||||
// 支付方式
|
||||
this.Data["priceType"] = config.DefaultPriceType
|
||||
|
||||
// 区域
|
||||
if supportRegions {
|
||||
// 所有区域
|
||||
regionsResp, err := this.RPC().NodeRegionRPC().FindAllAvailableNodeRegions(this.UserContext(), &pb.FindAllAvailableNodeRegionsRequest{})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var regionMaps = []maps.Map{}
|
||||
for _, region := range regionsResp.NodeRegions {
|
||||
var pricesMap = map[int64]float32{}
|
||||
if len(region.PricesJSON) > 0 {
|
||||
err = json.Unmarshal(region.PricesJSON, &pricesMap)
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 跳过没有设置的
|
||||
if len(pricesMap) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// 支持的计费方式
|
||||
var regionSupportBandwidth = false
|
||||
var regionSupportTraffic = false
|
||||
|
||||
for itemId := range pricesMap {
|
||||
if lists.ContainsInt64(bandwidthItemIds, itemId) {
|
||||
regionSupportBandwidth = true
|
||||
}
|
||||
if lists.ContainsInt64(trafficItemIds, itemId) {
|
||||
regionSupportTraffic = true
|
||||
}
|
||||
}
|
||||
|
||||
if regionSupportBandwidth || regionSupportTraffic {
|
||||
regionMaps = append(regionMaps, maps.Map{
|
||||
"id": region.Id,
|
||||
"isOn": region.IsOn,
|
||||
"name": region.Name,
|
||||
"prices": pricesMap,
|
||||
"supportBandwidth": regionSupportBandwidth,
|
||||
"supportTraffic": regionSupportTraffic,
|
||||
})
|
||||
}
|
||||
}
|
||||
this.Data["regions"] = regionMaps
|
||||
}
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *PricesAction) bandwidthPriceItems() (itemIds []int64, err error) {
|
||||
// 所有价格项目
|
||||
itemsResp, err := this.RPC().NodePriceItemRPC().FindAllAvailableNodePriceItems(this.UserContext(), &pb.FindAllAvailableNodePriceItemsRequest{Type: userconfigs.PriceTypeBandwidth})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var itemMaps = []maps.Map{}
|
||||
for _, item := range itemsResp.NodePriceItems {
|
||||
itemIds = append(itemIds, item.Id)
|
||||
|
||||
var maxSize string
|
||||
if item.BitsTo == 0 {
|
||||
maxSize = "∞"
|
||||
} else {
|
||||
maxSize = numberutils.FormatBits(item.BitsTo)
|
||||
}
|
||||
|
||||
itemMaps = append(itemMaps, maps.Map{
|
||||
"id": item.Id,
|
||||
"name": item.Name,
|
||||
"minSize": numberutils.FormatBits(item.BitsFrom),
|
||||
"maxSize": maxSize,
|
||||
})
|
||||
}
|
||||
this.Data["bandwidthItems"] = itemMaps
|
||||
|
||||
return itemIds, nil
|
||||
}
|
||||
|
||||
func (this *PricesAction) trafficPriceItems() (itemIds []int64, err error) {
|
||||
// 所有价格项目
|
||||
itemsResp, err := this.RPC().NodePriceItemRPC().FindAllAvailableNodePriceItems(this.UserContext(), &pb.FindAllAvailableNodePriceItemsRequest{Type: userconfigs.PriceTypeTraffic})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var itemMaps = []maps.Map{}
|
||||
for _, item := range itemsResp.NodePriceItems {
|
||||
itemIds = append(itemIds, item.Id)
|
||||
|
||||
var maxSize string
|
||||
if item.BitsTo == 0 {
|
||||
maxSize = "∞"
|
||||
} else {
|
||||
maxSize = numberutils.FormatBytes(item.BitsTo / 8)
|
||||
}
|
||||
|
||||
itemMaps = append(itemMaps, maps.Map{
|
||||
"id": item.Id,
|
||||
"name": item.Name,
|
||||
"minSize": numberutils.FormatBytes(item.BitsFrom / 8),
|
||||
"maxSize": maxSize,
|
||||
})
|
||||
}
|
||||
this.Data["trafficItems"] = itemMaps
|
||||
|
||||
return itemIds, nil
|
||||
}
|
||||
Reference in New Issue
Block a user