refactor(system): 重构系统设置组件以支持按键更新
- 将 updateSetting 替换为 updateSettingByKey 方法调用 - 为所有设置组件添加默认的 settingId 和 settingKey 初始值 - 在表单提交前确保 settingKey 正确赋值 - 优化 watch 数据监听逻辑以支持按键匹配 - 重构数据处理流程以支持数组和对象格式的数据 - 统一错误处理和边界条件检查 - 修复表单重置和初始化逻辑 - 标准化各组件中的 settingKey 默认值设定
This commit is contained in:
@@ -50,7 +50,7 @@ const props = defineProps<{
|
||||
|
||||
// 保存字段信息(设定好key和描述,content里的字段是随意加的会自动转为json保存到数据库)
|
||||
const settingId = ref(undefined);
|
||||
const settingKey = ref('setting');
|
||||
const settingKey = ref('clear');
|
||||
const comments = ref('系统设置');
|
||||
// 是否开启响应式布局
|
||||
const themeStore = useThemeStore();
|
||||
@@ -64,6 +64,8 @@ const isUpdate = ref(false);
|
||||
const formRef = ref<FormInstance | null>(null);
|
||||
// 表单数据
|
||||
const { form, resetFields, assignFields } = useFormData<Setting>({
|
||||
settingId: undefined,
|
||||
settingKey: settingKey.value,
|
||||
clearCache: 'setting,dict,category,temp',
|
||||
tenantId: localStorage.getItem('TenantId')
|
||||
});
|
||||
@@ -116,22 +118,44 @@ const save = () => {
|
||||
watch(
|
||||
() => props.data,
|
||||
(data) => {
|
||||
if(data?.settingId){
|
||||
isUpdate.value = true
|
||||
// 表单赋值
|
||||
if(data.content){
|
||||
const jsonData = JSON.parse(data.content);
|
||||
assignFields(jsonData);
|
||||
}
|
||||
// 其他必要参数
|
||||
form.settingId = data.settingId
|
||||
form.settingKey = data.settingKey
|
||||
} else {
|
||||
// 新增
|
||||
isUpdate.value = false
|
||||
const activeMatch = props.value === settingKey.value;
|
||||
if (!data || typeof data !== 'object') {
|
||||
if (!activeMatch) return;
|
||||
isUpdate.value = false;
|
||||
resetFields();
|
||||
form.settingKey = props.value
|
||||
form.settingId = undefined;
|
||||
form.settingKey = settingKey.value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const normalized: any = Array.isArray(data)
|
||||
? data.find((d) => d?.settingKey === settingKey.value) ?? data[0]
|
||||
: (data as any).data && typeof (data as any).data === 'object'
|
||||
? (data as any).data
|
||||
: data;
|
||||
|
||||
let parsedContent: any | undefined;
|
||||
const rawContent = (normalized as any).content;
|
||||
if (rawContent) {
|
||||
if (typeof rawContent === 'string') {
|
||||
try {
|
||||
parsedContent = JSON.parse(rawContent);
|
||||
} catch {
|
||||
parsedContent = undefined;
|
||||
}
|
||||
} else if (typeof rawContent === 'object') {
|
||||
parsedContent = rawContent;
|
||||
}
|
||||
}
|
||||
const contentOrRow = parsedContent ?? normalized;
|
||||
const incomingKey = (contentOrRow as any).settingKey ?? (normalized as any).settingKey;
|
||||
if (!activeMatch && incomingKey !== settingKey.value) return;
|
||||
|
||||
isUpdate.value = true;
|
||||
assignFields(contentOrRow);
|
||||
form.settingId = (normalized as any).settingId;
|
||||
form.settingKey = settingKey.value;
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user