refactor(setting): 优化系统设置组件的数据处理逻辑
- 将 updateSetting 替换为 updateSettingByKey 方法调用 - 统一设置表单中的 settingKey 字段值 - 重构 watch 数据监听逻辑,增强数据类型检查 - 改进表单初始化和数据回显流程 - 添加对 content 字段的 JSON 解析错误处理 - 标准化各设置组件的表单数据结构 - 优化数组和对象数据的查找匹配逻辑
This commit is contained in:
@@ -113,7 +113,7 @@
|
||||
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 { copyText } from '@/utils/common';
|
||||
import { CopyOutlined } from '@ant-design/icons-vue';
|
||||
|
||||
@@ -130,6 +130,7 @@
|
||||
// 保存字段信息(设定好key和描述,content里的字段是随意加的会自动转为json保存到数据库)
|
||||
// const settingId = ref(null);
|
||||
// const settingKey = ref('');
|
||||
const settingKey = ref('wx-work');
|
||||
// 是否开启响应式布局
|
||||
const themeStore = useThemeStore();
|
||||
const { styleResponsive } = storeToRefs(themeStore);
|
||||
@@ -141,6 +142,8 @@
|
||||
const formRef = ref<FormInstance | null>(null);
|
||||
// 表单数据
|
||||
const { form, resetFields, assignFields } = useFormData<Setting>({
|
||||
settingId: undefined,
|
||||
settingKey: settingKey.value,
|
||||
suiteId: '',
|
||||
secret: '',
|
||||
corpId: '',
|
||||
@@ -170,11 +173,12 @@
|
||||
.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(() => {
|
||||
message.success('保存成功');
|
||||
@@ -193,22 +197,45 @@
|
||||
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 {
|
||||
// 新增
|
||||
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