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); 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") @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) { 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); checkParams(apiTestEnvironment);

View File

@ -378,6 +378,41 @@ public class BaseEnvironmentService extends NodeTreeService<ApiModuleDTO> {
return request.getId(); 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) { private TestEnvironmentDTO updateConfig(TestEnvironmentDTO request, boolean isMock) {
if (StringUtils.isNotEmpty(request.getConfig())) { if (StringUtils.isNotEmpty(request.getConfig())) {
try { try {

View File

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

View File

@ -43,7 +43,9 @@
</template> </template>
<script> <script>
import {addEnvironment} from "../../../api/environment"; import {importEnvironment} from "../../../api/environment";
import {getCurrentProjectID} from "metersphere-frontend/src/utils/token";
export default { export default {
name: "EnvironmentImport", name: "EnvironmentImport",
props: { props: {
@ -92,33 +94,27 @@ export default {
continue; continue;
} }
let reader = new FileReader(); let reader = new FileReader();
reader.readAsText(file.raw) reader.readAsText(file.raw)
reader.onload = (e) => { reader.onload = (e) => {
let fileString = e.target.result;
try { try {
JSON.parse(fileString).map(env => { let arr = [];
//projectIdnullprojectId JSON.parse(e.target.result).map(env => {
if (this.toImportProjectId) { env.projectId = getCurrentProjectID();
env.projectId = this.toImportProjectId; arr.push(env);
} 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'));
});
}) })
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) { } catch (exception) {
this.$warning(this.$t('api_test.api_import.ms_env_import_file_limit')); 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.dialogVisible = true;
this.currentEnvironment = new Environment(); this.currentEnvironment = new Environment();
this.currentEnvironment.projectId = this.currentProjectId; this.currentEnvironment.projectId = this.currentProjectId;
this.currentEnvironment.currentProjectId = this.currentProjectId;
this.ifCreate = true; this.ifCreate = true;
}, },
search() { search() {
@ -292,8 +293,7 @@ export default {
}, },
editEnv(environment) { editEnv(environment) {
this.dialogTitle = this.$t('api_test.environment.config_environment'); this.dialogTitle = this.$t('api_test.environment.config_environment');
this.currentProjectId = environment.projectId; environment.currentProjectId = getCurrentProjectID();
environment.currentProjectId = environment.projectId;
const temEnv = {}; const temEnv = {};
Object.assign(temEnv, environment); Object.assign(temEnv, environment);
parseEnvironment(temEnv); //parseEnvironment parseEnvironment(temEnv); //parseEnvironment
@ -303,9 +303,8 @@ export default {
}, },
copyEnv(environment) { copyEnv(environment) {
this.currentProjectId = environment.projectId; //
this.dialogTitle = this.$t('api_test.environment.copy_environment'); this.dialogTitle = this.$t('api_test.environment.copy_environment');
environment.currentProjectId = environment.projectId; environment.currentProjectId = getCurrentProjectID();
const temEnv = {}; const temEnv = {};
Object.assign(temEnv, environment); Object.assign(temEnv, environment);
parseEnvironment(temEnv); //parseEnvironment 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" import mf from "metersphere-frontend/src/i18n/lang/en-US"
const message = { const message = {
pj: {
environment_import_repeat_tip: "(Environment configuration with the same name filtered {0})"
},
file_manage: { file_manage: {
my_file: 'My File', my_file: 'My File',
update_user: 'Update User', 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" import mf from "metersphere-frontend/src/i18n/lang/zh-CN"
const message = { const message = {
pj: {
environment_import_repeat_tip: "(已过滤同名称的环境配置 {0})"
},
file_manage: { 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-TW"; // 加载fit2cloud的内容
import mf from "metersphere-frontend/src/i18n/lang/zh-TW" import mf from "metersphere-frontend/src/i18n/lang/zh-TW"
const message = { const message = {
pj: {
environment_import_repeat_tip: "(已過濾同名稱的環境配置 {0})"
},
file_manage: { file_manage: {
my_file: '我的文件', my_file: '我的文件',
update_user: '更新人', update_user: '更新人',

View File

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

View File

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

View File

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

View File

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

View File

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