Skip to content

Commit 279e8ce

Browse files
fix: 环境变量批量编辑、单条操作校验优化 (#2336)
1 parent e234ceb commit 279e8ce

2 files changed

Lines changed: 52 additions & 64 deletions

File tree

webfe/package_vue/src/store/modules/var.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export default {
104104
/**
105105
* 保存环境变量数据
106106
*/
107-
saveEnvItem({}, { appCode, moduleId, data }) {
107+
batchConfigVars({}, { appCode, moduleId, data }) {
108108
const url = `${BACKEND_URL}/api/bkapps/applications/${appCode}/modules/${moduleId}/config_vars/batch/`;
109109
return http.post(url, data);
110110
},

webfe/package_vue/src/views/dev-center/app/engine/cloud-deployment/deploy-env.vue

Lines changed: 51 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,13 @@
393393
>
394394
<bk-button
395395
:theme="'primary'"
396-
@click="saveEnvData"
396+
@click="batchEditSaveValidation"
397397
>
398398
{{ $t('保存') }}
399399
</bk-button>
400400
<bk-button
401401
class="ml8"
402-
@click="$emit('cancel')"
402+
@click="handleCancel"
403403
>
404404
{{ $t('取消') }}
405405
</bk-button>
@@ -616,12 +616,6 @@ export default {
616616
const flag = this.envVarList.filter(
617617
(item) => item.key === this.curItem.key && item.environment_name === this.curItem.environment_name
618618
);
619-
if (flag.length <= 1) {
620-
// 如果符合要求需要清除错误
621-
this.envVarList.forEach((e, i) => {
622-
this.$refs[`envRefKey${i}`] && this.$refs[`envRefKey${i}`].clearError();
623-
});
624-
}
625619
return flag.length <= 1;
626620
},
627621
message: () => this.$t(`该环境下名称为 ${this.curItem.key} 的变量已经存在,不能重复添加。`),
@@ -799,67 +793,66 @@ export default {
799793
},
800794
801795
// 处理input事件
802-
handleInputEvent(rowItem) {
796+
handleInputEvent(rowItem, index) {
803797
this.curItem = rowItem;
798+
// 批量编辑模式下为全量数据可以使用index
799+
if (this.isPageEdit) {
800+
this.markAsTouched(index);
801+
}
804802
},
805803
806-
async saveEnvData() {
807-
let validate = true;
808-
// 提交时需要检验,拿到需要检验的数据下标
809-
const flag = this.envVarList.reduce((p, v, i) => {
810-
if (v.key === this.curItem.key && v.environment_name === this.curItem.environment_name) {
811-
p.push({ ...v, i });
812-
}
813-
if (v.key === '' || v.environment_name === '') {
814-
p.push({ ...v, i });
804+
// 批量编辑模式下,给操作表单的item项添加标记,用来标记是否被操作过
805+
markAsTouched(index) {
806+
this.$set(this.envVarList[index], 'isTouched', true);
807+
},
808+
809+
// 清除所有被操作过的标记
810+
clearTouchedFlags() {
811+
this.envVarList.forEach((item) => {
812+
this.$delete(item, 'isTouched');
813+
});
814+
},
815+
816+
getOperatedValidationPromises() {
817+
return this.envVarList.reduce((promises, item, index) => {
818+
if (item.isTouched || item.key === '' || item.environment_name === '') {
819+
promises.push(
820+
this.$refs[`envRefKey${index}`]?.validate(),
821+
this.$refs[`envRefValue${index}`]?.validate(),
822+
item.description ? this.$refs[`envRefDescription${index}`]?.validate() : Promise.resolve()
823+
);
815824
}
816-
return p;
825+
return promises;
817826
}, []);
818-
// 仅一条数据也可删除
827+
},
828+
829+
// 批量编辑保存校验
830+
async batchEditSaveValidation() {
831+
// 没有数据直接保存,允许删除最后一条
819832
if (this.envVarList.length === 0) {
820-
this.save();
833+
this.batchEditSave();
821834
return;
822835
}
823-
if (flag.length) {
824-
// 有数据时
825-
for (let index = 0; index < flag.length; index++) {
826-
try {
827-
await this.$refs[`envRefKey${flag[index].i}`].validate();
828-
await this.$refs[`envRefValue${flag[index].i}`].validate();
829-
if (flag[index]?.description) {
830-
await this.$refs[`envRefDescription${flag[index].i}`].validate();
831-
}
832-
} catch (error) {
833-
validate = false;
834-
break;
835-
}
836-
}
837-
} else {
838-
// 新增第一条数据时
839-
try {
840-
await this.$refs.envRefKey0.validate();
841-
await this.$refs.envRefValue0.validate();
842-
if (this.envVarList[0]?.description) {
843-
await this.$refs?.envRefDescription0?.validate();
844-
}
845-
} catch (error) {
846-
validate = false;
847-
}
848-
}
849-
if (validate) {
850-
// 通过检验才可以保存
851-
this.save();
836+
try {
837+
const validationPromises = this.getOperatedValidationPromises();
838+
// 执行所有验证
839+
await Promise.all(validationPromises);
840+
this.clearTouchedFlags();
841+
this.batchEditSave();
842+
} catch (error) {
843+
// 捕获并处理验证错误
844+
console.error(error);
852845
}
853846
},
854847
855848
// 单条环境变量校验
856849
async singleValidate(i, type) {
857850
try {
858-
await this.$refs[`envRefKey${i}`].validate();
859-
await this.$refs[`envRefValue${i}`].validate();
851+
const validateRefs = [this.$refs[`envRefKey${i}`].validate(), this.$refs[`envRefValue${i}`].validate()];
860852
if (this.envVarList[i]?.description) {
861-
await this.$refs[`envRefDescription${i}`].validate();
853+
validateRefs.push(this.$refs[`envRefDescription${i}`].validate());
862854
}
855+
await Promise.all(validateRefs);
863856
const data = cloneDeep(this.envVarList[i]);
864857
// 单条新建编辑操作
865858
type === 'add' ? this.createdEnvVariable(data, i) : this.updateEnvVariable(data, i);
@@ -923,13 +916,13 @@ export default {
923916
}
924917
},
925918
926-
// 保存
927-
async save() {
919+
// 批量编辑保存
920+
async batchEditSave() {
928921
try {
929922
// 保存环境变量,无需传递 is_global
930923
const params = cloneDeep(this.envVarList).map((item) => this.cleanEnvVarData(item, true));
931924
932-
await this.$store.dispatch('envVar/saveEnvItem', {
925+
await this.$store.dispatch('envVar/batchConfigVars', {
933926
appCode: this.appCode,
934927
moduleId: this.curModuleId,
935928
data: params,
@@ -943,10 +936,9 @@ export default {
943936
// 批量更新不打乱当前顺序,重新复制当前新建id
944937
this.getEnvVarList(false);
945938
} catch (error) {
946-
const errorMsg = error.message;
947939
this.$paasMessage({
948940
theme: 'error',
949-
message: `${this.$t('添加环境变量失败')}${errorMsg}`,
941+
message: `${this.$t('添加环境变量失败')}${error.message}`,
950942
});
951943
}
952944
this.isBatchEdit = false;
@@ -1337,11 +1329,7 @@ export default {
13371329
handleCancel() {
13381330
this.envVarList = cloneDeep(this.envLocalVarList);
13391331
this.isBatchEdit = false;
1340-
},
1341-
1342-
sourceFilterMethod(value, row, column) {
1343-
const { property } = column;
1344-
return row[property] === value;
1332+
this.$emit('cancel');
13451333
},
13461334
13471335
handleRenderHander(h, { $index }) {

0 commit comments

Comments
 (0)