优化PUT请求传参机制,实现最小化传参以减少网络传输和提高性能,在请求拦截器中自动计算差异只传递修改过的字段,同时新增通用的表单差异比较工具函数,

This commit is contained in:
WUSIJIAN
2026-01-13 18:06:53 +08:00
parent cdbda577cc
commit f8af956f06
6 changed files with 289 additions and 24 deletions

View File

@@ -126,6 +126,7 @@ import { ElMessage } from 'element-plus';
import { Plus, Delete } from '@element-plus/icons-vue';
import { getCategoryTree, getCategory, addCategory, updateCategory, getCategoryAttrTypeOptions } from '/@/api/assets/category';
import { getDicts } from '/@/api/system/dict/data';
import { createFormDiff } from '/@/utils/diffUtils';
interface CategoryRow {
id: string;
@@ -181,6 +182,8 @@ const attrTypeOptions = ref<{ key: string; value: string }[]>([]);
const dictTypeOptions = ref<DictInfo[]>([]);
const dictValueOptions = ref<any[]>([]);
const dictLoading = ref(false);
// 使用通用工具函数保存原始数据,用于最小化传参
const categoryFormDiff = createFormDiff<Record<string, any>>();
const ruleForm = reactive<RuleForm>({
id: '',
@@ -362,7 +365,12 @@ const openDialog = (row?: CategoryRow | string, edit?: boolean) => {
}
}
});
// 保存原始数据用于最小化传参
categoryFormDiff.saveOriginal(JSON.parse(JSON.stringify(ruleForm)));
});
} else {
// 保存原始数据用于最小化传参
categoryFormDiff.saveOriginal(JSON.parse(JSON.stringify(ruleForm)));
}
});
} else if (row && typeof row === 'string') {
@@ -432,10 +440,16 @@ const onSubmit = () => {
options: [],
};
});
const submitData = { ...ruleForm, attrs: processedAttrs };
if (isEdit.value) {
// 修改
// 编辑模式:通过 _originalData 让拦截器自动处理最小化传参
const originalData = categoryFormDiff.getOriginal();
const submitData = {
...ruleForm,
attrs: processedAttrs,
_originalData: originalData,
};
updateCategory(submitData)
.then(() => {
ElMessage.success('修改成功');
@@ -446,7 +460,8 @@ const onSubmit = () => {
submitLoading.value = false;
});
} else {
// 新增
// 新增模式:传递所有字段
const submitData = { ...ruleForm, attrs: processedAttrs };
addCategory(submitData)
.then(() => {
ElMessage.success('添加成功');