Files
EdgeAdmin/internal/web/actions/default/servers/server/settings/pages/createPopup.go

87 lines
2.0 KiB
Go
Raw Normal View History

2020-09-20 08:58:48 +08:00
package pages
import (
"encoding/json"
2020-11-20 15:32:42 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/oplogs"
2020-09-20 08:58:48 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
2021-10-10 10:35:14 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
2020-09-20 08:58:48 +08:00
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
)
type CreatePopupAction struct {
actionutils.ParentAction
}
func (this *CreatePopupAction) Init() {
this.Nav("", "", "")
}
func (this *CreatePopupAction) RunGet(params struct{}) {
2021-10-10 10:35:14 +08:00
this.Data["bodyTypes"] = shared.FindAllBodyTypes()
2020-09-20 08:58:48 +08:00
this.Show()
}
func (this *CreatePopupAction) RunPost(params struct {
2021-10-10 10:35:14 +08:00
Status string
BodyType string
URL string `alias:"url"`
Body string
2020-09-20 08:58:48 +08:00
NewStatus int
Must *actions.Must
}) {
// TODO 对状态码进行更多校验
params.Must.
Field("status", params.Status).
2021-10-10 10:35:14 +08:00
Require("请输入响应状态码")
switch params.BodyType {
case shared.BodyTypeURL:
params.Must.
Field("url", params.URL).
Require("请输入要显示的URL")
case shared.BodyTypeHTML:
params.Must.
Field("body", params.Body).
Require("请输入要显示的HTML内容")
}
2020-09-20 08:58:48 +08:00
createResp, err := this.RPC().HTTPPageRPC().CreateHTTPPage(this.AdminContext(), &pb.CreateHTTPPageRequest{
StatusList: []string{params.Status},
2021-10-10 10:35:14 +08:00
BodyType: params.BodyType,
2020-09-20 08:58:48 +08:00
Url: params.URL,
2021-10-10 10:35:14 +08:00
Body: params.Body,
2020-09-20 08:58:48 +08:00
NewStatus: types.Int32(params.NewStatus),
})
if err != nil {
this.ErrorPage(err)
return
}
pageId := createResp.PageId
configResp, err := this.RPC().HTTPPageRPC().FindEnabledHTTPPageConfig(this.AdminContext(), &pb.FindEnabledHTTPPageConfigRequest{PageId: pageId})
if err != nil {
this.ErrorPage(err)
return
}
pageConfig := &serverconfigs.HTTPPageConfig{}
2020-09-21 19:51:50 +08:00
err = json.Unmarshal(configResp.PageJSON, pageConfig)
2020-09-20 08:58:48 +08:00
if err != nil {
this.ErrorPage(err)
return
}
this.Data["page"] = pageConfig
2020-11-20 15:32:42 +08:00
// 日志
2022-03-31 14:53:07 +08:00
defer this.CreateLog(oplogs.LevelInfo, "创建自定义页面 %d", pageId)
2020-11-20 15:32:42 +08:00
2020-09-20 08:58:48 +08:00
this.Success()
}