From 165f63215ef40919afcc72c8a4fc77113ef4fc19 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 13 Jan 2022 14:45:13 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E5=BC=95=E7=94=A8=E5=9C=BA=E6=99=AF?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E5=A4=84=E7=90=86=EF=BC=8C=E5=87=8F=E5=B0=91?= =?UTF-8?q?=E5=89=8D=E5=90=8E=E7=AB=AF=E4=BA=A4=E4=BA=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ApiAutomationController.java | 2 +- .../api/service/ApiAutomationService.java | 48 ++++++++++++ .../component/ApiScenarioComponent.vue | 77 +------------------ 3 files changed, 52 insertions(+), 75 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java index 74692d78dd..648ec0afb1 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java @@ -175,7 +175,7 @@ public class ApiAutomationController { @GetMapping("/getApiScenario/{id}") public ApiScenarioWithBLOBs getScenarioDefinition(@PathVariable String id) { - return apiAutomationService.getApiScenario(id); + return apiAutomationService.getNewApiScenario(id); } @PostMapping("/getApiScenarioEnv") diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 37bec27ddd..a9417a6dc5 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -647,6 +647,54 @@ public class ApiAutomationService { return apiScenarioMapper.selectByPrimaryKey(id); } + public ApiScenarioWithBLOBs getNewApiScenario(String id) { + ApiScenarioWithBLOBs scenarioWithBLOBs = apiScenarioMapper.selectByPrimaryKey(id); + if (scenarioWithBLOBs != null && StringUtils.isNotEmpty(scenarioWithBLOBs.getScenarioDefinition())) { + JSONObject element = JSON.parseObject(scenarioWithBLOBs.getScenarioDefinition()); + this.dataFormatting(element); + scenarioWithBLOBs.setScenarioDefinition(JSON.toJSONString(element)); + } + return scenarioWithBLOBs; + } + + public void dataFormatting(JSONArray hashTree) { + for (int i = 0; i < hashTree.size(); i++) { + JSONObject element = hashTree.getJSONObject(i); + if (element != null && StringUtils.equalsIgnoreCase(element.getString("type"), "scenario")) { + ApiScenarioWithBLOBs scenarioWithBLOBs = apiScenarioMapper.selectByPrimaryKey(element.getString("id")); + if (scenarioWithBLOBs != null && StringUtils.isNotEmpty(scenarioWithBLOBs.getScenarioDefinition())) { + if (StringUtils.equalsIgnoreCase(element.getString("referenced"), "REF")) { + element = JSON.parseObject(scenarioWithBLOBs.getScenarioDefinition()); + element.put("referenced", "REF"); + } + element.put("num", scenarioWithBLOBs.getNum()); + hashTree.set(i, element); + } + } + if (element.containsKey("hashTree")) { + JSONArray elementJSONArray = element.getJSONArray("hashTree"); + dataFormatting(elementJSONArray); + } + } + } + + public void dataFormatting(JSONObject element) { + if (element != null && StringUtils.equalsIgnoreCase(element.getString("type"), "scenario")) { + ApiScenarioWithBLOBs scenarioWithBLOBs = apiScenarioMapper.selectByPrimaryKey(element.getString("id")); + if (scenarioWithBLOBs != null && StringUtils.isNotEmpty(scenarioWithBLOBs.getScenarioDefinition())) { + if (StringUtils.equalsIgnoreCase(element.getString("referenced"), "REF")) { + element = JSON.parseObject(scenarioWithBLOBs.getScenarioDefinition()); + element.put("referenced", "REF"); + } + element.put("num", scenarioWithBLOBs.getNum()); + } + } + if (element != null && element.containsKey("hashTree")) { + JSONArray elementJSONArray = element.getJSONArray("hashTree"); + dataFormatting(elementJSONArray); + } + } + public String setDomain(ApiScenarioEnvRequest request) { Boolean enable = request.getEnvironmentEnable(); String scenarioDefinition = request.getDefinition(); diff --git a/frontend/src/business/components/api/automation/scenario/component/ApiScenarioComponent.vue b/frontend/src/business/components/api/automation/scenario/component/ApiScenarioComponent.vue index 03969dfe47..63b358c161 100644 --- a/frontend/src/business/components/api/automation/scenario/component/ApiScenarioComponent.vue +++ b/frontend/src/business/components/api/automation/scenario/component/ApiScenarioComponent.vue @@ -108,56 +108,13 @@ export default { created() { if (this.scenario.num) { this.isShowNum = true; + this.getWorkspaceId(this.scenario.projectId); + } else { + this.isSameSpace = false; } if (!this.scenario.projectId) { this.scenario.projectId = getCurrentProjectID(); } - if (this.scenario.id && this.scenario.referenced === 'REF' && !this.scenario.loaded) { - let scenarios = JSON.parse(JSON.stringify(this.scenario.hashTree)); - let map = new Map(); - this.formatResult(map, scenarios); - this.result = this.$get("/api/automation/getApiScenario/" + this.scenario.id, response => { - if (response.data) { - this.scenario.loaded = true; - let obj = {}; - if (response.data.scenarioDefinition) { - obj = JSON.parse(response.data.scenarioDefinition); - this.scenario.hashTree = obj.hashTree; - } - this.scenario.projectId = response.data.projectId; - const pro = this.projectList.find(p => p.id === response.data.projectId); - if (!pro) { - this.scenario.projectId = getCurrentProjectID(); - } - if (this.scenario.hashTree) { - this.setDisabled(this.scenario.hashTree, this.scenario.projectId); - } - if (response.data.num) { - this.scenario.num = response.data.num; - this.getWorkspaceId(response.data.projectId); - } - this.scenario.name = response.data.name; - this.scenario.headers = obj.headers; - this.scenario.variables = obj.variables; - this.scenario.environmentMap = obj.environmentMap; - this.setResult(this.scenario.hashTree, map); - this.$emit('refReload'); - } - }) - } else if (this.scenario.id && (this.scenario.referenced === 'Copy' || this.scenario.referenced === 'Created') && !this.scenario.loaded) { - this.result = this.$get("/api/automation/getApiScenario/" + this.scenario.id, response => { - if (response.data) { - if (response.data.num) { - this.scenario.num = response.data.num; - this.getWorkspaceId(response.data.projectId); - } else { - this.isSameSpace = false - } - } else { - this.isSameSpace = false - } - }) - } }, components: {ApiBaseComponent, MsSqlBasisParameters, MsTcpBasisParameters, MsDubboBasisParameters, MsApiRequestForm}, data() { @@ -181,34 +138,6 @@ export default { }, }, methods: { - formatResult(map, scenarios) { - scenarios.forEach(item => { - if (this.stepFilter.get("AllSamplerProxy").indexOf(item.type) !== -1 && item.requestResult) { - let key = (item.id ? item.id : item.resourceId) + "_" + item.index; - if (map.has(key)) { - map.get(key).push(...item.requestResult); - } else { - map.set(key, item.requestResult); - } - } - if (item.hashTree && item.hashTree.length > 0) { - this.formatResult(map, item.hashTree); - } - }) - }, - setResult(array, scenarios) { - if (array && scenarios) { - array.forEach(item => { - let key = (item.id ? item.id : item.resourceId) + "_" + item.index; - if (scenarios.has(key)) { - item.requestResult = scenarios.get(key); - } - if (item.hashTree && item.hashTree.length > 0) { - this.setResult(item.hashTree, scenarios); - } - }) - } - }, run() { this.scenario.run = true; let runScenario = JSON.parse(JSON.stringify(this.scenario));