2020-09-20 08:58:48 +08:00
|
|
|
package pages
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
2023-06-30 18:08:30 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
2020-09-20 08:58:48 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
|
|
|
"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{}) {
|
2023-11-10 16:36:03 +08:00
|
|
|
this.Data["bodyTypes"] = serverconfigs.FindAllHTTPPageBodyTypes()
|
2021-10-10 10:35:14 +08:00
|
|
|
|
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 {
|
2023-11-10 16:36:03 +08:00
|
|
|
case serverconfigs.HTTPPageBodyTypeURL:
|
2021-10-10 10:35:14 +08:00
|
|
|
params.Must.
|
|
|
|
|
Field("url", params.URL).
|
2023-07-07 11:48:36 +08:00
|
|
|
Require("请输入要显示的URL").
|
|
|
|
|
Match( `^(?i)(http|https)://`, "请输入正确的URL")
|
2023-11-10 16:36:03 +08:00
|
|
|
case serverconfigs.HTTPPageBodyTypeRedirectURL:
|
|
|
|
|
params.Must.
|
|
|
|
|
Field("url", params.URL).
|
|
|
|
|
Require("请输入要跳转的URL").
|
|
|
|
|
Match( `^(?i)(http|https)://`, "请输入正确的URL")
|
|
|
|
|
case serverconfigs.HTTPPageBodyTypeHTML:
|
2021-10-10 10:35:14 +08:00
|
|
|
params.Must.
|
|
|
|
|
Field("body", params.Body).
|
|
|
|
|
Require("请输入要显示的HTML内容")
|
2023-07-05 16:12:01 +08:00
|
|
|
|
|
|
|
|
if len(params.Body) > 32*1024 {
|
|
|
|
|
this.FailField("body", "自定义页面内容不能超过32K")
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-10-10 10:35:14 +08:00
|
|
|
}
|
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
|
|
|
|
|
}
|
2023-07-05 16:12:01 +08:00
|
|
|
var pageId = createResp.HttpPageId
|
2020-09-20 08:58:48 +08:00
|
|
|
|
2022-03-31 15:21:02 +08:00
|
|
|
configResp, err := this.RPC().HTTPPageRPC().FindEnabledHTTPPageConfig(this.AdminContext(), &pb.FindEnabledHTTPPageConfigRequest{HttpPageId: pageId})
|
2020-09-20 08:58:48 +08:00
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-05 16:12:01 +08:00
|
|
|
var 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
|
|
|
|
|
}
|
2023-07-05 16:12:01 +08:00
|
|
|
err = pageConfig.Init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.Fail("配置校验失败:" + err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 08:58:48 +08:00
|
|
|
this.Data["page"] = pageConfig
|
|
|
|
|
|
2020-11-20 15:32:42 +08:00
|
|
|
// 日志
|
2023-06-30 18:08:30 +08:00
|
|
|
defer this.CreateLogInfo(codes.ServerPage_LogCreatePage, pageId)
|
2020-11-20 15:32:42 +08:00
|
|
|
|
2020-09-20 08:58:48 +08:00
|
|
|
this.Success()
|
|
|
|
|
}
|