This commit is contained in:
unknown
2026-02-04 20:27:13 +08:00
commit 3b042d1dad
9410 changed files with 1488147 additions and 0 deletions

View File

@@ -0,0 +1,141 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package fee
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/utils/numberutils"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/maps"
)
type CalculatorAction struct {
actionutils.ParentAction
}
func (this *CalculatorAction) Init() {
this.Nav("", "", "calculator")
}
func (this *CalculatorAction) 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
// 配置
// 仅显示有限的配置,防止信息泄露
var priceType = config.DefaultPriceType
var canChangePriceType = false
if config.UserCanChangePriceType {
canChangePriceType = true
priceInfoResp, err := this.RPC().UserRPC().FindUserPriceInfo(this.UserContext(), &pb.FindUserPriceInfoRequest{UserId: this.UserId()})
if err != nil {
this.ErrorPage(err)
return
}
if len(priceInfoResp.PriceType) > 0 && userconfigs.IsValidPriceType(priceInfoResp.PriceType) {
priceType = priceInfoResp.PriceType
}
}
this.Data["config"] = maps.Map{
"priceType": priceType,
"canChangePriceType": canChangePriceType,
}
// 所有区域
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 {
regionMaps = append(regionMaps, maps.Map{
"id": region.Id,
"name": region.Name,
})
}
this.Data["regions"] = regionMaps
this.Show()
}
func (this *CalculatorAction) RunPost(params struct {
PriceType string
Traffic float64
TrafficUnit string
Bandwidth float64
BandwidthUnit string
RegionId int64
}) {
config, err := configloaders.LoadCacheableUserPriceConfig()
if err != nil {
this.ErrorPage(err)
return
}
if config == nil || !config.UserUI.ShowPrices {
this.Fail("管理员未开启此配置")
return
}
var trafficGB float64 = 0
var bandwidthMB float64 = 0
switch params.PriceType {
case userconfigs.PriceTypeTraffic:
switch params.TrafficUnit {
case "gb":
trafficGB = params.Traffic
case "tb":
trafficGB = params.Traffic * (1 << 10)
case "eb":
trafficGB = params.Traffic * (1 << 20)
}
case userconfigs.PriceTypeBandwidth:
switch params.BandwidthUnit {
case "mb":
bandwidthMB = params.Bandwidth
case "gb":
bandwidthMB = params.Bandwidth * (1 << 10)
case "tb":
bandwidthMB = params.Bandwidth * (1 << 20)
}
default:
this.Fail("请选择正确的计费类型")
return
}
resp, err := this.RPC().PriceRPC().CalculatePrice(this.UserContext(), &pb.CalculatePriceRequest{
PriceType: params.PriceType,
TrafficGB: trafficGB,
BandwidthMB: bandwidthMB,
NodeRegionId: params.RegionId,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["amount"] = resp.Amount
this.Data["amountFormatted"] = numberutils.FormatFloat(resp.Amount, 2)
this.Data["hasRegionPrice"] = resp.HasNodeRegionPrice
this.Success()
}

View File

@@ -0,0 +1,70 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package fee
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"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "index")
}
func (this *IndexAction) RunGet(params struct{}) {
config, err := configloaders.LoadCacheableUserPriceConfig()
if err != nil {
this.ErrorPage(err)
return
}
if config == nil || !config.IsOn {
this.WriteString("管理员未开启此配置")
return
}
this.Data["showPrices"] = config.UserUI.ShowPrices
// 配置
// 仅显示有限的配置,防止信息泄露
var priceType = config.DefaultPriceType
var pricePeriod = config.DefaultPricePeriod
priceInfoResp, err := this.RPC().UserRPC().FindUserPriceInfo(this.UserContext(), &pb.FindUserPriceInfoRequest{UserId: this.UserId()})
if err != nil {
this.ErrorPage(err)
return
}
// 计费方式
if config.UserCanChangePriceType {
if len(priceInfoResp.PriceType) > 0 && userconfigs.IsValidPriceType(priceInfoResp.PriceType) {
priceType = priceInfoResp.PriceType
}
}
// 计费周期
if config.UserCanChangePricePeriod {
if len(priceInfoResp.PricePeriod) > 0 && userconfigs.IsValidPricePeriod(priceInfoResp.PricePeriod) {
pricePeriod = priceInfoResp.PricePeriod
}
}
this.Data["config"] = maps.Map{
"priceType": priceType,
"priceTypeName": userconfigs.PriceTypeName(priceType),
"pricePeriod": pricePeriod,
"pricePeriodName": userconfigs.PricePeriodName(pricePeriod),
"canChangeType": config.UserCanChangePriceType,
"canChangePeriod": config.UserCanChangePricePeriod,
}
this.Show()
}

View File

@@ -0,0 +1,24 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package fee
import (
"github.com/TeaOSLab/EdgeUser/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth("")).
Data("teaMenu", "servers").
Data("teaSubMenu", "fee").
Prefix("/servers/fee").
Get("", new(IndexAction)).
GetPost("/updatePriceTypePopup", new(UpdatePriceTypePopupAction)).
GetPost("/updatePricePeriodPopup", new(UpdatePricePeriodPopupAction)).
Get("/prices", new(PricesAction)).
GetPost("/calculator", new(CalculatorAction)).
EndAll()
})
}

