diff --git a/internal/web/actions/default/servers/server/settings/conds/addCondPopup.go b/internal/web/actions/default/servers/server/settings/conds/addCondPopup.go index b752c2a7..a7409887 100644 --- a/internal/web/actions/default/servers/server/settings/conds/addCondPopup.go +++ b/internal/web/actions/default/servers/server/settings/conds/addCondPopup.go @@ -30,7 +30,7 @@ func (this *AddCondPopupAction) RunPost(params struct { condConfig := &shared.HTTPRequestCond{} err := json.Unmarshal(params.CondJSON, condConfig) if err != nil { - this.Fail("解析条件设置时发生了错误:" + err.Error()) + this.Fail("解析条件设置时发生了错误:" + err.Error() + ", JSON: " + string(params.CondJSON)) } err = condConfig.Init() if err != nil { diff --git a/internal/web/actions/default/ui/components.go b/internal/web/actions/default/ui/components.go index f99bad57..f11d6928 100644 --- a/internal/web/actions/default/ui/components.go +++ b/internal/web/actions/default/ui/components.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/servers/server/settings/conds/condutils" + "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/actions" "github.com/iwind/TeaGo/files" @@ -52,13 +53,33 @@ func (this *ComponentsAction) RunGet(params struct{}) { // 条件组件 typesJSON, err := json.Marshal(condutils.ReadAllAvailableCondTypes()) if err != nil { - logs.Println("ComponentsAction: " + err.Error()) + logs.Println("ComponentsAction marshal request cond types failed: " + err.Error()) } else { buffer.WriteString("window.REQUEST_COND_COMPONENTS = ") buffer.Write(typesJSON) buffer.Write([]byte{'\n', '\n'}) } + // 条件操作符 + requestOperatorsJSON, err := json.Marshal(shared.AllRequestOperators()) + if err != nil { + logs.Println("ComponentsAction marshal request operators failed: " + err.Error()) + } else { + buffer.WriteString("window.REQUEST_COND_OPERATORS = ") + buffer.Write(requestOperatorsJSON) + buffer.Write([]byte{'\n', '\n'}) + } + + // 请求变量 + requestVariablesJSON, err := json.Marshal(shared.DefaultRequestVariables()) + if err != nil { + logs.Println("ComponentsAction marshal request variables failed: " + err.Error()) + } else { + buffer.WriteString("window.REQUEST_VARIABLES = ") + buffer.Write(requestVariablesJSON) + buffer.Write([]byte{'\n', '\n'}) + } + componentsData = buffer.Bytes() this.Write(componentsData) } diff --git a/web/public/js/components/server/http-cache-refs-box.js b/web/public/js/components/server/http-cache-refs-box.js index 77797c3d..650e528f 100644 --- a/web/public/js/components/server/http-cache-refs-box.js +++ b/web/public/js/components/server/http-cache-refs-box.js @@ -40,7 +40,6 @@ Vue.component("http-cache-refs-box", {
服务器返回的内容的MimeType,比如text/html、image/*等。
` +}) + +// 参数匹配 +Vue.component("http-cond-params", { + props: ["v-cond"], + mounted: function () { + let cond = this.vCond + if (cond == null) { + return + } + this.operator = cond.operator + + // stringValue + if (["regexp", "not regexp", "eq", "not", "prefix", "suffix", "contains", "not contains", "eq ip", "gt ip", "gte ip", "lt ip", "lte ip", "ip range"].$contains(cond.operator)) { + this.stringValue = cond.value + return + } + + // numberValue + if (["eq int", "eq float", "gt", "gte", "lt", "lte", "mod 10", "ip mod 10", "mod 100", "ip mod 100"].$contains(cond.operator)) { + this.numberValue = cond.value + return + } + + // modValue + if (["mod", "ip mod"].$contains(cond.operator)) { + let pieces = cond.value.split(",") + this.modDivValue = pieces[0] + if (pieces.length > 1) { + this.modRemValue = pieces[1] + } + return + } + + // stringValues + let that = this + if (["in", "not in", "file ext", "mime type"].$contains(cond.operator)) { + try { + let arr = JSON.parse(cond.value) + if (arr != null && (arr instanceof Array)) { + arr.forEach(function (v) { + that.stringValues.push(v) + }) + } + } catch (e) { + + } + return + } + + // versionValue + if (["version range"].$contains(cond.operator)) { + let pieces = cond.value.split(",") + this.versionRangeMinValue = pieces[0] + if (pieces.length > 1) { + this.versionRangeMaxValue = pieces[1] + } + return + } + }, + data: function () { + let cond = { + isRequest: true, + param: "", + operator: window.REQUEST_COND_OPERATORS[0].op, + value: "" + } + if (this.vCond != null) { + cond = this.vCond + } + return { + cond: cond, + operators: window.REQUEST_COND_OPERATORS, + operator: window.REQUEST_COND_OPERATORS[0].op, + operatorDescription: window.REQUEST_COND_OPERATORS[0].description, + variables: window.REQUEST_VARIABLES, + variable: "", + + // 各种类型的值 + stringValue: "", + numberValue: "", + + modDivValue: "", + modRemValue: "", + + stringValues: [], + + versionRangeMinValue: "", + versionRangeMaxValue: "" + } + }, + methods: { + changeVariable: function () { + let v = this.cond.param + if (v == null) { + v = "" + } + this.cond.param = v + this.variable + }, + changeOperator: function () { + let that = this + this.operators.forEach(function (v) { + if (v.op == that.operator) { + that.operatorDescription = v.description + } + }) + + this.cond.operator = this.operator + + // 移动光标 + let box = document.getElementById("variables-value-box") + if (box != null) { + setTimeout(function () { + let input = box.getElementsByTagName("INPUT") + if (input.length > 0) { + input[0].focus() + } + }, 100) + } + }, + changeStringValues: function (v) { + this.stringValues = v + this.cond.value = JSON.stringify(v) + } + }, + watch: { + stringValue: function (v) { + this.cond.value = v + }, + numberValue: function (v) { + // TODO 校验数字 + this.cond.value = v + }, + modDivValue: function (v) { + if (v.length == 0) { + return + } + let div = parseInt(v) + if (isNaN(div)) { + div = 1 + } + this.modDivValue = div + this.cond.value = div + "," + this.modRemValue + }, + modRemValue: function (v) { + if (v.length == 0) { + return + } + let rem = parseInt(v) + if (isNaN(rem)) { + rem = 0 + } + this.modRemValue = rem + this.cond.value = this.modDivValue + "," + rem + }, + versionRangeMinValue: function (v) { + this.cond.value = this.versionRangeMinValue + "," + this.versionRangeMaxValue + }, + versionRangeMaxValue: function (v) { + this.cond.value = this.versionRangeMinValue + "," + this.versionRangeMaxValue + } + }, + template: ` +其中可以使用变量,类似于
{{operatorDescription}}
+要匹配的正则表达式,比如
要对比的数字。
+参数值除以10的余数,在0-9之间。
+参数值除以100的余数,在0-99之间。
+和参数值一致的字符串。
+和参数值不一致的字符串。
+参数值的前缀。
+参数值的后缀为此字符串。
+参数值包含此字符串。
+参数值不包含此字符串。
+添加参数值列表。
+添加参数值列表。
+添加扩展名列表,比如
添加MimeType列表,类似于
要对比的IP。
+参数中IP转换成整数后除以10的余数,在0-9之间。
+参数中IP转换成整数后除以100的余数,在0-99之间。
+