Files
waf-platform/EdgeUser/internal/web/actions/default/servers/fee/updatePricePeriodPopup.go
2026-02-04 20:27:13 +08:00

90 lines
2.4 KiB
Go

// 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()
}