1.4.5.2
This commit is contained in:
225
EdgeUser/internal/web/actions/default/plans/renewPopup.go
Normal file
225
EdgeUser/internal/web/actions/default/plans/renewPopup.go
Normal file
@@ -0,0 +1,225 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package plans
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RenewPopupAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *RenewPopupAction) Init() {
|
||||
this.Nav("", "", "")
|
||||
}
|
||||
|
||||
func (this *RenewPopupAction) RunGet(params struct {
|
||||
UserPlanId int64
|
||||
}) {
|
||||
if !this.ValidateFeature(userconfigs.UserFeatureCodePlan, 0) {
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否可以操作套餐
|
||||
{
|
||||
priceConfig, err := configloaders.LoadUserPriceConfig()
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if priceConfig != nil && !priceConfig.ShowPlansInUserSystem {
|
||||
this.ForbidPage()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
userPlanResp, err := this.RPC().UserPlanRPC().FindEnabledUserPlan(this.UserContext(), &pb.FindEnabledUserPlanRequest{UserPlanId: params.UserPlanId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var userPlan = userPlanResp.UserPlan
|
||||
if userPlan == nil {
|
||||
this.Fail("找不到要修改的套餐")
|
||||
}
|
||||
var planId = userPlan.PlanId
|
||||
var userId = userPlan.UserId
|
||||
|
||||
this.Data["userPlanId"] = params.UserPlanId
|
||||
|
||||
// 套餐
|
||||
planResp, err := this.RPC().PlanRPC().FindEnabledPlan(this.UserContext(), &pb.FindEnabledPlanRequest{PlanId: planId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var plan = planResp.Plan
|
||||
if plan == nil {
|
||||
this.Fail("找不到要购买的套餐")
|
||||
}
|
||||
this.Data["plan"] = maps.Map{
|
||||
"id": plan.Id,
|
||||
"name": plan.Name,
|
||||
"priceType": plan.PriceType,
|
||||
"monthlyPrice": plan.MonthlyPrice,
|
||||
"seasonallyPrice": plan.SeasonallyPrice,
|
||||
"yearlyPrice": plan.YearlyPrice,
|
||||
}
|
||||
|
||||
// 账户
|
||||
accountResp, err := this.RPC().UserAccountRPC().FindEnabledUserAccountWithUserId(this.UserContext(), &pb.FindEnabledUserAccountWithUserIdRequest{UserId: userId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var account = accountResp.UserAccount
|
||||
if account == nil {
|
||||
this.Fail("找不到用户'" + types.String(userId) + "'的账户")
|
||||
}
|
||||
this.Data["userAccount"] = maps.Map{"total": account.Total}
|
||||
|
||||
// 默认结束日期
|
||||
var hasDefaultDayTo = false
|
||||
if len(userPlan.DayTo) > 0 {
|
||||
// 如果已经有日期,则自动增加1年
|
||||
var reg = regexp.MustCompile(`^(\d+)-(\d+)-(\d+)$`)
|
||||
if reg.MatchString(userPlan.DayTo) {
|
||||
var matches = reg.FindStringSubmatch(userPlan.DayTo)
|
||||
var year = types.Int(matches[1])
|
||||
var month = types.Int(matches[2])
|
||||
var day = types.Int(matches[3])
|
||||
if year > 1970 {
|
||||
this.Data["defaultDayTo"] = timeutil.Format("Y-m-d", time.Date(year+1, time.Month(month), day, 0, 0, 0, 0, time.Local))
|
||||
hasDefaultDayTo = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if !hasDefaultDayTo {
|
||||
this.Data["defaultDayTo"] = timeutil.Format("Y-m-d", time.Now().AddDate(20, 0, 0))
|
||||
}
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *RenewPopupAction) RunPost(params struct {
|
||||
UserPlanId int64
|
||||
|
||||
Period string
|
||||
CountMonths int32
|
||||
CountSeasons int32
|
||||
CountYears int32
|
||||
|
||||
DayTo string
|
||||
|
||||
Must *actions.Must
|
||||
CSRF *actionutils.CSRF
|
||||
}) {
|
||||
if !this.ValidateFeature(userconfigs.UserFeatureCodePlan, 0) {
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否可以操作套餐
|
||||
{
|
||||
priceConfig, err := configloaders.LoadUserPriceConfig()
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if priceConfig != nil && !priceConfig.ShowPlansInUserSystem {
|
||||
this.ForbidPage()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
defer this.CreateLogInfo(codes.UserPlan_LogRenewUserPlan, params.UserPlanId)
|
||||
|
||||
userPlanResp, err := this.RPC().UserPlanRPC().FindEnabledUserPlan(this.UserContext(), &pb.FindEnabledUserPlanRequest{UserPlanId: params.UserPlanId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var userPlan = userPlanResp.UserPlan
|
||||
if userPlan == nil {
|
||||
this.Fail("找不到要修改的套餐")
|
||||
}
|
||||
|
||||
var planId = userPlan.PlanId
|
||||
var userId = userPlan.UserId
|
||||
|
||||
// 检查余额
|
||||
planResp, err := this.RPC().PlanRPC().FindEnabledPlan(this.UserContext(), &pb.FindEnabledPlanRequest{PlanId: planId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var plan = planResp.Plan
|
||||
if plan == nil {
|
||||
this.Fail("找不到要购买的套餐")
|
||||
}
|
||||
|
||||
var countPeriod int32
|
||||
if plan.PriceType == serverconfigs.PlanPriceTypePeriod {
|
||||
var cost float64
|
||||
|
||||
switch params.Period {
|
||||
case "monthly":
|
||||
countPeriod = params.CountMonths
|
||||
cost = plan.MonthlyPrice * float64(countPeriod)
|
||||
case "seasonally":
|
||||
countPeriod = params.CountSeasons
|
||||
cost = plan.SeasonallyPrice * float64(countPeriod)
|
||||
case "yearly":
|
||||
countPeriod = params.CountYears
|
||||
cost = plan.YearlyPrice * float64(countPeriod)
|
||||
default:
|
||||
this.Fail("invalid period '" + params.Period + "'")
|
||||
}
|
||||
|
||||
// 账号
|
||||
accountResp, err := this.RPC().UserAccountRPC().FindEnabledUserAccountWithUserId(this.UserContext(), &pb.FindEnabledUserAccountWithUserIdRequest{UserId: userId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var account = accountResp.UserAccount
|
||||
if account == nil {
|
||||
this.Fail("找不到用户'" + types.String(userId) + "'的账户")
|
||||
}
|
||||
if account.Total < cost {
|
||||
this.Fail("用户账户余额不足")
|
||||
}
|
||||
} else if plan.PriceType == serverconfigs.PlanPriceTypeTraffic {
|
||||
params.Must.Field("dayTo", params.DayTo).
|
||||
Match(`^\d+-\d+-\d+$`, "请输入正确的结束日期")
|
||||
} else if plan.PriceType == serverconfigs.PlanPriceTypeBandwidth {
|
||||
params.Must.Field("dayTo", params.DayTo).
|
||||
Match(`^\d+-\d+-\d+$`, "请输入正确的结束日期")
|
||||
} else {
|
||||
this.Fail("不支持的付款方式:" + plan.PriceType)
|
||||
}
|
||||
|
||||
_, err = this.RPC().UserPlanRPC().RenewUserPlan(this.UserContext(), &pb.RenewUserPlanRequest{
|
||||
UserPlanId: params.UserPlanId,
|
||||
DayTo: params.DayTo,
|
||||
Period: params.Period,
|
||||
CountPeriod: countPeriod,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
Reference in New Issue
Block a user