207 lines
5.6 KiB
Go
207 lines
5.6 KiB
Go
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||
//go:build plus
|
||
|
||
package userPlans
|
||
|
||
import (
|
||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||
"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
|
||
}) {
|
||
userPlanResp, err := this.RPC().UserPlanRPC().FindEnabledUserPlan(this.AdminContext(), &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.AdminContext(), &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,
|
||
}
|
||
|
||
// 用户
|
||
userResp, err := this.RPC().UserRPC().FindEnabledUser(this.AdminContext(), &pb.FindEnabledUserRequest{UserId: userId})
|
||
if err != nil {
|
||
this.ErrorPage(err)
|
||
return
|
||
}
|
||
var user = userResp.User
|
||
if user == nil {
|
||
this.Fail("找不到套餐所属用户")
|
||
}
|
||
this.Data["user"] = maps.Map{
|
||
"id": user.Id,
|
||
"username": user.Username,
|
||
"fullname": user.Fullname,
|
||
}
|
||
|
||
// 账户
|
||
accountResp, err := this.RPC().UserAccountRPC().FindEnabledUserAccountWithUserId(this.AdminContext(), &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
|
||
}) {
|
||
defer this.CreateLogInfo(codes.UserPlan_LogRenewUserPlan, params.UserPlanId)
|
||
|
||
userPlanResp, err := this.RPC().UserPlanRPC().FindEnabledUserPlan(this.AdminContext(), &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.AdminContext(), &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.AdminContext(), &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.AdminContext(), &pb.RenewUserPlanRequest{
|
||
UserPlanId: params.UserPlanId,
|
||
DayTo: params.DayTo,
|
||
Period: params.Period,
|
||
CountPeriod: countPeriod,
|
||
})
|
||
if err != nil {
|
||
this.ErrorPage(err)
|
||
return
|
||
}
|
||
|
||
this.Success()
|
||
}
|