From fbd163cdd417dd64a14e4450777dcf309ecfb6b1 Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Wed, 15 Sep 2021 14:20:01 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):?= =?UTF-8?q?=20=E8=87=AA=E5=AE=9A=E4=B9=89=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mapper/ext/ExtCustomFunctionMapper.java | 12 +++ .../mapper/ext/ExtCustomFunctionMapper.xml | 22 +++++ .../service/CustomFunctionService.java | 15 ++-- .../common/Jsr233ProcessorContent.vue | 83 ++++++++++++++++++- .../components/case/MsEnvironmentSelect.vue | 5 +- .../project/function/ApiFuncRelevance.vue | 4 +- .../project/function/CustomFunction.vue | 2 +- .../project/function/CustomFunctionRelate.vue | 2 +- .../project/function/EditFunction.vue | 72 ++++++++++------ .../project/function/custom-function.js | 53 +++++++----- 10 files changed, 203 insertions(+), 67 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFunctionMapper.java create mode 100644 backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFunctionMapper.xml diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFunctionMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFunctionMapper.java new file mode 100644 index 0000000000..91347f1105 --- /dev/null +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFunctionMapper.java @@ -0,0 +1,12 @@ +package io.metersphere.base.mapper.ext; + +import io.metersphere.base.domain.CustomFunction; +import io.metersphere.controller.request.CustomFunctionRequest; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +public interface ExtCustomFunctionMapper { + + List queryAll(@Param("request")CustomFunctionRequest request); +} diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFunctionMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFunctionMapper.xml new file mode 100644 index 0000000000..0170e1316a --- /dev/null +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtCustomFunctionMapper.xml @@ -0,0 +1,22 @@ + + + + + + + \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/service/CustomFunctionService.java b/backend/src/main/java/io/metersphere/service/CustomFunctionService.java index 195dc8635f..654f716146 100644 --- a/backend/src/main/java/io/metersphere/service/CustomFunctionService.java +++ b/backend/src/main/java/io/metersphere/service/CustomFunctionService.java @@ -8,6 +8,7 @@ import io.metersphere.base.domain.CustomFunction; import io.metersphere.base.domain.CustomFunctionExample; import io.metersphere.base.domain.CustomFunctionWithBLOBs; import io.metersphere.base.mapper.CustomFunctionMapper; +import io.metersphere.base.mapper.ext.ExtCustomFunctionMapper; import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.BeanUtils; @@ -34,6 +35,8 @@ public class CustomFunctionService { @Resource private CustomFunctionMapper customFunctionMapper; @Resource + private ExtCustomFunctionMapper extCustomFunctionMapper; + @Resource private JMeterService jMeterService; public CustomFunctionWithBLOBs save(CustomFunctionRequest request) { @@ -76,17 +79,9 @@ public class CustomFunctionService { String projectId = request.getProjectId(); if (StringUtils.isBlank(projectId)) { projectId = SessionUtils.getCurrentProjectId(); + request.setProjectId(projectId); } - CustomFunctionExample example = new CustomFunctionExample(); - CustomFunctionExample.Criteria criteria = example.createCriteria(); - criteria.andProjectIdEqualTo(projectId); - if (StringUtils.isNotBlank(request.getType())) { - criteria.andTypeEqualTo(request.getType()); - } - if (StringUtils.isNotBlank(request.getName())) { - criteria.andNameEqualTo(request.getName()); - } - return customFunctionMapper.selectByExample(example); + return extCustomFunctionMapper.queryAll(request); } public void update(CustomFunctionRequest request) { diff --git a/frontend/src/business/components/api/automation/scenario/common/Jsr233ProcessorContent.vue b/frontend/src/business/components/api/automation/scenario/common/Jsr233ProcessorContent.vue index 3389898756..7be8ca7523 100644 --- a/frontend/src/business/components/api/automation/scenario/common/Jsr233ProcessorContent.vue +++ b/frontend/src/business/components/api/automation/scenario/common/Jsr233ProcessorContent.vue @@ -23,6 +23,8 @@ + + @@ -32,9 +34,11 @@ import MsCodeEdit from "../../../definition/components/MsCodeEdit"; import MsDropdown from "../../../../common/components/MsDropdown"; import CustomFunctionRelate from "@/business/components/settings/project/function/CustomFunctionRelate"; + import {getCodeTemplate} from "@/business/components/settings/project/function/custom-function"; + import ApiFuncRelevance from "@/business/components/settings/project/function/ApiFuncRelevance"; export default { name: "Jsr233ProcessorContent", - components: {MsDropdown, MsCodeEdit, CustomFunctionRelate}, + components: {MsDropdown, MsCodeEdit, CustomFunctionRelate, ApiFuncRelevance}, data() { return { jsr223ProcessorData: {}, @@ -100,8 +104,17 @@ title: "插入自定义函数", command: "custom_function", index: "custom_function" + }, + { + title: "从API定义导入", + command: "api_definition", + index: "api_definition" + }, + { + title: "新API测试[JSON]", + command: "new_api_request", + index: "new_api_request" } - ], isCodeEditAlive: true, languages: [ @@ -168,8 +181,74 @@ doFuncLink(funcLink) { if (funcLink.command === 'custom_function') { this.$refs.customFunctionRelate.open(this.jsr223ProcessorData.scriptLanguage); + } else if (funcLink.command === 'api_definition') { + this.$refs.apiFuncRelevance.open(); + } else if (funcLink.command === 'new_api_request') { + // requestObj为空则生产默认模版 + let headers = new Map(); + headers.set('Content-type', 'application/json'); + let code = getCodeTemplate(this.jsr223ProcessorData.scriptLanguage, {requestHeaders: headers}); + let codeStr = this.jsr223ProcessorData.script + "\n\n" + code; + this.jsr223ProcessorData.script = this.jsr223ProcessorData.script ? codeStr : code; + this.reload(); } }, + apiSave(data, env) { + // data:选中的多个接口定义或多个接口用例; env: 关联页面选中的环境 + let condition = env.config.httpConfig.conditions || []; + let requestUrl = ""; + if (condition && condition.length > 0) { + // 如果有多个环境,取第一个 + let protocol = condition[0].protocol ? condition[0].protocol : "http"; + requestUrl = protocol + "://" + condition[0].socket; + } + // todo + if (data.length > 5) { + this.$warning("最多可以选择5个接口!"); + return; + } + let code = ""; + if (data.length > 0) { + data.forEach(dt => { + let param = this.parseRequestObj(dt, requestUrl); + code += '\n' + getCodeTemplate(this.jsr223ProcessorData.scriptLanguage, param); + }) + } + if (code) { + let codeStr = this.jsr223ProcessorData.script + code; + this.jsr223ProcessorData.script = this.jsr223ProcessorData.script ? codeStr : code; + this.reload(); + } else { + //todo + this.$warning("无对应语言模版"); + } + this.$refs.apiFuncRelevance.close(); + }, + parseRequestObj(data, requestUrl) { + let requestHeaders = new Map(); + let requestMethod = ""; + let requestBody = ""; + let request = JSON.parse(data.request); + // 拼接发送请求需要的参数 + requestUrl = requestUrl + request.path; + requestMethod = request.method; + let headers = request.headers; + if (headers && headers.length > 0) { + headers.forEach(header => { + if (header.name) { + requestHeaders.set(header.name, header.value); + } + }) + } + let body = request.body; + if (body.json) { + requestBody = body.raw; + } + return {requestUrl, requestHeaders, requestMethod, requestBody} + }, + apiClose() { + + }, } } diff --git a/frontend/src/business/components/api/definition/components/case/MsEnvironmentSelect.vue b/frontend/src/business/components/api/definition/components/case/MsEnvironmentSelect.vue index dcc8401bb2..f0b1dd6b26 100644 --- a/frontend/src/business/components/api/definition/components/case/MsEnvironmentSelect.vue +++ b/frontend/src/business/components/api/definition/components/case/MsEnvironmentSelect.vue @@ -22,11 +22,12 @@