问:
if (this.addDataForm.locationName == 'topLeftBtnList') {
ajaxMethods(
"post",
xxxx + "getByConditionPage",
{
title: this.searchFormData.title,
pageType: this.searchFormData.pageType,
locationName: this.addDataForm.locationName,
status: this.searchFormData.status,
page: this.currentPage,
rows: this.pageSize,
sortby: 'id',
order: 'desc',
},
(res) => {
console.log(res.data.pageSize, "res.data.pageSize/?");
this.saveData = res.data.pageSize;
if (this.saveData < 1) {
ajaxMethods(
"post",
xxxx + "/inster",
{
...this.addDataForm,
},
(res) => {
if (res.code == 0) {
this.$message({
message: "添加成功",
type: "success",
type: "success",
message: res.msg,
type: "success",
message: res.msg,
});
this.resetForm("addDataForm");
this.batchVisible = false;
this.searchTableData();
} else {
this.$message.error("重复提交");
}
console.log(res);
}
);
} else {
this.$message.error('topLeftBtnList, 已经存在一个')
}
}
);
}else if (this.addDataForm.locationName == 'topRightBtnList') {
ajaxMethods(
"post",
xxxx + "/getByConditionPage",
{
title: this.searchFormData.title,
pageType: this.searchFormData.pageType,
locationName: this.addDataForm.locationName,
status: this.searchFormData.status,
page: this.currentPage,
rows: this.pageSize,
sortby: 'id',
order: 'desc',
},
(res) => {
console.log(res.data.pageSize, "res.data.pageSize/?");
this.saveData = res.data.pageSize;
if (this.saveData < 2) {
ajaxMethods(
"post",
xxxx + "/inster",
{
...this.addDataForm,
},
(res) => {
if (res.code == 0) {
this.$message({
message: "添加成功",
type: "success",
type: "success",
message: res.msg,
type: "success",
message: res.msg,
});
this.resetForm("addDataForm");
this.batchVisible = false;
this.searchTableData();
} else {
this.$message.error("重复提交");
}
console.log(res);
}
);
} else {
this.$message.error('topLeftBtnList, 已经存在一个')
}
}
);
}这里重复代码
同时,
我只是放上来了两个情况,后面还会有
this.addDataForm.locationName == 'settingsLeftBtn'
this.addDataForm.locationName == 'settingsRightBtn'
this.addDataForm.locationName == 'settings4Items'
this.addDataForm.locationName == 'settings3NItems'
this.addDataForm.locationName == 'settingsLists0'
this.addDataForm.locationName == 'settingsLists1'
这几种情况?
回答:
明白了,你的情况涵盖了多个可能的 `this.addDataForm.locationName` 值,
为了避免代码重复,可以进一步优化和抽象化。
你可以考虑使用一个对象来存储每种 `locationName` 对应的最大允许数量,
然后根据这个对象来动态判断。
这样可以使代码更加清晰和易于维护。以下是一个示例:
// 定义一个对象来存储每种 locationName 对应的最大允许数量
const maxAllowedMap = {
'topLeftBtnList': 1,
'topRightBtnList': 2,
'settingsLeftBtn': 3,
'settingsRightBtn': 3,
'settings4Items': 4,
'settings3NItems': 3,
'settingsLists0': 0,
'settingsLists1': 1,
// 可根据实际情况继续添加其他 locationName 的定义
};
// 判断 this.addDataForm.locationName 是否在 maxAllowedMap 中定义
if (this.addDataForm.locationName in maxAllowedMap) {
ajaxMethods(
"post",
xxxx + "/getByConditionPage",
{
title: this.searchFormData.title,
pageType: this.searchFormData.pageType,
locationName: this.addDataForm.locationName,
status: this.searchFormData.status,
page: this.currentPage,
rows: this.pageSize,
sortby: 'id',
order: 'desc',
},
(res) => {
console.log(res.data.pageSize, "res.data.pageSize/?");
this.saveData = res.data.pageSize;
let maxAllowed = maxAllowedMap[this.addDataForm.locationName];
if (this.saveData < maxAllowed) {
ajaxMethods(
"post",
xxxx + "/inster",
{
...this.addDataForm,
},
(res) => {
if (res.code == 0) {
this.$message({
message: "添加成功",
type: "success",
});
this.resetForm("addDataForm");
this.batchVisible = false;
this.searchTableData();
} else {
this.$message.error("重复提交");
}
console.log(res);
}
);
} else {
this.$message.error(`${this.addDataForm.locationName}, 已经存在一个`);
}
}
);
} else {
// 处理未知的 locationName 或者添加适当的错误处理
this.$message.error('未知的 locationName');
}
在这段代码中,我们引入了 `maxAllowedMap` 对象来存储不同 `locationName` 对应的最大允许数量。然后通过 `this.addDataForm.locationName` 来动态获取对应的最大值,并进行判断和处理。这种方式可以有效地减少代码的重复性,同时使得代码更易于扩展和维护。
问:
上述代码在新增的情况下是没有问题的, 但是编辑的情况下, 我们需要新增判断: 当status不变或者status从有效:1 改为-> 无效2的时候, 直接请求接口不需要走: if (this.saveData < maxAllowed) {这里的逻辑, 直接请求接口就可以,
但是当 编辑dialog中后端传回的这一行数据status的状态是无效:2, 且需要改为 有效:1 然后保存提交的时候, 才需要进行if (this.saveData < maxAllowed) {的判断,
所以我们在data中增加了两个字段:
// 存储一下 点击编辑按钮后打开dialog弹框 存储这时候的status状态
saveEditStatus: 0,
// 存储一下 编辑弹框的确认按钮点击后 知识后的status状态
saveNewEditStatus: 0,
头一个字段存储点击编辑按钮, dialog弹框显示后, 后端传回的当前status的值,
另一个字段存储修改编辑表单后, dialog弹框最新的status的值,
下面是修改后的代码:
// 新增
if (this.addDataForm.status == 1) {
// 定义一个对象来存储每种 locationName 对应的最大允许数量
const maxAllowedMap = {
'topLeftBtnList': 1,
'topRightBtnList': 2,
'settingsLeftBtn': 1,
'settingsRightBtn': 2,
'settings4Items': 4,
'settings3NItems': 9,
'settingsLists0': 10,
'settingsLists1': 10,
// 可根据实际情况继续添加其他 locationName 的定义
};
// 判断 this.addDataForm.locationName 是否在 maxAllowedMap 中定义
if (this.addDataForm.locationName in maxAllowedMap) {
ajaxMethods(
"post",
xxxx + "xxxx/getByConditionPage",
{
title: this.searchFormData.title,
pageType: this.searchFormData.pageType,
locationName: this.addDataForm.locationName,
// status: this.addDataForm.status,
status: 1, // 默认值查询 有效 的totle数据总数
page: this.currentPage,
rows: this.pageSize,
sortby: 'id',
order: 'desc',
},
(res) => {
console.log(res.data.total, "res.data.total/?");
this.saveData = res.data.total;
let maxAllowed = maxAllowedMap[this.addDataForm.locationName];
console.log(this.addDataForm.status,'status???')
this.saveNewEditStatus = this.addDataForm.status;
console.log(this.saveEditStatus,'this.saveEditStatus')
console.log(this.saveNewEditStatus,'this.saveNewEditStatus')
if (this.saveEditStatus == 2 && this.saveNewEditStatus == 1) { // 判断: 当是否有效 status从: 无效改为->有效 的时候才进行判断里面的逻辑, 否则直接请求接口就行
if (this.saveData < maxAllowed) {
ajaxMethods(
"post",
xxxx + "/xxxx/update",
{
...this.addDataForm,
id: this.id,
},
(res) => {
if (res.code == 0) {
this.$message({
message: "编辑成功",
type: "success",
});
this.resetForm("addDataForm");
this.batchVisible = false;
this.searchTableData();
} else {
this.$message.error("重复提交");
}
}
);
} else {
this.$message.error(`当前位置最多可配置${maxAllowed}条`);
}
} else { // 当status没有改变 或者 从有效改为无效 则不去判断 total 直接调用接口进行update
ajaxMethods(
"post",
xxxx + "/xxxx/update",
{
...this.addDataForm,
id: this.id,
},
(res) => {
if (res.code == 0) {
this.$message({
message: "编辑成功",
type: "success",
});
this.resetForm("addDataForm");
this.batchVisible = false;
this.searchTableData();
} else {
this.$message.error("重复提交");
}
}
);
}
}
);
}
} else {
ajaxMethods(
"post",
xxxx + "/xxxx/update",
{
...this.addDataForm,
id: this.id,
},
(res) => {
if (res.code == 0) {
this.$message({
message: "编辑成功",
type: "success",
});
this.resetForm("addDataForm");
this.batchVisible = false;
this.searchTableData();
} else {
this.$message.error("重复提交");
}
}
);
}
问:
感觉上述代码重复度过高, 怎么修改?
回答:
// 新增
if (this.addDataForm.status == 1) {
const maxAllowedMap = {
'topLeftBtnList': 1,
'topRightBtnList': 2,
'settingsLeftBtn': 1,
'settingsRightBtn': 2,
'settings4Items': 4,
'settings3NItems': 9,
'settingsLists0': 10,
'settingsLists1': 10,
// 可根据实际情况继续添加其他 locationName 的定义
};
if (this.addDataForm.locationName in maxAllowedMap) {
ajaxMethods(
"post",
xxxx + "xxxx/getByConditionPage",
{
title: this.searchFormData.title,
pageType: this.searchFormData.pageType,
locationName: this.addDataForm.locationName,
status: 1,
page: this.currentPage,
rows: this.pageSize,
sortby: 'id',
order: 'desc',
},
(res) => {
this.saveData = res.data.total;
let maxAllowed = maxAllowedMap[this.addDataForm.locationName];
if (this.saveEditStatus == 2 && this.addDataForm.status == 1) {
if (this.saveData < maxAllowed) {
updateData();
} else {
this.$message.error(`当前位置最多可配置${maxAllowed}条`);
}
} else {
updateData();
}
}
);
}
} else {
updateData();
}
function updateData() {
ajaxMethods(
"post",
xxxx + "/xxxx/update",
{
...this.addDataForm,
id: this.id,
},
(res) => {
if (res.code == 0) {
this.$message({
message: "编辑成功",
type: "success",
});
this.resetForm("addDataForm");
this.batchVisible = false;
this.searchTableData();
} else {
this.$message.error("重复提交");
}
}
);
}