Files
waf-platform/EdgeAdmin/internal/web/actions/default/ns/userPlans/createPopup.go
2026-02-04 20:27:13 +08:00

87 lines
2.1 KiB
Go

// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
//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/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
"regexp"
"strings"
)
type CreatePopupAction struct {
actionutils.ParentAction
}
func (this *CreatePopupAction) Init() {
this.Nav("", "", "")
}
func (this *CreatePopupAction) RunGet(params struct{}) {
// 所有套餐
plansResp, err := this.RPC().NSPlanRPC().FindAllEnabledNSPlans(this.AdminContext(), &pb.FindAllEnabledNSPlansRequest{})
if err != nil {
this.ErrorPage(err)
return
}
var planMaps = []maps.Map{}
for _, plan := range plansResp.NsPlans {
planMaps = append(planMaps, maps.Map{
"id": plan.Id,
"name": plan.Name,
})
}
this.Data["plans"] = planMaps
this.Show()
}
func (this *CreatePopupAction) RunPost(params struct {
UserId int64
PlanId int64
PeriodUnit string
DayTo string
Must *actions.Must
CSRF *actionutils.CSRF
}) {
defer this.CreateLogInfo(codes.NSUserPlan_LogCreateNSUserPlan, params.UserId, params.PlanId)
params.Must.
Field("userId", params.UserId).
Gt(0, "请选择用户").
Field("planId", params.PlanId).
Gt(0, "请选择套餐")
if len(params.DayTo) == 0 {
this.FailField("dayTo", "请选择到期时间")
}
if !regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`).MatchString(params.DayTo) {
this.FailField("dayTo", "请输入正确的到期时间")
}
if params.DayTo < timeutil.Format("Y-m-d") {
this.FailField("dayTo", "过期时间不能小于今天")
}
_, err := this.RPC().NSUserPlanRPC().CreateNSUserPlan(this.AdminContext(), &pb.CreateNSUserPlanRequest{
UserId: params.UserId,
NsPlanId: params.PlanId,
DayFrom: timeutil.Format("Ymd"),
DayTo: strings.ReplaceAll(params.DayTo, "-", ""),
PeriodUnit: params.PeriodUnit,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}