fix(项目设置): 环境导入问题

This commit is contained in:
shiziyuan9527 2022-10-10 17:49:02 +08:00 committed by lyh
parent 0925f0e2de
commit 80f88ebd32
13 changed files with 104 additions and 37 deletions

View File

@ -70,6 +70,12 @@ public class TestEnvironmentController {
return baseEnvironmentService.add(apiTestEnvironmentWithBLOBs, sslFiles, variableFile);
}
@PostMapping("/import")
public String create(@RequestBody List<TestEnvironmentDTO> environments) {
environments.forEach(this::checkParams);
return baseEnvironmentService.importEnvironment(environments);
}
@PostMapping(value = "/update")
public void update(@RequestPart("request") TestEnvironmentDTO apiTestEnvironment, @RequestPart(value = "files", required = false) List<MultipartFile> sslFiles, @RequestPart(value = "variablesFiles", required = false) List<MultipartFile> variableFile) {
checkParams(apiTestEnvironment);

View File

@ -378,6 +378,41 @@ public class BaseEnvironmentService extends NodeTreeService<ApiModuleDTO> {
return request.getId();
}
public String importEnvironment(List<TestEnvironmentDTO> environments) {
StringBuilder existNames = new StringBuilder();
for (TestEnvironmentDTO request : environments) {
request.setId(UUID.randomUUID().toString());
request.setCreateUser(SessionUtils.getUserId());
if (request.getName() != null) {
if (StringUtils.isBlank(request.getProjectId())) {
MSException.throwException(Translator.get("项目ID不能为空"));
}
ApiTestEnvironmentExample example = new ApiTestEnvironmentExample();
ApiTestEnvironmentExample.Criteria criteria = example.createCriteria();
criteria.andNameEqualTo(request.getName()).andProjectIdEqualTo(request.getProjectId());
if (StringUtils.isNotBlank(request.getId())) {
criteria.andIdNotEqualTo(request.getId());
}
if (apiTestEnvironmentMapper.selectByExample(example).size() > 0) {
existNames.append(" ").append(request.getName());
continue;
}
}
//检查Config判断isMock参数是否给True
this.updateConfig(request, false);
request.setCreateTime(System.currentTimeMillis());
request.setUpdateTime(System.currentTimeMillis());
apiTestEnvironmentMapper.insert(request);
// 存储附件关系
saveEnvironment(request.getId(), request.getConfig(), FileAssociationType.ENVIRONMENT.name());
}
if (existNames.length() > 0) {
return existNames.toString();
} else {
return "OK";
}
}
private TestEnvironmentDTO updateConfig(TestEnvironmentDTO request, boolean isMock) {
if (StringUtils.isNotEmpty(request.getConfig())) {
try {

View File

@ -26,6 +26,9 @@ export function addEnvironment(param) {
return request(config);
}
export function importEnvironment(params) {
return post("/environment/import", params);
}
export function editEnv(formData, param) {
let url = '/api/environment/add';

View File

@ -43,7 +43,9 @@
</template>
<script>
import {addEnvironment} from "../../../api/environment";
import {importEnvironment} from "../../../api/environment";
import {getCurrentProjectID} from "metersphere-frontend/src/utils/token";
export default {
name: "EnvironmentImport",
props: {
@ -92,33 +94,27 @@ export default {
continue;
}
let reader = new FileReader();
reader.readAsText(file.raw)
reader.onload = (e) => {
let fileString = e.target.result;
try {
JSON.parse(fileString).map(env => {
//projectIdnullprojectId
if (this.toImportProjectId) {
env.projectId = this.toImportProjectId;
} else {
env.projectId = this.currentProjectId === '' ? null : this.currentProjectId;
}
if (!env.projectId) {
this.$warning(this.$t('api_test.environment.project_warning'));
return;
}
addEnvironment(env).then(() => {
this.dialogVisible = false;
this.$emit('refresh');
this.$success(this.$t('commons.save_success'));
});
let arr = [];
JSON.parse(e.target.result).map(env => {
env.projectId = getCurrentProjectID();
arr.push(env);
})
importEnvironment(arr).then(res => {
if (res.data === 'OK') {
this.$success(this.$t('commons.save_success'));
} else {
this.$success(this.$t('commons.save_success') + this.$t('pj.environment_import_repeat_tip', [res.data]));
}
this.dialogVisible = false;
this.$emit('refresh');
});
} catch (exception) {
this.$warning(this.$t('api_test.api_import.ms_env_import_file_limit'));
}
}
}
}
},

View File

@ -285,6 +285,7 @@ export default {
this.dialogVisible = true;
this.currentEnvironment = new Environment();
this.currentEnvironment.projectId = this.currentProjectId;
this.currentEnvironment.currentProjectId = this.currentProjectId;
this.ifCreate = true;
},
search() {
@ -292,8 +293,7 @@ export default {
},
editEnv(environment) {
this.dialogTitle = this.$t('api_test.environment.config_environment');
this.currentProjectId = environment.projectId;
environment.currentProjectId = environment.projectId;
environment.currentProjectId = getCurrentProjectID();
const temEnv = {};
Object.assign(temEnv, environment);
parseEnvironment(temEnv); //parseEnvironment
@ -303,9 +303,8 @@ export default {
},
copyEnv(environment) {
this.currentProjectId = environment.projectId; //
this.dialogTitle = this.$t('api_test.environment.copy_environment');
environment.currentProjectId = environment.projectId;
environment.currentProjectId = getCurrentProjectID();
const temEnv = {};
Object.assign(temEnv, environment);
parseEnvironment(temEnv); //parseEnvironment

View File

@ -3,6 +3,9 @@ import fu from "fit2cloud-ui/src/locale/lang/en_US"; // 加载fit2cloud的内容
import mf from "metersphere-frontend/src/i18n/lang/en-US"
const message = {
pj: {
environment_import_repeat_tip: "(Environment configuration with the same name filtered {0})"
},
file_manage: {
my_file: 'My File',
update_user: 'Update User',

View File

@ -3,6 +3,9 @@ import fu from "fit2cloud-ui/src/locale/lang/zh-CN"; // 加载fit2cloud的内容
import mf from "metersphere-frontend/src/i18n/lang/zh-CN"
const message = {
pj: {
environment_import_repeat_tip: "(已过滤同名称的环境配置 {0})"
},
file_manage: {
my_file: '我的文件',
update_user: '更新人',

View File

@ -3,6 +3,9 @@ import fu from "fit2cloud-ui/src/locale/lang/zh-TW"; // 加载fit2cloud的内容
import mf from "metersphere-frontend/src/i18n/lang/zh-TW"
const message = {
pj: {
environment_import_repeat_tip: "(已過濾同名稱的環境配置 {0})"
},
file_manage: {
my_file: '我的文件',
update_user: '更新人',

View File

@ -25,6 +25,10 @@ export function addEnvironment(param) {
}; return request(config);
}
export function importEnvironment(params) {
return post("/environment/import", params);
}
export function getEnvironmentGroupPages(goPage, pageSize, param) {
return post(`/environment/group/list/${goPage}/${pageSize}`, param);
}

View File

@ -43,7 +43,7 @@
<script>
import {addEnvironment} from "../../../api/environment";
import {importEnvironment} from "../../../api/environment";
export default {
name: "EnvironmentImport",
@ -92,19 +92,25 @@ export default {
reader.onload = (e) => {
let fileString = e.target.result;
try {
JSON.parse(fileString).map(env => {
//projectIdnullprojectId
env.projectId = this.currentProjectId === '' ? null : this.currentProjectId;
if (!env.projectId) {
this.$warning(this.$t('api_test.environment.project_warning'));
return;
}
addEnvironment(env).then(() => {
this.dialogVisible = false;
this.$emit('refresh');
this.$success(this.$t('commons.save_success'));
});
let tempProjectId = this.currentProjectId === '' ? null : this.currentProjectId;
if (!tempProjectId) {
this.$warning(this.$t('api_test.environment.project_warning'));
return;
}
let arr = [];
JSON.parse(e.target.result).map(env => {
env.projectId = tempProjectId;
arr.push(env);
})
importEnvironment(arr).then(res => {
if (res.data === 'OK') {
this.$success(this.$t('commons.save_success'));
} else {
this.$success(this.$t('commons.save_success') + this.$t('system.environment_import_repeat_tip', [res.data]));
}
this.dialogVisible = false;
this.$emit('refresh');
});
} catch (exception) {
this.$warning(this.$t('api_test.api_import.ms_env_import_file_limit'));
}

View File

@ -6,6 +6,9 @@ const message = {
user: {
search_get_more_tip: 'Search for other options'
},
system: {
environment_import_repeat_tip: "(Environment configuration with the same name filtered {0})"
},
display: {
title: 'Theme',
logo: 'System LOGO',

View File

@ -6,6 +6,9 @@ const message = {
system_user: {
search_get_more_tip: '搜索获取其他选项'
},
system: {
environment_import_repeat_tip: "(已过滤同名称的环境配置 {0})"
},
display: {
title: '显示设置',
logo: '系统 LOGO',

View File

@ -6,6 +6,9 @@ const message = {
user: {
search_get_more_tip: '搜索獲取其他選項'
},
system: {
environment_import_repeat_tip: "(已過濾同名稱的環境配置 {0})"
},
display: {
title: '顯示設置',
logo: '系統 LOGO',