diff --git a/internal/web/actions/default/ui/init.go b/internal/web/actions/default/ui/init.go index dff32542..fc0543f6 100644 --- a/internal/web/actions/default/ui/init.go +++ b/internal/web/actions/default/ui/init.go @@ -30,6 +30,7 @@ func init() { Post("/showTip", new(ShowTipAction)). Post("/hideTip", new(HideTipAction)). Post("/theme", new(ThemeAction)). + Post("/validateIPs", new(ValidateIPsAction)). EndAll() }) diff --git a/internal/web/actions/default/ui/validateIPs.go b/internal/web/actions/default/ui/validateIPs.go new file mode 100644 index 00000000..484cf06c --- /dev/null +++ b/internal/web/actions/default/ui/validateIPs.go @@ -0,0 +1,41 @@ +// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. + +package ui + +import ( + "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils" + "net" + "strings" +) + +type ValidateIPsAction struct { + actionutils.ParentAction +} + +func (this *ValidateIPsAction) RunPost(params struct { + Ips string +}) { + var ips = params.Ips + if len(ips) == 0 { + this.Data["ips"] = []string{} + this.Success() + } + + var ipSlice = strings.Split(ips, "\n") + var result = []string{} + for _, ip := range ipSlice { + ip = strings.TrimSpace(ip) + if len(ip) == 0 { + continue + } + data := net.ParseIP(ip) + if len(data) == 0 { + this.Data["failIP"] = ip + this.Fail() + } + result = append(result, ip) + } + + this.Data["ips"] = result + this.Success() +} diff --git a/web/public/js/components/node/node-ip-address-thresholds-box.js b/web/public/js/components/node/node-ip-address-thresholds-box.js index 6152bb74..088161b9 100644 --- a/web/public/js/components/node/node-ip-address-thresholds-box.js +++ b/web/public/js/components/node/node-ip-address-thresholds-box.js @@ -95,10 +95,16 @@ Vue.component("node-ip-address-thresholds-box", { "name": "通知", "code": "notify", "description": "发送已达到阈值通知。" - } + }, + { + "name": "切换", + "code": "switch", + "description": "在DNS中记录中将IP切换到指定的备用IP。" + }, ], - actionCode: "up" + actionCode: "up", + actionBackupIPs: "" } }, methods: { @@ -280,8 +286,12 @@ Vue.component("node-ip-address-thresholds-box", { cancelAction: function () { this.isAddingAction = false this.actionCode = "up" + this.actionBackupIPs = "" }, confirmAction: function () { + this.doConfirmAction(false) + }, + doConfirmAction: function (validated, options) { // 是否已存在 let exists = false let that = this @@ -295,9 +305,41 @@ Vue.component("node-ip-address-thresholds-box", { return } + if (options == null) { + options = {} + } + + switch (this.actionCode) { + case "switch": + if (!validated) { + Tea.action("/ui/validateIPs") + .params({ + "ips": this.actionBackupIPs + }) + .success(function (resp) { + if (resp.data.ips.length == 0) { + teaweb.warn("请输入备用IP", function () { + that.$refs.actionBackupIPs.focus() + }) + return + } + options["ips"] = resp.data.ips + that.doConfirmAction(true, options) + }) + .fail(function (resp) { + teaweb.warn("输入的IP '" + resp.data.failIP + "' 格式不正确,请改正后提交", function () { + that.$refs.actionBackupIPs.focus() + }) + }) + .post() + return + } + break + } + this.addingThreshold.actions.push({ action: this.actionCode, - options: {} + options: options }) // 还原 @@ -322,14 +364,16 @@ Vue.component("node-ip-address-thresholds-box", {
{{action.description}}
+每行一个备用IP。
+