From acec6570e129d51124b8ad003686c2eab359786a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BF=A0=E6=9E=97?= <170083662@qq.com> Date: Fri, 27 Feb 2026 18:36:15 +0800 Subject: [PATCH] =?UTF-8?q?refactor(setting):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE=E7=BB=84=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 updateSetting 替换为 updateSettingByKey 方法调用 - 统一设置表单中的 settingKey 字段值 - 重构 watch 数据监听逻辑,增强数据类型检查 - 改进表单初始化和数据回显流程 - 添加对 content 字段的 JSON 解析错误处理 - 标准化各设置组件的表单数据结构 - 优化数组和对象数据的查找匹配逻辑 --- src/views/system/setting/components/basic.vue | 89 ++-- src/views/system/setting/components/clear.vue | 56 ++- .../system/setting/components/developer.vue | 86 ++-- .../system/setting/components/mp-weixin.vue | 59 ++- .../system/setting/components/payment.vue | 59 ++- .../system/setting/components/printer.vue | 59 ++- .../system/setting/components/privacy.vue | 47 +- .../system/setting/components/register.vue | 61 ++- src/views/system/setting/components/sms.vue | 64 ++- .../system/setting/components/upload.vue | 438 +++++++++--------- .../system/setting/components/website.vue | 81 ++-- .../system/setting/components/wx-official.vue | 59 ++- .../system/setting/components/wx-work.vue | 59 ++- src/views/system/setting/index.vue | 14 +- 14 files changed, 776 insertions(+), 455 deletions(-) diff --git a/src/views/system/setting/components/basic.vue b/src/views/system/setting/components/basic.vue index 2271bc2..d9208ee 100644 --- a/src/views/system/setting/components/basic.vue +++ b/src/views/system/setting/components/basic.vue @@ -132,7 +132,7 @@ import { storeToRefs } from 'pinia'; import { FormInstance } from 'ant-design-vue/es/form'; import useFormData from '@/utils/use-form-data'; - import { addSetting, listSetting, updateSetting } from "@/api/system/setting"; + import { addSetting, listSetting, 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"; @@ -149,7 +149,7 @@ // 保存字段信息(设定好key和描述,content里的字段是随意加的会自动转为json保存到数据库) const settingId = ref(null); - const settingKey = ref(''); + const settingKey = ref('basic'); // 是否开启响应式布局 const themeStore = useThemeStore(); const { styleResponsive } = storeToRefs(themeStore); @@ -167,6 +167,8 @@ const formRef = ref(null); // 表单数据 const { form, resetFields, assignFields } = useFormData({ + settingId: undefined, + settingKey: settingKey.value, siteName: '', icp: '', copyright: '', @@ -266,11 +268,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((msg) => { message.success('保存成功'); @@ -285,32 +288,64 @@ watch( () => props.data, (data) => { - if(data?.settingId){ - isUpdate.value = true - // 表单赋值 - if(data.content){ - const jsonData = JSON.parse(data.content); - assignFields(jsonData); - // 头像赋值 - logo.value = []; - if (jsonData.logo) { - logo.value.push({ uid:1, url: jsonData.logo, status: '' }); - } - if(jsonData.keyword){ - keyword.value = JSON.parse(jsonData.keyword) - } - } - // 其他必要参数 - 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 + logo.value = []; + keyword.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); + + // 头像/关键词回显(兼容 content 与平铺字段两种返回) + logo.value = []; + const logoPath = (contentOrRow as any).logo; + if (logoPath) { + logo.value.push({ uid: 1, url: logoPath, status: '' }); + } + const rawKeyword = (contentOrRow as any).keyword; + if (rawKeyword) { + try { + keyword.value = typeof rawKeyword === 'string' ? JSON.parse(rawKeyword) : rawKeyword; + } catch { + keyword.value = []; + } + } + + form.settingId = (normalized as any).settingId; + form.settingKey = settingKey.value; + }, + { immediate: true } ); diff --git a/src/views/system/setting/components/clear.vue b/src/views/system/setting/components/clear.vue index 30e78f1..bfa62c1 100644 --- a/src/views/system/setting/components/clear.vue +++ b/src/views/system/setting/components/clear.vue @@ -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(null); // 表单数据 const { form, resetFields, assignFields } = useFormData({ + 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 } ); diff --git a/src/views/system/setting/components/developer.vue b/src/views/system/setting/components/developer.vue index 4dc4887..ce90bc9 100644 --- a/src/views/system/setting/components/developer.vue +++ b/src/views/system/setting/components/developer.vue @@ -25,7 +25,7 @@ import { storeToRefs } from 'pinia'; import { FormInstance } from 'ant-design-vue/es/form'; import useFormData from '@/utils/use-form-data'; - import { addSetting, listSetting, updateSetting } from "@/api/system/setting"; + import { addSetting, listSetting, 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"; @@ -61,6 +61,8 @@ const formRef = ref(null); // 表单数据 const { form, resetFields, assignFields } = useFormData({ + settingId: undefined, + settingKey: settingKey.value, siteName: '', icp: '', copyright: '', @@ -152,11 +154,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((msg) => { message.success('保存成功'); @@ -171,32 +174,63 @@ watch( () => props.data, (data) => { - if(data?.settingId){ - isUpdate.value = true - // 表单赋值 - if(data.content){ - const jsonData = JSON.parse(data.content); - assignFields(jsonData); - // 头像赋值 - logo.value = []; - if (jsonData.logo) { - logo.value.push({ uid:1, url: FILE_SERVER + jsonData.logo, status: '' }); - } - if(jsonData.keyword){ - keyword.value = JSON.parse(jsonData.keyword) - } - } - // 其他必要参数 - 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 + logo.value = []; + keyword.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); + + // 头像/关键词回显(兼容 content 与平铺字段两种返回) + logo.value = []; + const logoPath = (contentOrRow as any).logo; + if (logoPath) { + logo.value.push({ uid: 1, url: FILE_SERVER + logoPath, status: '' }); + } + const rawKeyword = (contentOrRow as any).keyword; + if (rawKeyword) { + try { + keyword.value = typeof rawKeyword === 'string' ? JSON.parse(rawKeyword) : rawKeyword; + } catch { + keyword.value = []; + } + } + + form.settingId = (normalized as any).settingId; + form.settingKey = settingKey.value; + }, + { immediate: true } ); diff --git a/src/views/system/setting/components/mp-weixin.vue b/src/views/system/setting/components/mp-weixin.vue index e47f2ab..e51d17f 100644 --- a/src/views/system/setting/components/mp-weixin.vue +++ b/src/views/system/setting/components/mp-weixin.vue @@ -86,7 +86,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"; @@ -116,6 +116,8 @@ const isUpdate = ref(false); const formRef = ref(null); // 表单数据 const { form, resetFields, assignFields } = useFormData({ + settingId: undefined, + settingKey: settingKey.value, appId: '', appSecret: '', tenantId: localStorage.getItem('TenantId') @@ -169,11 +171,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('保存成功'); @@ -189,23 +192,45 @@ 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 } ); diff --git a/src/views/system/setting/components/payment.vue b/src/views/system/setting/components/payment.vue index 1f7a2b1..b488262 100644 --- a/src/views/system/setting/components/payment.vue +++ b/src/views/system/setting/components/payment.vue @@ -257,7 +257,7 @@ import { storeToRefs } from "pinia"; import { UploadOutlined } from '@ant-design/icons-vue'; 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 Upload from "@/components/UploadCert/index.vue"; @@ -287,6 +287,8 @@ const token = localStorage.getItem(TOKEN_STORE_NAME); const formRef = ref(null); // 表单数据 const { form, resetFields, assignFields } = useFormData({ + settingId: undefined, + settingKey: settingKey.value, payMethod: 10, signMode: "公钥证书", appId: "", @@ -477,11 +479,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("保存成功"); @@ -498,23 +501,47 @@ 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 { - // 新增 + 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 } ); diff --git a/src/views/system/setting/components/printer.vue b/src/views/system/setting/components/printer.vue index aee9642..01740ae 100644 --- a/src/views/system/setting/components/printer.vue +++ b/src/views/system/setting/components/printer.vue @@ -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(null); // 表单数据 const { form, resetFields, assignFields } = useFormData({ + 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 } ); diff --git a/src/views/system/setting/components/privacy.vue b/src/views/system/setting/components/privacy.vue index e590d03..2680c34 100644 --- a/src/views/system/setting/components/privacy.vue +++ b/src/views/system/setting/components/privacy.vue @@ -114,16 +114,47 @@ watch( () => props.data, (data) => { - console.log(data, 'propss'); - if (data?.settingKey) { - isUpdate.value = true; - // 表单赋值 - assignFields(data); - } else { - // 新增 + const settingKey = 'privacy'; + const activeMatch = props.value === settingKey; + if (!data || typeof data !== 'object') { + if (!activeMatch) return; isUpdate.value = false; resetFields(); + form.settingId = undefined; + form.settingKey = settingKey; + return; } - } + + const normalized: any = Array.isArray(data) + ? data.find((d) => d?.settingKey === settingKey) ?? 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) return; + + isUpdate.value = true; + assignFields(contentOrRow); + form.settingId = (normalized as any).settingId; + form.settingKey = settingKey; + }, + { immediate: true } ); diff --git a/src/views/system/setting/components/register.vue b/src/views/system/setting/components/register.vue index 96f0b05..5d392d7 100644 --- a/src/views/system/setting/components/register.vue +++ b/src/views/system/setting/components/register.vue @@ -78,7 +78,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 RoleSelect from './role-select.vue'; @@ -108,7 +108,8 @@ const isUpdate = ref(false); const formRef = ref(null); // 表单数据 const { form, resetFields, assignFields } = useFormData({ - settingKey: '', + settingId: undefined, + settingKey: settingKey.value, type: 1, roleId: undefined, openWxAuth: 1, @@ -196,11 +197,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('保存成功'); @@ -215,22 +217,45 @@ 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 = settingKey.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 } ); diff --git a/src/views/system/setting/components/sms.vue b/src/views/system/setting/components/sms.vue index b9b8723..70577a5 100644 --- a/src/views/system/setting/components/sms.vue +++ b/src/views/system/setting/components/sms.vue @@ -109,7 +109,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, listSetting, updateSetting } from "@/api/system/setting"; +import { addSetting, listSetting, 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"; @@ -127,7 +127,7 @@ const emit = defineEmits<{ // 保存字段信息(设定好key和描述,content里的字段是随意加的会自动转为json保存到数据库) const settingId = ref(); -const settingKey = ref(''); +const settingKey = ref('sms'); // 是否开启响应式布局 const themeStore = useThemeStore(); const { styleResponsive } = storeToRefs(themeStore); @@ -141,7 +141,8 @@ const isUpdate = ref(false); const formRef = ref(null); // 表单数据 const { form, resetFields, assignFields } = useFormData({ - settingKey: '', + settingId: undefined, + settingKey: settingKey.value, type: 1, accessKeyId: '', accessKeySecret: '', @@ -206,11 +207,14 @@ const save = () => { .validate() .then(() => { loading.value = true; + // Keep key stable; parent `props.value` is the active tab key and may change. + form.settingKey = settingKey.value; const appForm = { ...form, content: JSON.stringify(form) }; - const saveOrUpdate = isUpdate.value ? updateSetting : addSetting; + // `getByKey` may not include `settingId`; update by key is safer here. + const saveOrUpdate = isUpdate.value ? updateSettingByKey : addSetting; saveOrUpdate(appForm) .then((msg) => { message.success('保存成功'); @@ -225,22 +229,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 } ); diff --git a/src/views/system/setting/components/upload.vue b/src/views/system/setting/components/upload.vue index b578b19..1f5a9da 100644 --- a/src/views/system/setting/components/upload.vue +++ b/src/views/system/setting/components/upload.vue @@ -16,46 +16,25 @@