refactor(setting): 更新设置组件以支持基于键值的更新

- 将 updateSetting 替换为 updateSettingByKey 方法调用
- 为所有设置组件初始化 settingKey 和 settingId 字段
- 重构 watch 监听逻辑以支持多类型数据格式处理
- 实现对 content 字段的字符串和对象格式解析
- 添加对数组和对象数据类型的兼容性支持
- 优化表单数据赋值和重置逻辑
- 统一各组件中的数据处理流程
This commit is contained in:
2026-02-27 22:59:51 +08:00
parent 75d31dd09e
commit 6a9be80811
15 changed files with 778 additions and 456 deletions

View File

@@ -88,7 +88,7 @@ import { useThemeStore } from '@/store/modules/theme';
import { storeToRefs } from 'pinia';
import { FormInstance } from 'ant-design-vue/es/form';
import useFormData from '@/utils/use-form-data';
import { addSetting, updateSetting } from "@/api/system/setting";
import { addSetting, updateSettingByKey } from "@/api/system/setting";
import { ItemType } from "ele-admin-pro/es/ele-image-upload/types";
import { uploadFile } from "@/api/system/file";
import { FILE_SERVER } from "@/config/setting";
@@ -117,6 +117,8 @@ const isUpdate = ref(false);
const formRef = ref<FormInstance | null>(null);
// 表单数据
const { form, resetFields, assignFields } = useFormData<Setting>({
settingId: undefined,
settingKey: settingKey.value,
isOpenPrinter: '0',
printerType: '1',
printerStatus: '20',
@@ -174,11 +176,12 @@ const save = () => {
.validate()
.then(() => {
loading.value = true;
form.settingKey = settingKey.value;
const appForm = {
...form,
content: JSON.stringify(form)
};
const saveOrUpdate = isUpdate.value ? updateSetting : addSetting;
const saveOrUpdate = isUpdate.value ? updateSettingByKey : addSetting;
saveOrUpdate(appForm)
.then((msg) => {
message.success('保存成功');
@@ -193,22 +196,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>