fix: 修改自定义函数报错

This commit is contained in:
shiziyuan9527 2021-09-13 17:55:08 +08:00 committed by 刘瑞斌
parent ce8de01585
commit d195499696
3 changed files with 12 additions and 92 deletions

View File

@ -28,11 +28,6 @@
</el-form-item>
</el-col>
</el-row>
<!-- <el-row>-->
<!-- <el-form-item :label="'参数列表'" prop="">-->
<!-- <function-params :items="form.params"/>-->
<!-- </el-form-item>-->
<!-- </el-row>-->
<el-row style="margin-right: 10px;">
<el-col :span="20">
<el-form-item>
@ -96,9 +91,9 @@ import MsInputTag from "@/business/components/api/automation/scenario/MsInputTag
import FunctionParams from "@/business/components/settings/project/function/FunctionParams";
import MsCodeEdit from "@/business/components/common/components/MsCodeEdit";
import MsDropdown from "@/business/components/common/components/MsDropdown";
import {FUNC_TEMPLATE, splicingCustomFunc} from "@/business/components/settings/project/function/custom_function";
import {FUNC_TEMPLATE} from "@/business/components/settings/project/function/custom-function";
import MsRun from "@/business/components/api/automation/scenario/DebugRun";
import {getUUID} from "@/common/js/utils";
import {getCurrentProjectID, getUUID} from "@/common/js/utils";
import {JSR223Processor} from "@/business/components/api/definition/model/ApiTestModel";
import FunctionRun from "@/business/components/settings/project/function/FunctionRun";
import CustomFunctionRelate from "@/business/components/settings/project/function/CustomFunctionRelate";
@ -229,38 +224,7 @@ export default {
console: "无执行结果"
}
},
// watch: {
// 'form.name'() {
// this.splicingFunc();
// },
// 'form.params': {
// handler() {
// this.splicingFunc();
// },
// deep: true
// }
// },
methods: {
// _parseFuncParam(funcObj) {
// let params = undefined;
// if (funcObj.params) {
// params = funcObj.params.map(p => p.name);
// if (params.length > 0) {
// params = params.filter(p => {
// return p !== undefined
// });
// params.join(",\s");
// }
// }
// // todo
// return params;
// },
// splicingFunc() {
// let funcObj = this.form;
// let funcParams = this._parseFuncParam(funcObj);
// this.form.script = splicingCustomFunc(funcObj, funcParams);
// this.reloadCodeEdit();
// },
open(data) {
this.activeName = "code";
this.visible = true;
@ -285,11 +249,6 @@ export default {
} else {
this.form.tags = JSON.parse(this.form.tags);
}
// if (!this.form.params) {
// this.form.params = [];
// } else {
// this.form.params = JSON.parse(this.form.params);
// }
this.reload();
})
},
@ -357,6 +316,9 @@ export default {
})
},
update(obj) {
if (!obj.projectId) {
obj.projectId = getCurrentProjectID();
}
this.result = this.$post("/custom/func/update", obj, () => {
this.$emit("refresh");
this.$success(this.$t('commons.modify_success'));

View File

@ -0,0 +1,7 @@
export const FUNC_TEMPLATE = {
beanshell: "public static void test() {\n\n\n}",
groovy: "public static void test() {\n\n\n}",
python: "def test():\n",
nashornScript: "function test() {\n\n\n}",
rhinoScript: "function test() {\n\n\n}"
}

View File

@ -1,49 +0,0 @@
export const FUNC_TEMPLATE = {
beanshell: "public static void test() {\n\n\n}",
groovy: "public static void test() {\n\n\n}",
python: "def test():\n",
nashornScript: "function test() {\n\n\n}",
rhinoScript: "function test() {\n\n\n}"
}
// 拼接函数
export function splicingCustomFunc(funcObj, funcParams) {
let funcLanguage = funcObj.type || "beanshell";
let funcObjScript = funcObj.script;
let funcName = funcObj.name;
let funcFirstLine = generateFuncFirstLine(funcLanguage, funcName, funcParams);
if (!funcObjScript && funcName) {
funcObjScript = funcLanguage === "python" ? funcFirstLine : funcFirstLine + "\n\n\n}";
}
if (funcObjScript) {
funcObjScript = funcObjScript.replace(regex[funcLanguage], funcFirstLine);
}
return funcObjScript;
}
export function generateFuncFirstLine(funcLanguage, funcName, funcParams) {
let funcEnd = funcLanguage === "python" ? ":" : "{";
return scriptFuncDefinition[funcLanguage] + " " + funcName + "(" + funcParams + ") " + funcEnd;
}
const scriptFuncDefinition = {
beanshell: "public static void",
python: "def",
groovy: "public static void",
// nashornScript: "",
// rhinoScript: "",
}
const firstFuncRegex = RegExp(".*\(.*\)\\s\{\\r?");
const regex = {
beanshell: calcRegex(scriptFuncDefinition.beanshell),
python: RegExp("^def\\s.*\(.*\)\\s\:"),
groovy: calcRegex(scriptFuncDefinition.groovy),
// nashornScript: calcRegex(scriptFuncDefinition.nashornScript),
// rhinoScript: calcRegex(scriptFuncDefinition.rhinoScript),
}
function calcRegex(str) {
return RegExp("^" + str + "\\s.*\(.*\)\\s\{");
}