88 lines
1.9 KiB
Go
88 lines
1.9 KiB
Go
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
//go:build plus
|
|
|
|
package scripts
|
|
|
|
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"
|
|
"strings"
|
|
)
|
|
|
|
type UpdateAction struct {
|
|
actionutils.ParentAction
|
|
}
|
|
|
|
func (this *UpdateAction) Init() {
|
|
this.Nav("", "", "")
|
|
}
|
|
|
|
func (this *UpdateAction) RunGet(params struct {
|
|
ScriptId int64
|
|
EditingCode bool
|
|
}) {
|
|
this.Data["isEditingCode"] = params.EditingCode
|
|
|
|
scriptResp, err := this.RPC().ScriptRPC().FindEnabledScript(this.AdminContext(), &pb.FindEnabledScriptRequest{ScriptId: params.ScriptId})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
var script = scriptResp.Script
|
|
if script == nil {
|
|
this.NotFound("script", params.ScriptId)
|
|
return
|
|
}
|
|
|
|
this.Data["script"] = maps.Map{
|
|
"id": script.Id,
|
|
"name": script.Name,
|
|
"filename": script.Filename,
|
|
"code": script.Code,
|
|
"isOn": script.IsOn,
|
|
}
|
|
|
|
this.Show()
|
|
}
|
|
|
|
func (this *UpdateAction) RunPost(params struct {
|
|
ScriptId int64
|
|
Filename string
|
|
Name string
|
|
Code string
|
|
IsOn bool
|
|
|
|
Must *actions.Must
|
|
CSRF *actionutils.CSRF
|
|
}) {
|
|
defer this.CreateLogInfo(codes.Script_LogUpdateScript, params.ScriptId)
|
|
|
|
params.Must.
|
|
Field("filename", params.Filename).
|
|
Require("请输入脚本文件名").
|
|
Field("name", params.Name).
|
|
Require("请输入脚本说明")
|
|
|
|
if !strings.HasSuffix(params.Filename, ".js") {
|
|
params.Filename += ".js"
|
|
}
|
|
|
|
_, err := this.RPC().ScriptRPC().UpdateScript(this.AdminContext(), &pb.UpdateScriptRequest{
|
|
ScriptId: params.ScriptId,
|
|
Name: params.Name,
|
|
Filename: params.Filename,
|
|
Code: params.Code,
|
|
IsOn: params.IsOn,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
this.Success()
|
|
}
|