130 lines
3.4 KiB
Go
130 lines
3.4 KiB
Go
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
|
|
package instances
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
"github.com/TeaOSLab/EdgeUser/internal/utils/dateutils"
|
|
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
|
|
"github.com/iwind/TeaGo/actions"
|
|
"github.com/iwind/TeaGo/maps"
|
|
timeutil "github.com/iwind/TeaGo/utils/time"
|
|
)
|
|
|
|
type UpdateObjectsPopupAction struct {
|
|
actionutils.ParentAction
|
|
}
|
|
|
|
func (this *UpdateObjectsPopupAction) Init() {
|
|
this.Nav("", "", "")
|
|
}
|
|
|
|
func (this *UpdateObjectsPopupAction) RunGet(params struct {
|
|
UserInstanceId int64
|
|
}) {
|
|
userInstanceResp, err := this.RPC().UserADInstanceRPC().FindUserADInstance(this.UserContext(), &pb.FindUserADInstanceRequest{UserADInstanceId: params.UserInstanceId})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
var userInstance = userInstanceResp.UserADInstance
|
|
if userInstance == nil {
|
|
this.NotFound("userInstance", params.UserInstanceId)
|
|
return
|
|
}
|
|
var objectMaps = []maps.Map{}
|
|
if len(userInstance.ObjectsJSON) > 0 {
|
|
err = json.Unmarshal(userInstance.ObjectsJSON, &objectMaps)
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
}
|
|
|
|
// 用户
|
|
var userMap = maps.Map{
|
|
"id": 0,
|
|
}
|
|
if userInstance.User != nil {
|
|
userMap = maps.Map{
|
|
"id": userInstance.User.Id,
|
|
"fullname": userInstance.User.Fullname,
|
|
"username": userInstance.User.Username,
|
|
}
|
|
}
|
|
|
|
// 实例
|
|
var instanceMap = maps.Map{
|
|
"id": 0,
|
|
}
|
|
if userInstance.AdPackageInstance != nil {
|
|
if userInstance.AdPackageInstance.IpAddresses == nil {
|
|
userInstance.AdPackageInstance.IpAddresses = []string{}
|
|
}
|
|
instanceMap = maps.Map{
|
|
"id": userInstance.AdPackageInstance.Id,
|
|
"ipAddresses": userInstance.AdPackageInstance.IpAddresses,
|
|
}
|
|
}
|
|
|
|
// 产品
|
|
var packageMap = maps.Map{
|
|
"id": 0,
|
|
}
|
|
if userInstance.AdPackageInstance != nil && userInstance.AdPackageInstance.AdPackage != nil {
|
|
packageMap = maps.Map{
|
|
"id": userInstance.AdPackageInstance.AdPackage.Id,
|
|
"summary": userInstance.AdPackageInstance.AdPackage.Summary,
|
|
}
|
|
}
|
|
|
|
this.Data["userInstance"] = maps.Map{
|
|
"id": userInstance.Id,
|
|
"periodId": userInstance.AdPackagePeriodId,
|
|
"isAvailable": userInstance.IsAvailable,
|
|
"objects": objectMaps,
|
|
"dayTo": dateutils.SplitYmd(userInstance.DayTo),
|
|
"today": timeutil.Format("Y-m-d"),
|
|
"isExpired": len(userInstance.DayTo) == 0 || userInstance.DayTo < timeutil.Format("Ymd"),
|
|
"user": userMap,
|
|
"instance": instanceMap,
|
|
"package": packageMap,
|
|
}
|
|
|
|
this.Show()
|
|
}
|
|
|
|
func (this *UpdateObjectsPopupAction) RunPost(params struct {
|
|
UserInstanceId int64
|
|
ObjectCodesJSON []byte
|
|
|
|
Must *actions.Must
|
|
CSRF *actionutils.CSRF
|
|
}) {
|
|
defer this.CreateLogInfo(codes.UserADInstance_LogUpdateUserADInstanceObjects, params.UserInstanceId)
|
|
|
|
var objectCodes = []string{}
|
|
if len(params.ObjectCodesJSON) > 0 {
|
|
err := json.Unmarshal(params.ObjectCodesJSON, &objectCodes)
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
}
|
|
|
|
// TODO 检查有没有超出最大防护对象数量
|
|
|
|
_, err := this.RPC().UserADInstanceRPC().UpdateUserADInstanceObjects(this.UserContext(), &pb.UpdateUserADInstanceObjectsRequest{
|
|
UserADInstanceId: params.UserInstanceId,
|
|
ObjectCodes: objectCodes,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
this.Success()
|
|
}
|