refactor: 文件上传接口文件名称去掉id (#2895)

* refactor: 文件上传接口文件名称去掉id

* refactor: 代码规范

* refactor: 代码规范

Co-authored-by: chenjianxing <jianxing.chen@fit2cloud.com>
Co-authored-by: jianxing <41557596+AgAngle@users.noreply.github.com>
This commit is contained in:
metersphere-bot 2021-05-18 12:08:08 +08:00 committed by GitHub
parent 3be5ddef29
commit 5464d603fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
33 changed files with 617 additions and 518 deletions

View File

@ -77,14 +77,16 @@ public class ApiAutomationController {
@PostMapping(value = "/create")
@MsAuditLog(module = "api_automation", type = OperLogConstants.CREATE, title = "#request.name", content = "#msClass.getLogDetails(#request.id)", msClass = ApiAutomationService.class)
public ApiScenario create(@RequestPart("request") SaveApiScenarioRequest request, @RequestPart(value = "files") List<MultipartFile> bodyFiles) {
return apiAutomationService.create(request, bodyFiles);
public ApiScenario create(@RequestPart("request") SaveApiScenarioRequest request, @RequestPart(value = "bodyFiles") List<MultipartFile> bodyFiles,
@RequestPart(value = "scenarioFiles") List<MultipartFile> scenarioFiles) {
return apiAutomationService.create(request, bodyFiles, scenarioFiles);
}
@PostMapping(value = "/update")
@MsAuditLog(module = "api_automation", type = OperLogConstants.UPDATE, beforeEvent = "#msClass.getLogDetails(#request.id)", title = "#request.name", content = "#msClass.getLogDetails(#request.id)", msClass = ApiAutomationService.class)
public void update(@RequestPart("request") SaveApiScenarioRequest request, @RequestPart(value = "files") List<MultipartFile> bodyFiles) {
apiAutomationService.update(request, bodyFiles);
public void update(@RequestPart("request") SaveApiScenarioRequest request, @RequestPart(value = "bodyFiles") List<MultipartFile> bodyFiles,
@RequestPart(value = "scenarioFiles") List<MultipartFile> scenarioFiles) {
apiAutomationService.update(request, bodyFiles, scenarioFiles);
}
@GetMapping("/delete/{id}")
@ -150,9 +152,10 @@ public class ApiAutomationController {
@PostMapping(value = "/run/debug")
@MsAuditLog(module = "api_automation", type = OperLogConstants.DEBUG, title = "#request.scenarioName", project = "#request.projectId")
public void runDebug(@RequestPart("request") RunDefinitionRequest request, @RequestPart(value = "files") List<MultipartFile> bodyFiles) {
public void runDebug(@RequestPart("request") RunDefinitionRequest request,
@RequestPart(value = "bodyFiles") List<MultipartFile> bodyFiles, @RequestPart(value = "scenarioFiles") List<MultipartFile> scenarioFiles) {
request.setExecuteType(ExecuteType.Debug.name());
apiAutomationService.debugRun(request, bodyFiles);
apiAutomationService.debugRun(request, bodyFiles, scenarioFiles);
}
@PostMapping(value = "/run")

View File

@ -42,7 +42,9 @@ public class SaveApiScenarioRequest {
private MsTestElement scenarioDefinition;
List<String> bodyUploadIds;
List<String> bodyFileRequestIds;
List<String> scenarioFileIds;
private List<String> scenarioIds;

View File

@ -17,6 +17,8 @@ public class RunDefinitionRequest {
private String reportId;
private String requestId;
private String name;
private String type;
@ -35,9 +37,11 @@ public class RunDefinitionRequest {
private Response response;
private RunModeConfig config;
List<String> bodyFileRequestIds;
private List<String> bodyUploadIds;
List<String> scenarioFileIds;
private RunModeConfig config;
private Map<String, String> environmentMap;
}

View File

@ -650,5 +650,24 @@ public class MsHTTPSamplerProxy extends MsTestElement {
private boolean isRest() {
return this.getRest().stream().filter(KeyValue::isEnable).filter(KeyValue::isValid).toArray().length > 0;
}
public static List<MsHTTPSamplerProxy> findHttpSampleFromHashTree(MsTestElement hashTree, List<MsHTTPSamplerProxy> requests) {
if (requests == null) {
requests = new ArrayList<>();
}
if (hashTree instanceof MsHTTPSamplerProxy) {
requests.add((MsHTTPSamplerProxy)hashTree);
} else {
if (hashTree!= null) {
LinkedList<MsTestElement> childHashTree = hashTree.getHashTree();
if (CollectionUtils.isNotEmpty(childHashTree)) {
for (MsTestElement item : childHashTree) {
findHttpSampleFromHashTree(item, requests);
}
}
}
}
return requests;
}
}

View File

@ -127,7 +127,13 @@ public class Body {
if (files != null) {
files.forEach(file -> {
String paramName = keyValue.getName() == null ? requestId : keyValue.getName();
String path = FileUtils.BODY_FILE_DIR + '/' + file.getId() + '_' + file.getName();
String path = null;
if (StringUtils.isNotBlank(file.getId())) {
// 旧数据
path = FileUtils.BODY_FILE_DIR + '/' + file.getId() + '_' + file.getName();
} else {
path = FileUtils.BODY_FILE_DIR + '/' + requestId + '/' + file.getName();
}
String mimetype = keyValue.getContentType();
list.add(new HTTPFileArg(path, paramName, mimetype));
});

View File

@ -229,7 +229,7 @@ public class ApiAutomationService {
extApiScenarioMapper.removeToGcByExample(example);
}
public ApiScenario create(SaveApiScenarioRequest request, List<MultipartFile> bodyFiles) {
public ApiScenario create(SaveApiScenarioRequest request, List<MultipartFile> bodyFiles, List<MultipartFile> scenarioFiles) {
request.setId(UUID.randomUUID().toString());
checkNameExist(request);
checkScenarioNum(request);
@ -243,11 +243,29 @@ public class ApiAutomationService {
apiScenarioMapper.insert(scenario);
List<String> bodyUploadIds = request.getBodyUploadIds();
FileUtils.createBodyFiles(bodyUploadIds, bodyFiles);
uploadFiles(request, bodyFiles, scenarioFiles);
return scenario;
}
private void uploadFiles(SaveApiScenarioRequest request, List<MultipartFile> bodyFiles, List<MultipartFile> scenarioFiles) {
FileUtils.createBodyFiles(request.getScenarioFileIds(), scenarioFiles);
List<String> bodyFileRequestIds = request.getBodyFileRequestIds();
if (CollectionUtils.isNotEmpty(bodyFileRequestIds)) {
bodyFileRequestIds.forEach(requestId -> {
FileUtils.createBodyFiles(requestId, bodyFiles);
});
}
}
private void uploadBodyFiles(List<String> bodyFileRequestIds, List<MultipartFile> bodyFiles) {
if (CollectionUtils.isNotEmpty(bodyFileRequestIds)) {
bodyFileRequestIds.forEach(requestId -> {
FileUtils.createBodyFiles(requestId, bodyFiles);
});
}
}
private void checkScenarioNum(SaveApiScenarioRequest request) {
if (StringUtils.isNotBlank(request.getCustomNum())) {
String projectId = request.getProjectId();
@ -285,18 +303,47 @@ public class ApiAutomationService {
}
}
public void update(SaveApiScenarioRequest request, List<MultipartFile> bodyFiles) {
public void update(SaveApiScenarioRequest request, List<MultipartFile> bodyFiles, List<MultipartFile> scenarioFiles) {
checkNameExist(request);
checkScenarioNum(request);
List<String> bodyUploadIds = request.getBodyUploadIds();
FileUtils.createBodyFiles(bodyUploadIds, bodyFiles);
//检查场景的请求步骤如果含有ESB请求步骤的话要做参数计算处理
esbApiParamService.checkScenarioRequests(request);
final ApiScenarioWithBLOBs scenario = buildSaveScenario(request);
deleteUpdateBodyFile(scenario);
apiScenarioMapper.updateByPrimaryKeySelective(scenario);
extScheduleMapper.updateNameByResourceID(request.getId(), request.getName());// 修改场景name同步到修改首页定时任务
uploadFiles(request, bodyFiles, scenarioFiles);
}
/**
* 更新时如果有删除自定义请求则删除对应body文件
* @param scenario
*/
public void deleteUpdateBodyFile(ApiScenarioWithBLOBs scenario) {
ApiScenarioWithBLOBs oldScenario = apiScenarioMapper.selectByPrimaryKey(scenario.getId());
Set<String> newRequestIds = getRequestIds(scenario.getScenarioDefinition());
MsTestElement msTestElement = parseScenarioDefinition(oldScenario.getScenarioDefinition());
List<MsHTTPSamplerProxy> oldRequests = MsHTTPSamplerProxy.findHttpSampleFromHashTree(msTestElement, null);
oldRequests.forEach(item -> {
if (item.isCustomizeReq() && !newRequestIds.contains(item.getId())) {
FileUtils.deleteBodyFiles(item.getId());
}
});
}
public MsScenario parseScenarioDefinition(String scenarioDefinition) {
MsScenario scenario = JSONObject.parseObject(scenarioDefinition, MsScenario.class);
parse(scenarioDefinition, scenario);
return scenario;
}
public Set<String> getRequestIds(String scenarioDefinition) {
MsScenario msScenario = parseScenarioDefinition(scenarioDefinition);
List<MsHTTPSamplerProxy> httpSampleFromHashTree = MsHTTPSamplerProxy.findHttpSampleFromHashTree(msScenario, null);
return httpSampleFromHashTree.stream()
.map(MsHTTPSamplerProxy::getId).collect(Collectors.toSet());
}
public ApiScenarioWithBLOBs buildSaveScenario(SaveApiScenarioRequest request) {
@ -373,6 +420,32 @@ public class ApiAutomationService {
testPlanApiScenarioMapper.deleteByExample(example);
}
deleteBodyFileByScenarioId(scenarioId);
}
public void deleteBodyFileByScenarioId(String scenarioId) {
ApiScenarioWithBLOBs apiScenarioWithBLOBs = apiScenarioMapper.selectByPrimaryKey(scenarioId);
String scenarioDefinition = apiScenarioWithBLOBs.getScenarioDefinition();
deleteBodyFile(scenarioDefinition);
}
public void deleteBodyFileByScenarioIds(List<String> ids) {
ApiScenarioExample example = new ApiScenarioExample();
example.createCriteria().andIdIn(ids);
List<ApiScenarioWithBLOBs> apiScenarios = apiScenarioMapper.selectByExampleWithBLOBs(example);
apiScenarios.forEach((item) -> {
deleteBodyFile(item.getScenarioDefinition());
});
}
public void deleteBodyFile(String scenarioDefinition) {
MsTestElement msTestElement = parseScenarioDefinition(scenarioDefinition);
List<MsHTTPSamplerProxy> httpSampleFromHashTree = MsHTTPSamplerProxy.findHttpSampleFromHashTree(msTestElement, null);
httpSampleFromHashTree.forEach((httpSamplerProxy) -> {
if (httpSamplerProxy.isCustomizeReq()) {
FileUtils.deleteBodyFiles(httpSamplerProxy.getId());
}
});
}
private void deleteApiScenarioReport(List<String> scenarioIds) {
@ -411,7 +484,7 @@ public class ApiAutomationService {
example.createCriteria().andIdIn(testPlanApiScenarioIdList);
testPlanApiScenarioMapper.deleteByExample(example);
}
deleteBodyFileByScenarioIds(scenarioIds);
}
public void deleteBatch(List<String> ids) {
@ -446,86 +519,89 @@ public class ApiAutomationService {
return apiScenarioMapper.selectByPrimaryKey(id);
}
public LinkedList<MsTestElement> getScenarioHashTree(String definition) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JSONObject element = JSON.parseObject(definition);
try {
return objectMapper.readValue(element.getString("hashTree"), new TypeReference<LinkedList<MsTestElement>>() {});
} catch (JsonProcessingException e) {
LogUtil.error(e.getMessage(), e);
}
return new LinkedList<>();
}
public ScenarioEnv getApiScenarioEnv(String definition) {
ObjectMapper mapper = new ObjectMapper();
ScenarioEnv env = new ScenarioEnv();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
try {
JSONObject element = JSON.parseObject(definition);
List<MsTestElement> hashTree = mapper.readValue(element.getString("hashTree"), new TypeReference<LinkedList<MsTestElement>>() {
});
for (int i = 0; i < hashTree.size(); i++) {
MsTestElement tr = hashTree.get(i);
String referenced = tr.getReferenced();
if (StringUtils.equals(MsTestElementConstants.REF.name(), referenced)) {
if (StringUtils.equals(tr.getType(), "HTTPSamplerProxy")) {
MsHTTPSamplerProxy http = (MsHTTPSamplerProxy) tr;
String refType = tr.getRefType();
if (StringUtils.equals(refType, "CASE")) {
http.setUrl(null);
} else {
ApiDefinition apiDefinition = apiDefinitionService.get(tr.getId());
if (apiDefinition != null) {
http.setUrl(apiDefinition.getPath());
}
}
if (http.isEnable()) {
if (StringUtils.isBlank(http.getUrl()) || !tr.isURL(http.getUrl())) {
env.getProjectIds().add(http.getProjectId());
env.setFullUrl(false);
}
}
} else if (StringUtils.equals(tr.getType(), "JDBCSampler") || StringUtils.equals(tr.getType(), "TCPSampler")) {
if (StringUtils.equals(tr.getRefType(), "CASE")) {
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = apiTestCaseService.get(tr.getId());
if (apiTestCaseWithBLOBs != null) {
env.getProjectIds().add(apiTestCaseWithBLOBs.getProjectId());
}
} else {
ApiDefinition apiDefinition = apiDefinitionService.get(tr.getId());
if (apiDefinition != null) {
env.getProjectIds().add(apiDefinition.getProjectId());
}
}
} else if (StringUtils.equals(tr.getType(), "scenario")) {
if (tr.isEnable()) {
ApiScenarioWithBLOBs apiScenario = getApiScenario(tr.getId());
if (apiScenario != null) {
env.getProjectIds().add(apiScenario.getProjectId());
String scenarioDefinition = apiScenario.getScenarioDefinition();
JSONObject element1 = JSON.parseObject(scenarioDefinition);
LinkedList<MsTestElement> hashTree1 = mapper.readValue(element1.getString("hashTree"), new TypeReference<LinkedList<MsTestElement>>() {
});
tr.setHashTree(hashTree1);
}
List<MsTestElement> hashTree = getScenarioHashTree(definition);
for (int i = 0; i < hashTree.size(); i++) {
MsTestElement tr = hashTree.get(i);
String referenced = tr.getReferenced();
if (StringUtils.equals(MsTestElementConstants.REF.name(), referenced)) {
if (StringUtils.equals(tr.getType(), "HTTPSamplerProxy")) {
MsHTTPSamplerProxy http = (MsHTTPSamplerProxy) tr;
String refType = tr.getRefType();
if (StringUtils.equals(refType, "CASE")) {
http.setUrl(null);
} else {
ApiDefinition apiDefinition = apiDefinitionService.get(tr.getId());
if (apiDefinition != null) {
http.setUrl(apiDefinition.getPath());
}
}
} else {
if (StringUtils.equals(tr.getType(), "HTTPSamplerProxy")) {
// 校验是否是全路径
MsHTTPSamplerProxy httpSamplerProxy = (MsHTTPSamplerProxy) tr;
if (httpSamplerProxy.isEnable()) {
if (StringUtils.isBlank(httpSamplerProxy.getUrl()) || !tr.isURL(httpSamplerProxy.getUrl())) {
env.getProjectIds().add(httpSamplerProxy.getProjectId());
env.setFullUrl(false);
}
if (http.isEnable()) {
if (StringUtils.isBlank(http.getUrl()) || !tr.isURL(http.getUrl())) {
env.getProjectIds().add(http.getProjectId());
env.setFullUrl(false);
}
}
} else if (StringUtils.equals(tr.getType(), "JDBCSampler") || StringUtils.equals(tr.getType(), "TCPSampler")) {
if (StringUtils.equals(tr.getRefType(), "CASE")) {
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = apiTestCaseService.get(tr.getId());
if (apiTestCaseWithBLOBs != null) {
env.getProjectIds().add(apiTestCaseWithBLOBs.getProjectId());
}
} else {
ApiDefinition apiDefinition = apiDefinitionService.get(tr.getId());
if (apiDefinition != null) {
env.getProjectIds().add(apiDefinition.getProjectId());
}
}
} else if (StringUtils.equals(tr.getType(), "scenario")) {
if (tr.isEnable()) {
ApiScenarioWithBLOBs apiScenario = getApiScenario(tr.getId());
if (apiScenario != null) {
env.getProjectIds().add(apiScenario.getProjectId());
String scenarioDefinition = apiScenario.getScenarioDefinition();
tr.setHashTree(getScenarioHashTree(scenarioDefinition));
}
} else if (StringUtils.equals(tr.getType(), "JDBCSampler") || StringUtils.equals(tr.getType(), "TCPSampler")) {
env.getProjectIds().add(tr.getProjectId());
}
}
if (!tr.isEnable()) {
continue;
}
if (StringUtils.equals(tr.getType(), "scenario")) {
} else {
if (StringUtils.equals(tr.getType(), "HTTPSamplerProxy")) {
// 校验是否是全路径
MsHTTPSamplerProxy httpSamplerProxy = (MsHTTPSamplerProxy) tr;
if (httpSamplerProxy.isEnable()) {
if (StringUtils.isBlank(httpSamplerProxy.getUrl()) || !tr.isURL(httpSamplerProxy.getUrl())) {
env.getProjectIds().add(httpSamplerProxy.getProjectId());
env.setFullUrl(false);
}
}
} else if (StringUtils.equals(tr.getType(), "JDBCSampler") || StringUtils.equals(tr.getType(), "TCPSampler")) {
env.getProjectIds().add(tr.getProjectId());
}
if (CollectionUtils.isNotEmpty(tr.getHashTree())) {
getHashTree(tr.getHashTree(), env);
}
}
} catch (JsonProcessingException e) {
e.printStackTrace();
if (!tr.isEnable()) {
continue;
}
if (StringUtils.equals(tr.getType(), "scenario")) {
env.getProjectIds().add(tr.getProjectId());
}
if (CollectionUtils.isNotEmpty(tr.getHashTree())) {
getHashTree(tr.getHashTree(), env);
}
}
return env;
}
@ -1168,9 +1244,7 @@ public class ApiAutomationService {
* @param bodyFiles
* @return
*/
public String debugRun(RunDefinitionRequest request, List<MultipartFile> bodyFiles) {
List<String> bodyUploadIds = new ArrayList<>(request.getBodyUploadIds());
FileUtils.createBodyFiles(bodyUploadIds, bodyFiles);
public String debugRun(RunDefinitionRequest request, List<MultipartFile> bodyFiles, List<MultipartFile> scenarioFiles) {
Map<String, EnvironmentConfig> envConfig = new HashMap<>();
Map<String, String> map = request.getEnvironmentMap();
if (map != null) {
@ -1189,12 +1263,13 @@ public class ApiAutomationService {
MSException.throwException(e.getMessage());
}
// 调用执行方法
APIScenarioReportResult report = createScenarioReport(request.getId(), request.getScenarioId(), request.getScenarioName(), ReportTriggerMode.MANUAL.name(), request.getExecuteType(), request.getProjectId(),
SessionUtils.getUserId(), null);
apiScenarioReportMapper.insert(report);
// 调用执行方法
// jMeterService.runTest(request.getId(), hashTree, ApiRunMode.SCENARIO.name(), true, null);
uploadBodyFiles(request.getBodyFileRequestIds(), bodyFiles);
FileUtils.createBodyFiles(request.getScenarioFileIds(), scenarioFiles);
// 调用执行方法
jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), ApiRunMode.SCENARIO.name());
return request.getId();

View File

@ -48,14 +48,12 @@ import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.jorphan.collections.HashTree;
import org.aspectj.util.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import sun.security.util.Cache;
import javax.annotation.Resource;
import java.io.File;
import java.net.MalformedURLException;
import java.util.*;
import java.util.function.Function;
@ -169,25 +167,23 @@ public class ApiDefinitionService {
}
public void create(SaveApiDefinitionRequest request, List<MultipartFile> bodyFiles) {
List<String> bodyUploadIds = new ArrayList<>(request.getBodyUploadIds());
if (StringUtils.equals(request.getProtocol(), "DUBBO")) {
request.setMethod("dubbo://");
}
createTest(request);
FileUtils.createBodyFiles(bodyUploadIds, bodyFiles);
FileUtils.createBodyFiles(request.getRequest().getId(), bodyFiles);
}
public ApiDefinitionWithBLOBs update(SaveApiDefinitionRequest request, List<MultipartFile> bodyFiles) {
if (request.getRequest() != null) {
deleteFileByTestId(request.getRequest().getId());
}
List<String> bodyUploadIds = request.getBodyUploadIds();
request.setBodyUploadIds(null);
if (StringUtils.equals(request.getProtocol(), "DUBBO")) {
request.setMethod("dubbo://");
}
ApiDefinitionWithBLOBs returnModel = updateTest(request);
FileUtils.createBodyFiles(bodyUploadIds, bodyFiles);
FileUtils.createBodyFiles(request.getRequest().getId(), bodyFiles);
return returnModel;
}
@ -197,7 +193,7 @@ public class ApiDefinitionService {
extApiDefinitionExecResultMapper.deleteByResourceId(apiId);
apiDefinitionMapper.deleteByPrimaryKey(apiId);
esbApiParamService.deleteByResourceId(apiId);
deleteBodyFiles(apiId);
FileUtils.deleteBodyFiles(apiId);
}
public void deleteBatch(List<String> apiIds) {
@ -219,14 +215,6 @@ public class ApiDefinitionService {
}
}
public void deleteBodyFiles(String apiId) {
File file = new File(BODY_FILE_DIR + "/" + apiId);
FileUtil.deleteContents(file);
if (file.exists()) {
file.delete();
}
}
private void checkNameExist(SaveApiDefinitionRequest request) {
ApiDefinitionExample example = new ApiDefinitionExample();
if (request.getProtocol().equals(RequestType.HTTP)) {
@ -552,8 +540,6 @@ public class ApiDefinitionService {
* @return
*/
public String run(RunDefinitionRequest request, List<MultipartFile> bodyFiles) {
//检查是否是ESB请求ESB请求需要根据数据结构更换参数
request = esbApiParamService.checkIsEsbRequest(request);
int count = 100;
BaseSystemConfigDTO dto = systemParameterService.getBaseInfo();
if (StringUtils.isNotEmpty(dto.getConcurrency())) {
@ -562,8 +548,6 @@ public class ApiDefinitionService {
if (request.getTestElement() != null && request.getTestElement().getHashTree().size() == 1 && request.getTestElement().getHashTree().get(0).getHashTree().size() > count) {
MSException.throwException("并发数量过大,请重新选择!");
}
List<String> bodyUploadIds = new ArrayList<>(request.getBodyUploadIds());
FileUtils.createBodyFiles(bodyUploadIds, bodyFiles);
ParameterConfig config = new ParameterConfig();
config.setProjectId(request.getProjectId());
@ -577,11 +561,23 @@ public class ApiDefinitionService {
config.setConfig(envConfig);
}
List<MsHTTPSamplerProxy> requests = MsHTTPSamplerProxy.findHttpSampleFromHashTree(request.getTestElement(), null);
// 单接口调试生成tmp临时目录
requests.forEach(item -> {
String originId = item.getId();
item.setId("tmp/" + UUID.randomUUID().toString());
FileUtils.copyBdyFile(originId, item.getId());
FileUtils.createBodyFiles(item.getId(), bodyFiles);
});
HashTree hashTree = request.getTestElement().generateHashTree(config);
String runMode = ApiRunMode.DEFINITION.name();
if (StringUtils.isNotBlank(request.getType()) && StringUtils.equals(request.getType(), ApiRunMode.API_PLAN.name())) {
runMode = ApiRunMode.API_PLAN.name();
}
// 调用执行方法
if (request.getConfig() != null && StringUtils.isNotBlank(request.getConfig().getResourcePoolId())) {
jMeterService.runTest(request.getId(), hashTree, runMode, request.getReportId() != null, request.getConfig());

View File

@ -180,9 +180,8 @@ public class ApiTestCaseService {
}
public ApiTestCase create(SaveApiTestCaseRequest request, List<MultipartFile> bodyFiles) {
List<String> bodyUploadIds = new ArrayList<>(request.getBodyUploadIds());
ApiTestCase test = createTest(request);
FileUtils.createBodyFiles(bodyUploadIds, bodyFiles);
FileUtils.createBodyFiles(request.getId(), bodyFiles);
return test;
}
@ -195,10 +194,9 @@ public class ApiTestCaseService {
public ApiTestCase update(SaveApiTestCaseRequest request, List<MultipartFile> bodyFiles) {
deleteFileByTestId(request.getId());
List<String> bodyUploadIds = new ArrayList<>(request.getBodyUploadIds());
request.setBodyUploadIds(null);
ApiTestCase test = updateTest(request);
FileUtils.createBodyFiles(bodyUploadIds, bodyFiles);
FileUtils.createBodyFiles(request.getId(), bodyFiles);
return test;
}
@ -293,7 +291,6 @@ public class ApiTestCaseService {
}
private ApiTestCase createTest(SaveApiTestCaseRequest request) {
request.setId(UUID.randomUUID().toString());
checkNameExist(request);
if (StringUtils.isNotEmpty(request.getEsbDataStruct()) || StringUtils.isNotEmpty(request.getBackEsbDataStruct())) {
@ -318,6 +315,7 @@ public class ApiTestCaseService {
} else {
test.setTags(request.getTags());
}
FileUtils.copyBdyFile(request.getApiDefinitionId(), request.getId());
apiTestCaseMapper.insert(test);
return test;
}

View File

@ -42,6 +42,35 @@ public class FileUtils {
}
}
public static void createBodyFiles(String requestId, List<MultipartFile> bodyFiles) {
if (CollectionUtils.isNotEmpty(bodyFiles) && StringUtils.isNotBlank(requestId)) {
String path = BODY_FILE_DIR + "/" + requestId;
File testDir = new File(path);
if (!testDir.exists()) {
testDir.mkdirs();
}
bodyFiles.forEach(item -> {
File file = new File(path + "/" + item.getOriginalFilename());
try (InputStream in = item.getInputStream(); OutputStream out = new FileOutputStream(file)) {
file.createNewFile();
FileUtil.copyStream(in, out);
} catch (IOException e) {
LogUtil.error(e);
MSException.throwException(Translator.get("upload_fail"));
}
});
}
}
public static void copyBdyFile(String originId, String toId) {
try {
FileUtil.copyDir(new File(FileUtils.BODY_FILE_DIR + "/" + originId),
new File(FileUtils.BODY_FILE_DIR + "/" + toId));
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
}
}
public static void createBodyFiles(List<String> bodyUploadIds, List<MultipartFile> bodyFiles) {
FileUtils.create(bodyUploadIds, bodyFiles, null);
}
@ -67,8 +96,9 @@ public class FileUtils {
return file.getPath();
}
public static void delFile(String path) {
File file = new File(path);
public static void deleteBodyFiles(String requestId) {
File file = new File(BODY_FILE_DIR + "/" + requestId);
FileUtil.deleteContents(file);
if (file.exists()) {
file.delete();
}

View File

@ -25,13 +25,15 @@ import java.util.UUID;
* @Description
*/
public class ApiScenarioTestJob extends MsScheduleJob {
private String projectID;
private String projectID;
private List<String> scenarioIds;
private ApiAutomationService apiAutomationService;
public ApiScenarioTestJob() {
apiAutomationService = (ApiAutomationService) CommonBeanFactory.getBean(ApiAutomationService.class);
apiAutomationService = CommonBeanFactory.getBean(ApiAutomationService.class);
}
@Override

View File

@ -13,7 +13,7 @@ public class ApiTestJob extends MsScheduleJob {
private APITestService apiTestService;
public ApiTestJob() {
apiTestService = (APITestService) CommonBeanFactory.getBean(APITestService.class);
apiTestService = CommonBeanFactory.getBean(APITestService.class);
}
@Override

View File

@ -0,0 +1,16 @@
package io.metersphere.job.sechedule;
import io.metersphere.commons.utils.FileUtils;
import io.metersphere.xmind.utils.FileUtil;
import org.quartz.JobExecutionContext;
import java.io.File;
public class ClearJob extends MsScheduleJob {
@Override
void businessExecute(JobExecutionContext context) {
// 清理调试产生的body文件
FileUtil.deleteDir(new File(FileUtils.BODY_FILE_DIR + "/tmp"));
}
}

@ -1 +1 @@
Subproject commit 9a95a444a4f0a477427f94bd107571430d85e766
Subproject commit e435fe1d01a2495481efa58daf3875be07b2ac9b

View File

@ -17,6 +17,11 @@ SET load_test_report.test_name = load_test.name;
alter table schedule
add config VARCHAR(500) null;
-- 自动清理调试用的body文件
-- INSERT INTO schedule (id,`key`,`type`,value,`group`,job,enable,resource_id,user_id,workspace_id,create_time,update_time,project_id,name)
-- VALUES ('f7f99f50-850b-41b2-8cb9-edee70669c4b','f7f99f50-850b-41b2-8cb9-edee70669c4b','CRON','0 0 4 * * ?','CLEAR_JOB','io.metersphere.job.sechedule.ClearJob',1,'system','admin','system',unix_timestamp() * 1000,unix_timestamp() * 1000,'system','ClearJob');
CREATE TABLE `operating_log` (
`id` varchar(50) NOT NULL COMMENT 'ID',
`project_id` varchar(50) NOT NULL COMMENT 'Project ID',
@ -42,3 +47,4 @@ ALTER TABLE api_scenario_module ADD create_user VARCHAR(100) NULL;
ALTER TABLE api_scenario_report ADD create_user VARCHAR(100) NULL;
ALTER TABLE test_case_node ADD create_user VARCHAR(100) NULL;
ALTER TABLE test_case ADD create_user VARCHAR(100) NULL;

View File

@ -0,0 +1,93 @@
import {getUUID} from "@/common/js/utils";
import {getUploadConfig, request} from "@/common/js/ajax";
function buildBodyFile(item, bodyUploadFiles, obj, bodyParam) {
if (bodyParam) {
bodyParam.forEach(param => {
if (param.files) {
param.files.forEach(fileItem => {
if (fileItem.file) {
fileItem.name = fileItem.file.name;
obj.bodyFileRequestIds.push(item.id);
bodyUploadFiles.push(fileItem.file);
}
});
}
});
}
}
function setFiles(item, bodyUploadFiles, obj) {
if (item.body) {
buildBodyFile(item, bodyUploadFiles, obj, item.body.kvs);
buildBodyFile(item, bodyUploadFiles, obj, item.body.binary);
}
}
function recursiveFile(arr, bodyUploadFiles, obj) {
arr.forEach(item => {
setFiles(item, bodyUploadFiles, obj);
if (item.hashTree !== undefined && item.hashTree.length > 0) {
recursiveFile(item.hashTree, bodyUploadFiles, obj);
}
});
}
export function getBodyUploadFiles(obj, scenarioDefinition) {
let bodyUploadFiles = [];
obj.bodyFileRequestIds = [];
scenarioDefinition.forEach(item => {
setFiles(item, bodyUploadFiles, obj);
if (item.hashTree !== undefined && item.hashTree.length > 0) {
recursiveFile(item.hashTree, bodyUploadFiles, obj);
}
})
return bodyUploadFiles;
}
function getScenarioFiles(obj) {
let scenarioFiles = [];
obj.scenarioFileIds = [];
// 场景变量csv 文件
if (obj.variables) {
obj.variables.forEach(param => {
if (param.type === 'CSV' && param.files) {
param.files.forEach(item => {
if (item.file) {
if (!item.id) {
let fileId = getUUID().substring(0, 12);
item.name = item.file.name;
item.id = fileId;
}
obj.scenarioFileIds.push(item.id);
scenarioFiles.push(item.file);
}
})
}
});
}
return scenarioFiles;
}
export function saveScenario(url, scenario, scenarioDefinition, success) {
let bodyFiles = getBodyUploadFiles(scenario, scenarioDefinition);
let scenarioFiles = getScenarioFiles(scenario);
let formData = new FormData();
if (bodyFiles) {
bodyFiles.forEach(f => {
formData.append("bodyFiles", f);
})
}
if (scenarioFiles) {
scenarioFiles.forEach(f => {
formData.append("scenarioFiles", f);
})
}
formData.append('request', new Blob([JSON.stringify(scenario)], {type: "application/json"}));
let axiosRequestConfig = getUploadConfig(url, formData);
request(axiosRequestConfig, (response) => {
if (success) {
success(response);
}
});
}

View File

@ -60,6 +60,7 @@
import {WORKSPACE_ID} from '@/common/js/constants';
import {getCurrentUser, getUUID} from "@/common/js/utils";
import MsDialogFooter from "@/business/components/common/components/MsDialogFooter";
import {saveScenario} from "@/business/components/api/automation/api-automation";
export default {
name: "MsAddBasisScenario",
@ -100,7 +101,7 @@
this.$emit('saveAsEdit', this.scenarioForm);
this.visible = false;
} else {
this.$fileUpload(path, null, [], this.scenarioForm, () => {
saveScenario(path, this.scenarioForm, [], () => {
this.visible = false;
this.$emit('refresh');
});

View File

@ -2,8 +2,9 @@
<div></div>
</template>
<script>
import {getUUID, getCurrentProjectID, strMapToObj} from "@/common/js/utils";
import {getCurrentProjectID, strMapToObj} from "@/common/js/utils";
import {createComponent} from "../../definition/components/jmeter/components";
import {saveScenario} from "@/business/components/api/automation/api-automation";
export default {
name: 'MsDebugRun',
@ -30,82 +31,6 @@ import {getUUID, getCurrentProjectID, strMapToObj} from "@/common/js/utils";
}
},
methods: {
setFiles(item, bodyUploadFiles, obj) {
if (item.body) {
if (item.body.kvs) {
item.body.kvs.forEach(param => {
if (param.files) {
param.files.forEach(item => {
if (item.file) {
if (!item.id) {
let fileId = getUUID().substring(0, 12);
item.name = item.file.name;
item.id = fileId;
}
obj.bodyUploadIds.push(item.id);
bodyUploadFiles.push(item.file);
}
});
}
});
}
if (item.body.binary) {
item.body.binary.forEach(param => {
if (param.files) {
param.files.forEach(item => {
if (item.file) {
if (!item.id) {
let fileId = getUUID().substring(0, 12);
item.name = item.file.name;
item.id = fileId;
}
obj.bodyUploadIds.push(item.id);
bodyUploadFiles.push(item.file);
}
});
}
});
}
}
if (item && item.files) {
item.files.forEach(fileItem => {
if (fileItem.file) {
if (!fileItem.id) {
let fileId = getUUID().substring(0, 12);
fileItem.name = fileItem.file.name;
fileItem.id = fileId;
}
obj.bodyUploadIds.push(fileItem.id);
bodyUploadFiles.push(fileItem.file);
}
});
}
},
recursiveFile(arr, bodyUploadFiles, obj) {
arr.forEach(item => {
this.setFiles(item, bodyUploadFiles, obj);
if (item.hashTree != undefined && item.hashTree.length > 0) {
this.recursiveFile(item.hashTree, bodyUploadFiles, obj);
}
});
},
getBodyUploadFiles(obj) {
let bodyUploadFiles = [];
obj.bodyUploadIds = [];
let request = this.runData;
request.hashTree.forEach(item => {
this.setFiles(item, bodyUploadFiles, obj);
if (item.hashTree != undefined && item.hashTree.length > 0) {
this.recursiveFile(item.hashTree, bodyUploadFiles, obj);
}
})
if (request.variables) {
request.variables.forEach(item => {
this.setFiles(item, bodyUploadFiles, obj);
})
}
return bodyUploadFiles;
},
run() {
let testPlan = createComponent('TestPlan');
let threadGroup = createComponent('ThreadGroup');
@ -118,12 +43,9 @@ import {getUUID, getCurrentProjectID, strMapToObj} from "@/common/js/utils";
testPlan.hashTree.push(threadGroup);
let reqObj = {id: this.reportId, reportId: this.reportId, scenarioName: this.runData.name,
scenarioId: this.runData.id, testElement: testPlan, projectId: getCurrentProjectID(), environmentMap: strMapToObj(map)};
let bodyFiles = this.getBodyUploadFiles(reqObj);
let url = "/api/automation/run/debug";
this.$fileUpload(url, null, bodyFiles, reqObj, response => {
saveScenario('/api/automation/run/debug', reqObj, this.runData.hashTree, (response) => {
this.runId = response.data;
this.$emit('runRefresh', {});
}, error => {
});
},
}

View File

@ -261,6 +261,7 @@
import ScenarioHeader from "./maximize/ScenarioHeader";
import MsDrawer from "../../../common/components/MsDrawer";
import MsSelectTree from "../../../common/select-tree/SelectTree";
import {saveScenario} from "@/business/components/api/automation/api-automation";
let jsonPath = require('jsonpath');
export default {
@ -913,81 +914,6 @@
this.expandedNode.splice(this.expandedNode.indexOf(data.resourceId), 1);
}
},
setFiles(item, bodyUploadFiles, obj) {
if (item.body) {
if (item.body.kvs) {
item.body.kvs.forEach(param => {
if (param.files) {
param.files.forEach(item => {
if (item.file) {
if (!item.id) {
let fileId = getUUID().substring(0, 12);
item.name = item.file.name;
item.id = fileId;
}
obj.bodyUploadIds.push(item.id);
bodyUploadFiles.push(item.file);
}
});
}
});
}
if (item.body.binary) {
item.body.binary.forEach(param => {
if (param.files) {
param.files.forEach(item => {
if (item.file) {
if (!item.id) {
let fileId = getUUID().substring(0, 12);
item.name = item.file.name;
item.id = fileId;
}
obj.bodyUploadIds.push(item.id);
bodyUploadFiles.push(item.file);
}
});
}
});
}
}
},
recursiveFile(arr, bodyUploadFiles, obj) {
arr.forEach(item => {
this.setFiles(item, bodyUploadFiles, obj);
if (item.hashTree != undefined && item.hashTree.length > 0) {
this.recursiveFile(item.hashTree, bodyUploadFiles, obj);
}
});
},
getBodyUploadFiles(obj) {
let bodyUploadFiles = [];
obj.bodyUploadIds = [];
this.scenarioDefinition.forEach(item => {
this.setFiles(item, bodyUploadFiles, obj);
if (item.hashTree != undefined && item.hashTree.length > 0) {
this.recursiveFile(item.hashTree, bodyUploadFiles, obj);
}
})
// csv
if (this.currentScenario.variables) {
this.currentScenario.variables.forEach(param => {
if (param.type === 'CSV' && param.files) {
param.files.forEach(item => {
if (item.file) {
if (!item.id) {
let fileId = getUUID().substring(0, 12);
item.name = item.file.name;
item.id = fileId;
}
obj.bodyUploadIds.push(item.id);
bodyUploadFiles.push(item.file);
}
})
}
})
}
return bodyUploadFiles;
},
editScenario() {
if (!document.getElementById("inputDelay")) {
return;
@ -997,8 +923,7 @@
this.$refs['currentScenario'].validate((valid) => {
if (valid) {
this.setParameter();
let bodyFiles = this.getBodyUploadFiles(this.currentScenario);
this.$fileUpload(this.path, null, bodyFiles, this.currentScenario, response => {
saveScenario(this.path, this.currentScenario, this.scenarioDefinition, (response) => {
this.$success(this.$t('commons.save_success'));
this.path = "/api/automation/update";
if (response.data) {
@ -1009,7 +934,7 @@
}
this.$emit('refresh', this.currentScenario);
resolve();
})
});
}
})
});

View File

@ -233,9 +233,11 @@
getEnvironments() {
this.environment = {};
let id = this.envMap.get(this.request.projectId);
this.$get('/api/environment/get/' + id, response => {
this.environment = response.data;
});
if (id) {
this.$get('/api/environment/get/' + id, response => {
this.environment = response.data;
});
}
},
remove() {
this.$emit('remove', this.request, this.node);

View File

@ -54,7 +54,7 @@
methods: {
getExecResult() {
//
if (this.apiItem) {
if (this.apiItem && this.apiItem.id) {
let url = "/api/definition/report/getReport/" + this.apiItem.id;
this.$get(url, response => {
if (response.data) {

View File

@ -160,6 +160,7 @@
import MsContainer from "../../../../common/components/MsContainer";
import MsMainContainer from "../../../../common/components/MsMainContainer";
import MsAsideContainer from "./MsLeftContainer";
import {saveScenario} from "@/business/components/api/automation/api-automation";
let jsonPath = require('jsonpath');
export default {
@ -817,43 +818,13 @@
}
});
},
getBodyUploadFiles(obj) {
let bodyUploadFiles = [];
obj.bodyUploadIds = [];
this.scenarioDefinition.forEach(item => {
this.setFiles(item, bodyUploadFiles, obj);
if (item.hashTree != undefined && item.hashTree.length > 0) {
this.recursiveFile(item.hashTree, bodyUploadFiles, obj);
}
})
// csv
if (this.currentScenario.variables) {
this.currentScenario.variables.forEach(param => {
if (param.type === 'CSV' && param.files) {
param.files.forEach(item => {
if (item.file) {
if (!item.id) {
let fileId = getUUID().substring(0, 12);
item.name = item.file.name;
item.id = fileId;
}
obj.bodyUploadIds.push(item.id);
bodyUploadFiles.push(item.file);
}
})
}
})
}
return bodyUploadFiles;
},
editScenario() {
return new Promise((resolve, reject) => {
document.getElementById("inputDelay").focus(); // input
this.$refs['currentScenario'].validate((valid) => {
if (valid) {
this.setParameter();
let bodyFiles = this.getBodyUploadFiles(this.currentScenario);
this.$fileUpload(this.path, null, bodyFiles, this.currentScenario, response => {
saveScenario(this.path, this.currentScenario, this.scenarioDefinition, (response) => {
this.$success(this.$t('commons.save_success'));
this.path = "/api/automation/update";
if (response.data) {
@ -864,7 +835,7 @@
}
this.$emit('refresh', this.currentScenario);
resolve();
})
});
}
})
});

View File

@ -203,6 +203,7 @@ export default {
saveApi(data) {
this.setParameters(data);
let bodyFiles = this.getBodyUploadFiles(data);
data.requestId = data.request.id;
this.$fileUpload(this.reqUrl, null, bodyFiles, data, () => {
this.$success(this.$t('commons.save_success'));
this.reqUrl = "/api/definition/update";
@ -236,10 +237,10 @@ export default {
if (param.files) {
param.files.forEach(item => {
if (item.file) {
let fileId = getUUID().substring(0, 8);
// let fileId = getUUID().substring(0, 8);
item.name = item.file.name;
item.id = fileId;
data.bodyUploadIds.push(fileId);
// item.id = fileId;
// data.bodyUploadIds.push(fileId);
bodyUploadFiles.push(item.file);
}
});

View File

@ -1,5 +1,5 @@
<template>
<el-card style="margin-top: 5px" @click.native="selectTestCase(apiCase,$event)">
<el-card v-loading="result.loading" style="margin-top: 5px" @click.native="selectTestCase(apiCase,$event)">
<div @click="active(apiCase)" v-if="type!=='detail'">
<el-row>
<el-col :span="3">
@ -324,6 +324,8 @@
}
}
tmp.id = tmp.request.id;
if (tmp.request.esbDataStruct != null) {
tmp.esbDataStruct = JSON.stringify(tmp.request.esbDataStruct);
}
@ -334,14 +336,14 @@
if (tmp.tags instanceof Array) {
tmp.tags = JSON.stringify(tmp.tags);
}
this.$fileUpload(url, null, bodyFiles, tmp, (response) => {
this.result = this.$fileUpload(url, null, bodyFiles, tmp, (response) => {
let data = response.data;
row.id = data.id;
row.createTime = data.createTime;
row.updateTime = data.updateTime;
if (!row.message) {
this.$success(this.$t('commons.save_success'));
if (!hideAlert) {
this.$success(this.$t('commons.save_success'));
this.$emit('refresh');
}
}

View File

@ -190,6 +190,7 @@
apiCaseClose() {
this.apiCaseList = [];
this.visible = false;
this.$emit('refresh');
},
refreshModule() {
this.$emit('refreshModule');
@ -324,6 +325,7 @@
request.hashTree.push(request.backScript);
}
let uuid = getUUID();
request.id = uuid;
let obj = {apiDefinitionId: this.api.id, name: '', priority: 'P0', active: true, tags: [], uuid: uuid};
obj.request = request;
this.apiCaseList.unshift(obj);

View File

@ -85,14 +85,16 @@
},
created() {
if (this.testCase) {
//
let url = "/api/definition/report/getReport/" + this.testCase.id;
this.$get(url, response => {
if (response.data) {
let data = JSON.parse(response.data.content);
this.responseData = data;
}
});
if (this.testCase.id) {
//
let url = "/api/definition/report/getReport/" + this.testCase.id;
this.$get(url, response => {
if (response.data) {
let data = JSON.parse(response.data.content);
this.responseData = data;
}
});
}
this.request = this.testCase.request;
if (this.request) {
this.debugForm.method = this.request.method;

View File

@ -112,14 +112,16 @@
},
created() {
if (this.testCase) {
//
let url = "/api/definition/report/getReport/" + this.testCase.id;
this.$get(url, response => {
if (response.data) {
let data = JSON.parse(response.data.content);
this.responseData = data;
}
});
if (this.testCase.id) {
//
let url = "/api/definition/report/getReport/" + this.testCase.id;
this.$get(url, response => {
if (response.data) {
let data = JSON.parse(response.data.content);
this.responseData = data;
}
});
}
this.request = this.testCase.request;
if (this.request) {
this.debugForm.method = this.request.method;

View File

@ -88,14 +88,16 @@
},
created() {
if (this.testCase) {
//
let url = "/api/definition/report/getReport/" + this.testCase.id;
this.$get(url, response => {
if (response.data) {
let data = JSON.parse(response.data.content);
this.responseData = data;
}
});
if (this.testCase.id) {
//
let url = "/api/definition/report/getReport/" + this.testCase.id;
this.$get(url, response => {
if (response.data) {
let data = JSON.parse(response.data.content);
this.responseData = data;
}
});
}
this.request = this.testCase.request;
if (this.request) {
this.debugForm.method = this.request.method;

View File

@ -94,14 +94,16 @@
},
created() {
if (this.testCase) {
//
let url = "/api/definition/report/getReport/" + this.testCase.id;
this.$get(url, response => {
if (response.data) {
let data = JSON.parse(response.data.content);
this.responseData = data;
}
});
if (this.testCase.id) {
//
let url = "/api/definition/report/getReport/" + this.testCase.id;
this.$get(url, response => {
if (response.data) {
let data = JSON.parse(response.data.content);
this.responseData = data;
}
});
}
this.request = this.testCase.request;
if (this.request) {
this.debugForm.method = this.request.method;

View File

@ -224,13 +224,15 @@ export default {
this.getEnvironments();
},
getResult() {
let url = "/api/definition/report/getReport/" + this.api.id;
this.$get(url, response => {
if (response.data) {
let data = JSON.parse(response.data.content);
this.responseData = data;
}
});
if (this.api.id) {
let url = "/api/definition/report/getReport/" + this.api.id;
this.$get(url, response => {
if (response.data) {
let data = JSON.parse(response.data.content);
this.responseData = data;
}
});
}
}
},
created() {

View File

@ -174,10 +174,7 @@
if (param.files) {
param.files.forEach(item => {
if (item.file) {
let fileId = getUUID().substring(0, 8);
item.name = item.file.name;
item.id = fileId;
this.api.bodyUploadIds.push(fileId);
bodyUploadFiles.push(item.file);
}
});
@ -230,13 +227,15 @@
this.$emit('refresh');
},
getResult() {
let url = "/api/definition/report/getReport/" + this.api.id;
this.$get(url, response => {
if (response.data) {
let data = JSON.parse(response.data.content);
this.responseData = data;
}
});
if (this.api.id) {
let url = "/api/definition/report/getReport/" + this.api.id;
this.$get(url, response => {
if (response.data) {
let data = JSON.parse(response.data.content);
this.responseData = data;
}
});
}
}
},
created() {

View File

@ -226,13 +226,15 @@ export default {
this.getEnvironments();
},
getResult() {
let url = "/api/definition/report/getReport/" + this.api.id;
this.$get(url, response => {
if (response.data) {
let data = JSON.parse(response.data.content);
this.responseData = data;
}
});
if (this.api.id) {
let url = "/api/definition/report/getReport/" + this.api.id;
this.$get(url, response => {
if (response.data) {
let data = JSON.parse(response.data.content);
this.responseData = data;
}
});
}
}
},
created() {

View File

@ -13,12 +13,147 @@ export function registerRequestHeaders() {
});
}
export function getUploadConfig(url, formData) {
return {
method: 'POST',
url: url,
data: formData,
headers: {
'Content-Type': undefined
}
};
}
// 登入请求不重定向
let unRedirectUrls = new Set(['signin', 'ldap/signin', '/signin', '/ldap/signin']);
export function login() {
MessageBox.alert(i18n.t('commons.tips'), i18n.t('commons.prompt'), {
callback: () => {
axios.get("/signout");
localStorage.setItem('Admin-Token', "{}");
window.location.href = "/login"
}
});
}
function then(success, response, result) {
if (!response.data) {
success(response);
} else if (response.data.success) {
success(response.data);
} else {
window.console.warn(response.data);
if (response.data.message) {
Message.warning(response.data.message);
}
}
result.loading = false;
}
function exception(error, result, url) {
if (error.response && error.response.status === 401 && !unRedirectUrls.has(url)) {
login();
return;
}
if (error.response && error.response.status === 403 && !unRedirectUrls.has(url)) {
window.location.href = "/";
return;
}
result.loading = false;
window.console.error(error.response || error.message);
if (error.response && error.response.data) {
if (error.response.headers["authentication-status"] !== "invalid") {
Message.error({message: error.response.data.message || error.response.data, showClose: true});
}
} else {
Message.error({message: error.message, showClose: true});
}
}
export function get(url, success) {
let result = {loading: true};
if (!success) {
return axios.get(url);
} else {
axios.get(url).then(response => {
then(success, response, result);
}).catch(error => {
exception(error, result, url);
});
return result;
}
}
export function post (url, data, success, failure) {
let result = {loading: true};
if (!success) {
return axios.post(url, data);
} else {
axios.post(url, data).then(response => {
then(success, response, result);
}).catch(error => {
exception(error, result, url);
if (failure) {
then(failure, error, result);
}
});
return result;
}
}
export function request(axiosRequestConfig, success, failure) {
let result = {loading: true};
if (!success) {
return axios.request(axiosRequestConfig);
} else {
axios.request(axiosRequestConfig).then(response => {
then(success, response, result);
}).catch(error => {
exception(error, result);
if (failure) {
then(failure, error, result);
}
});
return result;
}
}
export function fileDownload(url) {
axios.get(url, {responseType: 'blob'})
.then(response => {
let fileName = window.decodeURI(response.headers['content-disposition'].split('=')[1]);
let link = document.createElement("a");
link.href = window.URL.createObjectURL(new Blob([response.data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"}));
link.download = fileName;
link.click();
});
}
export function fileUpload(url, file, files, param, success, failure) {
let formData = new FormData();
if (file) {
formData.append("file", file);
}
if (files) {
files.forEach(f => {
formData.append("files", f);
})
}
formData.append('request', new Blob([JSON.stringify(param)], {type: "application/json"}));
let axiosRequestConfig = getUploadConfig(url, formData);
return request(axiosRequestConfig, success, failure);
}
export function all(array, callback) {
if (array.length < 1) return;
axios.all(array).then(axios.spread(callback));
}
export default {
install(Vue) {
// 登入请求不重定向
let unRedirectUrls = new Set(['signin', 'ldap/signin', '/signin', '/ldap/signin']);
if (!axios) {
window.console.error('You have to install axios');
return
@ -29,15 +164,7 @@ export default {
return
}
let login = function () {
MessageBox.alert(i18n.t('commons.tips'), i18n.t('commons.prompt'), {
callback: () => {
axios.get("/signout");
localStorage.setItem('Admin-Token', "{}");
window.location.href = "/login"
}
});
};
let login = login;
axios.defaults.withCredentials = true;
@ -50,125 +177,16 @@ export default {
return Promise.reject(error);
});
function then(success, response, result) {
if (!response.data) {
success(response);
} else if (response.data.success) {
success(response.data);
} else {
window.console.warn(response.data);
if (response.data.message) {
Message.warning(response.data.message);
}
}
result.loading = false;
}
Vue.prototype.$get = get;
function exception(error, result, url) {
if (error.response && error.response.status === 401 && !unRedirectUrls.has(url)) {
login();
return;
}
if (error.response && error.response.status === 403 && !unRedirectUrls.has(url)) {
window.location.href = "/";
return;
}
result.loading = false;
window.console.error(error.response || error.message);
if (error.response && error.response.data) {
if (error.response.headers["authentication-status"] !== "invalid") {
Message.error({message: error.response.data.message || error.response.data, showClose: true});
}
} else {
Message.error({message: error.message, showClose: true});
}
}
Vue.prototype.$post = post;
Vue.prototype.$get = function (url, success) {
let result = {loading: true};
if (!success) {
return axios.get(url);
} else {
axios.get(url).then(response => {
then(success, response, result);
}).catch(error => {
exception(error, result, url);
});
return result;
}
};
Vue.prototype.$request = request;
Vue.prototype.$post = function (url, data, success, failure) {
let result = {loading: true};
if (!success) {
return axios.post(url, data);
} else {
axios.post(url, data).then(response => {
then(success, response, result);
}).catch(error => {
exception(error, result, url);
if (failure) {
then(failure, error, result);
}
});
return result;
}
};
Vue.prototype.$all = all;
Vue.prototype.$request = function (axiosRequestConfig, success, failure) {
let result = {loading: true};
if (!success) {
return axios.request(axiosRequestConfig);
} else {
axios.request(axiosRequestConfig).then(response => {
then(success, response, result);
}).catch(error => {
exception(error, result);
if (failure) {
then(failure, error, result);
}
});
return result;
}
};
Vue.prototype.$all = function (array, callback) {
if (array.length < 1) return;
axios.all(array).then(axios.spread(callback));
};
Vue.prototype.$fileDownload = function (url) {
axios.get(url, {responseType: 'blob'})
.then(response => {
let fileName = window.decodeURI(response.headers['content-disposition'].split('=')[1]);
let link = document.createElement("a");
link.href = window.URL.createObjectURL(new Blob([response.data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"}));
link.download = fileName;
link.click();
});
};
Vue.prototype.$fileUpload = function (url, file, files, param, success, failure) {
let formData = new FormData();
if (file) {
formData.append("file", file);
}
if (files) {
files.forEach(f => {
formData.append("files", f);
})
}
formData.append('request', new Blob([JSON.stringify(param)], {type: "application/json"}));
let axiosRequestConfig = {
method: 'POST',
url: url,
data: formData,
headers: {
'Content-Type': undefined
}
};
return Vue.prototype.$request(axiosRequestConfig, success, failure);
}
Vue.prototype.$fileDownload = fileDownload;
Vue.prototype.$fileUpload = fileUpload;
}
}

View File

@ -287,9 +287,11 @@ export function getBodyUploadFiles(obj, runData) {
if (runData) {
if (runData instanceof Array) {
runData.forEach(request => {
obj.requestId = request.id;
_getBodyUploadFiles(request, bodyUploadFiles, obj);
});
} else {
obj.requestId = runData.id;
_getBodyUploadFiles(runData, bodyUploadFiles, obj);
}
}
@ -299,8 +301,10 @@ export function getBodyUploadFiles(obj, runData) {
export function _getBodyUploadFiles(request, bodyUploadFiles, obj) {
let body = null;
if (request.hashTree && request.hashTree.length > 0 && request.hashTree[0] && request.hashTree[0].body) {
obj.requestId = request.hashTree[0].id;
body = request.hashTree[0].body;
} else if (request.body) {
obj.requestId = request.id;
body = request.body;
}
if (body) {
@ -309,12 +313,7 @@ export function _getBodyUploadFiles(request, bodyUploadFiles, obj) {
if (param.files) {
param.files.forEach(item => {
if (item.file) {
if (!item.id) {
let fileId = getUUID().substring(0, 12);
item.name = item.file.name;
item.id = fileId;
}
obj.bodyUploadIds.push(item.id);
item.name = item.file.name;
bodyUploadFiles.push(item.file);
}
});
@ -326,12 +325,7 @@ export function _getBodyUploadFiles(request, bodyUploadFiles, obj) {
if (param.files) {
param.files.forEach(item => {
if (item.file) {
if (!item.id) {
let fileId = getUUID().substring(0, 12);
item.name = item.file.name;
item.id = fileId;
}
obj.bodyUploadIds.push(item.id);
item.name = item.file.name;
bodyUploadFiles.push(item.file);
}
});