// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. //go:build plus package routes import ( "encoding/json" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/langs/codes" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/iwind/TeaGo/actions" "github.com/iwind/TeaGo/maps" ) type UpdatePopupAction struct { actionutils.ParentAction } func (this *UpdatePopupAction) Init() { this.Nav("", "", "") } func (this *UpdatePopupAction) RunGet(params struct { RouteId int64 }) { routeResp, err := this.RPC().NSRouteRPC().FindNSRoute(this.AdminContext(), &pb.FindNSRouteRequest{NsRouteId: params.RouteId}) if err != nil { this.ErrorPage(err) return } var route = routeResp.NsRoute if route == nil { this.NotFound("nsRoute", params.RouteId) return } var rangeMaps = []maps.Map{} if len(route.RangesJSON) > 0 { err = json.Unmarshal(route.RangesJSON, &rangeMaps) if err != nil { this.ErrorPage(err) return } } // 分类 var categoryId int64 = 0 if route.NsRouteCategory != nil { categoryId = route.NsRouteCategory.Id } this.Data["route"] = maps.Map{ "id": route.Id, "name": route.Name, "isOn": route.IsOn, "isPublic": route.IsPublic, "categoryId": categoryId, "priority": route.Priority, "ranges": rangeMaps, } // 范围类型 this.Data["rangeTypes"] = dnsconfigs.AllNSRouteRangeTypes() // 所有分类 categoriesResp, err := this.RPC().NSRouteCategoryRPC().FindAllNSRouteCategories(this.AdminContext(), &pb.FindAllNSRouteCategoriesRequest{}) if err != nil { this.ErrorPage(err) return } var categoryMaps = []maps.Map{} for _, category := range categoriesResp.NsRouteCategories { categoryMaps = append(categoryMaps, maps.Map{ "id": category.Id, "name": category.Name, }) } this.Data["categories"] = categoryMaps this.Show() } func (this *UpdatePopupAction) RunPost(params struct { RouteId int64 Name string RangesJSON []byte IsPublic bool CategoryId int64 Priority int32 IsOn bool Must *actions.Must CSRF *actionutils.CSRF }) { defer this.CreateLogInfo(codes.NSRoute_LogUpdateNSRoute, params.RouteId) params.Must.Field("name", params.Name). Require("请输入线路名称") ranges, err := dnsconfigs.InitNSRangesFromJSON(params.RangesJSON) if err != nil { this.Fail("配置校验失败:" + err.Error()) } if len(ranges) == 0 { this.Fail("请添加线路范围") } _, err = this.RPC().NSRouteRPC().UpdateNSRoute(this.AdminContext(), &pb.UpdateNSRouteRequest{ NsRouteId: params.RouteId, Name: params.Name, RangesJSON: params.RangesJSON, IsPublic: params.IsPublic, NsRouteCategoryId: params.CategoryId, Priority: params.Priority, IsOn: params.IsOn, }) if err != nil { this.ErrorPage(err) return } this.Success() }