View 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
}

View File

@@ -0,0 +1,89 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package fee
import (
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"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"
)
type UpdatePricePeriodPopupAction struct {
actionutils.ParentAction
}
func (this *UpdatePricePeriodPopupAction) RunGet(params struct {
}) {
config, err := configloaders.LoadCacheableUserPriceConfig()
if err != nil {
this.ErrorPage(err)
return
}
if config == nil || !config.IsOn || !config.UserCanChangePricePeriod {
this.WriteString("管理员未开启此配置")
return
}
var pricePeriod = config.DefaultPricePeriod
userPriceInfo, err := this.RPC().UserRPC().FindUserPriceInfo(this.UserContext(), &pb.FindUserPriceInfoRequest{UserId: this.UserId()})
if err != nil {
this.ErrorPage(err)
return
}
if len(userPriceInfo.PricePeriod) > 0 && userconfigs.IsValidPricePeriod(userPriceInfo.PricePeriod) {
pricePeriod = userPriceInfo.PricePeriod
}
this.Data["pricePeriod"] = pricePeriod
this.Data["pricePeriods"] = []maps.Map{
{
"code": userconfigs.PricePeriodMonthly,
"name": "按月",
"description": "按自然月计费,每个月出上一个月的账单。",
},
{
"code": userconfigs.PricePeriodDaily,
"name": "按日",
"description": "按自然日计费,每天出上一天的账单。",
},
}
this.Show()
}
func (this *UpdatePricePeriodPopupAction) RunPost(params struct {
PricePeriod string
CSRF *actionutils.CSRF
}) {
defer this.CreateLogInfo(codes.User_LogUpdateUserPricePeriod, params.PricePeriod)
config, err := configloaders.LoadCacheableUserPriceConfig()
if err != nil {
this.ErrorPage(err)
return
}
if config == nil || !config.IsOn || !config.UserCanChangePricePeriod {
this.Fail("管理员未开启此配置")
}
if !userconfigs.IsValidPricePeriod(params.PricePeriod) {
this.Fail("计费方式错误:'" + params.PricePeriod + "'")
}
_, err = this.RPC().UserRPC().UpdateUserPricePeriod(this.UserContext(), &pb.UpdateUserPricePeriodRequest{
UserId: this.UserId(),
PricePeriod: params.PricePeriod,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}

View File

@@ -0,0 +1,92 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package fee
import (
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"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 UpdatePriceTypePopupAction struct {
actionutils.ParentAction
}
func (this *UpdatePriceTypePopupAction) RunGet(params struct {
}) {
config, err := configloaders.LoadCacheableUserPriceConfig()
if err != nil {
this.ErrorPage(err)
return
}
if config == nil || !config.IsOn || !config.UserCanChangePriceType || config.DefaultBandwidthPriceConfig == nil {
this.WriteString("管理员未开启此配置")
return
}
var percentile = config.DefaultBandwidthPriceConfig.Percentile
var priceType = config.DefaultPriceType
userPriceInfo, err := this.RPC().UserRPC().FindUserPriceInfo(this.UserContext(), &pb.FindUserPriceInfoRequest{UserId: this.UserId()})
if err != nil {
this.ErrorPage(err)
return
}
if len(userPriceInfo.PriceType) > 0 && userconfigs.IsValidPriceType(userPriceInfo.PriceType) {
priceType = userPriceInfo.PriceType
}
this.Data["priceType"] = priceType
this.Data["priceTypes"] = []maps.Map{
{
"code": userconfigs.PriceTypeBandwidth,
"name": "带宽",
"description": "使用在计费周期内的" + types.String(percentile) + "th带宽计费。",
},
{
"code": userconfigs.PriceTypeTraffic,
"name": "流量",
"description": "使用在计费周期内产生的流量总和进行计费。",
},
}
this.Show()
}
func (this *UpdatePriceTypePopupAction) RunPost(params struct {
PriceType string
CSRF *actionutils.CSRF
}) {
defer this.CreateLogInfo(codes.User_LogUpdateUserPriceType, params.PriceType)
config, err := configloaders.LoadCacheableUserPriceConfig()
if err != nil {
this.ErrorPage(err)
return
}
if config == nil || !config.IsOn || !config.UserCanChangePriceType {
this.Fail("管理员未开启此配置")
}
if !userconfigs.IsValidPriceType(params.PriceType) {
this.Fail("计费方式错误:'" + params.PriceType + "'")
}
_, err = this.RPC().UserRPC().UpdateUserPriceType(this.UserContext(), &pb.UpdateUserPriceTypeRequest{
UserId: this.UserId(),
PriceType: params.PriceType,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}