From b888024deaff553e06b3806030be46abfc5270ed Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Mon, 1 Apr 2024 19:54:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AE=9A=E6=97=B6=E5=AF=BC=E5=85=A5=E4=B8=8D?= =?UTF-8?q?=E7=94=9F=E6=95=88=E7=9A=84=E7=BC=BA=E9=99=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ApiReportShareController.java | 2 +- .../definition/ApiDefinitionController.java | 3 ++- .../service/ApiReportSendNoticeService.java | 26 ++++++++++++------- .../api/service/ApiReportShareService.java | 12 ++++++--- .../ApiDefinitionImportUtilService.java | 24 +++++++++-------- .../definition/ApiDefinitionService.java | 4 +-- .../service/schedule/SwaggerUrlImportJob.java | 8 +----- .../components/requestComposition/body.vue | 3 +++ .../requestComposition/response/edit.vue | 3 +++ .../api-test/management/components/import.vue | 1 + 10 files changed, 51 insertions(+), 35 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiReportShareController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiReportShareController.java index c9b9913902..00585383ad 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiReportShareController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiReportShareController.java @@ -28,7 +28,7 @@ public class ApiReportShareController { @Operation(summary = "接口测试-接口报告-生成分享链接") @RequiresPermissions(PermissionConstants.PROJECT_API_REPORT_SHARE) public ShareInfoDTO generateShareInfo(@Validated(Created.class) @RequestBody ApiReportShareRequest request) { - return apiReportShareService.gen(request, Objects.requireNonNull(SessionUtils.getUser())); + return apiReportShareService.gen(request, SessionUtils.getUserId()); } @GetMapping("/get/{id}") diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java index 0aef1a10c9..c4adbbdb6f 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/definition/ApiDefinitionController.java @@ -217,7 +217,8 @@ public class ApiDefinitionController { @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_IMPORT) @Operation(summary = "接口测试-接口管理-导入接口定义") public void testCaseImport(@RequestPart(value = "file", required = false) MultipartFile file, @RequestPart("request") ImportRequest request) { - apiDefinitionService.apiTestImport(file, request, SessionUtils.getUser(), SessionUtils.getCurrentProjectId()); + request.setUserId(SessionUtils.getUserId()); + apiDefinitionService.apiTestImport(file, request, SessionUtils.getCurrentProjectId()); } @PostMapping("/operation-history") diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiReportSendNoticeService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiReportSendNoticeService.java index 01ab1ee6c3..555f04466d 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiReportSendNoticeService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiReportSendNoticeService.java @@ -4,6 +4,8 @@ import io.metersphere.api.domain.ApiReport; import io.metersphere.api.domain.ApiScenario; import io.metersphere.api.domain.ApiScenarioReport; import io.metersphere.api.domain.ApiTestCase; +import io.metersphere.api.dto.share.ApiReportShareRequest; +import io.metersphere.api.dto.share.ShareInfoDTO; import io.metersphere.api.mapper.ApiReportMapper; import io.metersphere.api.mapper.ApiScenarioMapper; import io.metersphere.api.mapper.ApiScenarioReportMapper; @@ -51,23 +53,32 @@ public class ApiReportSendNoticeService { @Resource private ProjectMapper projectMapper; private static final String API_SCENARIO = "API_SCENARIO"; + private static final String API_CASE = "API_CASE"; public void sendNotice(ApiNoticeDTO noticeDTO) { String noticeType; - String reportUrl; SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class); assert systemParameterService != null; BaseSystemConfigDTO baseSystemConfigDTO = systemParameterService.getBaseInfo(); BeanMap beanMap; String event; String status; + ApiReportShareService shareService = CommonBeanFactory.getBean(ApiReportShareService.class); + ApiReportShareRequest shareRequest = new ApiReportShareRequest(); + shareRequest.setReportId(noticeDTO.getReportId()); + shareRequest.setProjectId(noticeDTO.getProjectId()); + assert shareService != null; + ShareInfoDTO url = shareService.gen(shareRequest, noticeDTO.getUserId()); + Project project = projectMapper.selectByPrimaryKey(noticeDTO.getProjectId()); + String reportUrl = baseSystemConfigDTO.getUrl() + "/#/api-test/report?orgId=%s&pId=%s&type=%s&reportId=%s"; + String shareUrl = baseSystemConfigDTO.getUrl() + "/#/share/%s?shareId=" + url.getId(); ApiScenarioReport report = new ApiScenarioReport(); if (API_SCENARIO.equals(noticeDTO.getResourceType())) { ApiScenario scenario = apiScenarioMapper.selectByPrimaryKey(noticeDTO.getResourceId()); beanMap = new BeanMap(scenario); noticeType = NoticeConstants.TaskType.API_SCENARIO_TASK; - reportUrl = baseSystemConfigDTO.getUrl() + "/#/api/automation/report/view/" + noticeDTO.getReportId(); report = apiScenarioReportMapper.selectByPrimaryKey(noticeDTO.getReportId()); + reportUrl = String.format(reportUrl, project.getOrganizationId(), project.getId(), API_SCENARIO, report.getId()); if (StringUtils.endsWithIgnoreCase(noticeDTO.getReportStatus(), ApiReportStatus.SUCCESS.name())) { event = NoticeConstants.Event.SCENARIO_EXECUTE_SUCCESSFUL; status = "成功"; @@ -78,13 +89,14 @@ public class ApiReportSendNoticeService { event = NoticeConstants.Event.SCENARIO_EXECUTE_FAILED; status = "失败"; } + shareUrl = String.format(shareUrl, "shareReportScenario"); } else { ApiTestCase testCase = apiTestCaseMapper.selectByPrimaryKey(noticeDTO.getResourceId()); beanMap = new BeanMap(testCase); // TODO 是否需要区分场景和用例 noticeType = NoticeConstants.TaskType.API_DEFINITION_TASK; - reportUrl = baseSystemConfigDTO.getUrl() + "/#/api/automation/report/view/" + noticeDTO.getReportId(); + reportUrl = String.format(reportUrl, project.getOrganizationId(), project.getId(), API_CASE, report.getId()); ApiReport apiReport = apiReportMapper.selectByPrimaryKey(noticeDTO.getReportId()); BeanUtils.copyBean(report, apiReport); @@ -98,6 +110,7 @@ public class ApiReportSendNoticeService { event = NoticeConstants.Event.CASE_EXECUTE_FAILED; status = "失败"; } + shareUrl = String.format(shareUrl, "shareReportCase"); } String userId = noticeDTO.getUserId(); @@ -137,16 +150,11 @@ public class ApiReportSendNoticeService { } paramMap.put("reportUrl", reportUrl); - - // TODO: 缺少生成分享链接 - String shareUrl = null; - paramMap.put("scenarioShareUrl", baseSystemConfigDTO.getUrl() + "/api/share-api-report" + shareUrl); + paramMap.put("scenarioShareUrl", shareUrl); String context = "${operator}执行接口测试" + status + ": ${name}"; NoticeModel noticeModel = NoticeModel.builder().operator(userId) .context(context).subject("执行通知").paramMap(paramMap).event(event).build(); - Project project = projectMapper.selectByPrimaryKey(noticeDTO.getProjectId()); - noticeSendService.send(project, noticeType, noticeModel); } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiReportShareService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiReportShareService.java index a65c3c006a..1d17be8139 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiReportShareService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiReportShareService.java @@ -16,7 +16,8 @@ import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.mapper.ShareInfoMapper; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.Translator; -import io.metersphere.system.dto.sdk.SessionUser; +import io.metersphere.system.dto.user.UserDTO; +import io.metersphere.system.mapper.BaseUserMapper; import io.metersphere.system.uid.IDGenerator; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; @@ -42,6 +43,8 @@ public class ApiReportShareService { private ProjectApplicationMapper projectApplicationMapper; @Resource private ApiScenarioReportMapper apiScenarioReportMapper; + @Resource + private BaseUserMapper baseUserMapper; private static final Long DEFAULT = 1000L * 60 * 60 * 24; @@ -102,12 +105,13 @@ public class ApiReportShareService { return returnDTO; } - public ShareInfoDTO gen(ApiReportShareRequest shareRequest, SessionUser user) { - String lang = user.getLanguage() == null ? LocaleContextHolder.getLocale().toString() : user.getLanguage(); + public ShareInfoDTO gen(ApiReportShareRequest shareRequest, String userId) { + UserDTO userDTO = baseUserMapper.selectById(userId); + String lang = userDTO.getLanguage() == null ? LocaleContextHolder.getLocale().toString() : userDTO.getLanguage(); ShareInfo request = new ShareInfo(); BeanUtils.copyBean(request, shareRequest); request.setLang(lang); - request.setCreateUser(user.getId()); + request.setCreateUser(userId); request.setCustomData(shareRequest.getReportId().getBytes()); request.setShareType(ShareInfoType.API_SHARE_REPORT.name()); ShareInfo shareInfo = createShareInfo(request); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionImportUtilService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionImportUtilService.java index 59c963553a..86d8d565f0 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionImportUtilService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionImportUtilService.java @@ -14,8 +14,8 @@ import io.metersphere.api.dto.definition.ApiDefinitionDTO; import io.metersphere.api.dto.definition.ApiDefinitionPageRequest; import io.metersphere.api.dto.definition.ApiModuleRequest; import io.metersphere.api.dto.request.ImportRequest; -import io.metersphere.api.dto.request.http.MsHeader; import io.metersphere.api.dto.request.http.MsHTTPElement; +import io.metersphere.api.dto.request.http.MsHeader; import io.metersphere.api.dto.request.http.QueryParam; import io.metersphere.api.dto.request.http.RestParam; import io.metersphere.api.dto.request.http.body.*; @@ -34,13 +34,14 @@ import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.Translator; +import io.metersphere.system.domain.User; import io.metersphere.system.dto.sdk.ApiDefinitionCaseDTO; import io.metersphere.system.dto.sdk.BaseTreeNode; -import io.metersphere.system.dto.sdk.SessionUser; import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.log.dto.LogDTO; import io.metersphere.system.log.service.OperationLogService; +import io.metersphere.system.mapper.UserMapper; import io.metersphere.system.notice.constants.NoticeConstants; import io.metersphere.system.service.CommonNoticeSendService; import io.metersphere.system.uid.IDGenerator; @@ -88,6 +89,8 @@ public class ApiDefinitionImportUtilService { private OperationLogService operationLogService; @Resource private CommonNoticeSendService commonNoticeSendService; + @Resource + private UserMapper userMapper; private static final String FILE_JMX = "jmx"; private static final String FILE_HAR = "har"; @@ -111,7 +114,7 @@ public class ApiDefinitionImportUtilService { } } - public void importApi(ImportRequest request, ApiDefinitionImport apiImport, SessionUser user) { + public void importApi(ImportRequest request, ApiDefinitionImport apiImport) { String defaultVersion = extBaseProjectVersionMapper.getDefaultVersion(request.getProjectId()); request.setDefaultVersion(defaultVersion); if (request.getVersionId() == null) { @@ -133,11 +136,11 @@ public class ApiDefinitionImportUtilService { } //处理数据,判断数据是否重复 - dealWithData(request, filterData, user); + dealWithData(request, filterData); } - private void dealWithData(ImportRequest request, List importData, SessionUser user) { + private void dealWithData(ImportRequest request, List importData) { //查询数据库中所有的数据, 用于判断是否重复 ApiDefinitionPageRequest pageRequest = new ApiDefinitionPageRequest(); pageRequest.setProjectId(request.getProjectId()); @@ -176,7 +179,7 @@ public class ApiDefinitionImportUtilService { getNeedUpdateData(request, apiDealWithData, apiDetailWithDataUpdate); //数据入库 - insertData(modulePathMap, idModuleMap, apiDetailWithDataUpdate, request, user); + insertData(modulePathMap, idModuleMap, apiDetailWithDataUpdate, request); } @@ -219,7 +222,7 @@ public class ApiDefinitionImportUtilService { public void insertData(Map modulePathMap, Map idModuleMap, ApiDetailWithDataUpdate apiDetailWithDataUpdate, - ImportRequest request, SessionUser user) { + ImportRequest request) { //先判断是否需要新增模块 List addModuleData = apiDetailWithDataUpdate.getAddModuleData(); List updateModuleData = apiDetailWithDataUpdate.getUpdateModuleData(); @@ -387,11 +390,10 @@ public class ApiDefinitionImportUtilService { SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); operationLogService.batchAdd(operationLogs); //发送通知 - List createResources = new ArrayList<>(); - createResources.addAll(JSON.parseArray(JSON.toJSONString(createLists), Map.class)); + List createResources = new ArrayList<>(JSON.parseArray(JSON.toJSONString(createLists), Map.class)); + User user = userMapper.selectByPrimaryKey(request.getUserId()); commonNoticeSendService.sendNotice(NoticeConstants.TaskType.API_DEFINITION_TASK, NoticeConstants.Event.CREATE, createResources, user, request.getProjectId()); - List updateResources = new ArrayList<>(); - updateResources.addAll(JSON.parseArray(JSON.toJSONString(updateResources), Map.class)); + List updateResources = new ArrayList<>(JSON.parseArray(JSON.toJSONString(updateLists), Map.class)); commonNoticeSendService.sendNotice(NoticeConstants.TaskType.API_DEFINITION_TASK, NoticeConstants.Event.UPDATE, updateResources, user, request.getProjectId()); } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java index d45882723d..24614fe7df 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java @@ -952,7 +952,7 @@ public class ApiDefinitionService extends MoveNodeService { return apiDefinitionDocDTO; } - public void apiTestImport(MultipartFile file, ImportRequest request, SessionUser user, String projectId) { + public void apiTestImport(MultipartFile file, ImportRequest request, String projectId) { if (file != null) { String originalFilename = file.getOriginalFilename(); if (StringUtils.isNotBlank(originalFilename)) { @@ -977,7 +977,7 @@ public class ApiDefinitionService extends MoveNodeService { } try { - apiDefinitionImportUtilService.importApi(request, apiImport, user); + apiDefinitionImportUtilService.importApi(request, apiImport); } catch (Exception e) { LogUtils.error(e); throw new MSException(Translator.get("user_import_format_wrong")); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/schedule/SwaggerUrlImportJob.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/schedule/SwaggerUrlImportJob.java index cd6cd16f1e..9cf2b069d3 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/schedule/SwaggerUrlImportJob.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/schedule/SwaggerUrlImportJob.java @@ -8,11 +8,8 @@ import io.metersphere.api.service.definition.ApiDefinitionScheduleService; import io.metersphere.api.service.definition.ApiDefinitionService; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.CommonBeanFactory; -import io.metersphere.system.dto.sdk.SessionUser; -import io.metersphere.system.dto.user.UserDTO; import io.metersphere.system.schedule.BaseScheduleJob; import io.metersphere.system.service.UserService; -import io.metersphere.system.utils.SessionUtils; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobKey; @@ -40,10 +37,7 @@ public class SwaggerUrlImportJob extends BaseScheduleJob { request.setUserId(jobDataMap.getString("userId")); request.setType("SCHEDULE"); request.setResourceId(resourceId); - - UserDTO userDTO = userService.getUserDTOByKeyword(request.getUserId()); - SessionUser user = SessionUser.fromUser(userDTO, SessionUtils.getSessionId()); - apiDefinitionService.apiTestImport(null, request, user, request.getProjectId()); + apiDefinitionService.apiTestImport(null, request, request.getProjectId()); } public static JobKey getJobKey(String resourceId) { diff --git a/frontend/src/views/api-test/components/requestComposition/body.vue b/frontend/src/views/api-test/components/requestComposition/body.vue index 7f9eb6b98c..9c4554a14f 100644 --- a/frontend/src/views/api-test/components/requestComposition/body.vue +++ b/frontend/src/views/api-test/components/requestComposition/body.vue @@ -180,6 +180,9 @@ local: false, }; } + if (innerParams.value.binaryBody.file && !innerParams.value.binaryBody.file.fileId) { + innerParams.value.binaryBody.file = undefined; + } emit('change'); } catch (error) { // eslint-disable-next-line no-console diff --git a/frontend/src/views/api-test/components/requestComposition/response/edit.vue b/frontend/src/views/api-test/components/requestComposition/response/edit.vue index c29ff7db6a..d9a29a8ac3 100644 --- a/frontend/src/views/api-test/components/requestComposition/response/edit.vue +++ b/frontend/src/views/api-test/components/requestComposition/response/edit.vue @@ -385,6 +385,9 @@ local: false, }; } + if (activeResponse.value.body.binaryBody.file && !activeResponse.value.body.binaryBody.file.fileId) { + activeResponse.value.body.binaryBody.file = undefined; + } emit('change'); } catch (error) { // eslint-disable-next-line no-console diff --git a/frontend/src/views/api-test/management/components/import.vue b/frontend/src/views/api-test/management/components/import.vue index 872fe96143..b4946c669b 100644 --- a/frontend/src/views/api-test/management/components/import.vue +++ b/frontend/src/views/api-test/management/components/import.vue @@ -569,6 +569,7 @@ (item) => ({ ...item, operationTime: dayjs(item.operationTime).format('YYYY-MM-DD HH:mm:ss'), + nextTime: dayjs(item.nextTime).format('YYYY-MM-DD HH:mm:ss'), }) ); function loadTaskList() {