From 457a4a091de3087e8dd8d06ce4d47c1845f7f44a Mon Sep 17 00:00:00 2001 From: WUSIJIAN <13825895+wsj0228@user.noreply.gitee.com> Date: Tue, 16 Dec 2025 16:00:28 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AD=97=E5=85=B8?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91,?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=A0=91=E5=BD=A2=E7=BB=93=E6=9E=84=E5=AD=97?= =?UTF-8?q?=E5=85=B8=E6=8E=A5=E5=8F=A3=E5=B9=B6=E4=BF=AE=E5=A4=8D=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=B1=9E=E6=80=A7=E5=AD=97=E6=AE=B5=E6=98=A0?= =?UTF-8?q?=E5=B0=84=E5=85=B3=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/system/dict/data.ts | 2 +- .../category/component/editCategory.vue | 75 ++++++++++++------- src/views/assets/category/index.vue | 2 +- 3 files changed, 52 insertions(+), 27 deletions(-) diff --git a/src/api/system/dict/data.ts b/src/api/system/dict/data.ts index c97ae71..87fef0b 100644 --- a/src/api/system/dict/data.ts +++ b/src/api/system/dict/data.ts @@ -8,7 +8,7 @@ export function getDicts(dictType :string,defaultValue?:string):Promise { defaultValue:dv } return request({ - url: '/api/v1/system/dict/data/getDictData', + url: '/api/v1/system/dict/data/getDictDataTree', method: 'get', params:params }) diff --git a/src/views/assets/category/component/editCategory.vue b/src/views/assets/category/component/editCategory.vue index 552b395..c3f20dc 100644 --- a/src/views/assets/category/component/editCategory.vue +++ b/src/views/assets/category/component/editCategory.vue @@ -51,7 +51,7 @@ ([]); const dictTypeOptions = ref([]); -const dictValueOptions = ref([]); +const dictValueOptions = ref([]); const dictLoading = ref(false); const ruleForm = reactive({ - id: '', + id: '', parentId: '', name: '', sort: 0, @@ -185,18 +185,13 @@ const fetchDictTypeOptions = () => { getDicts('assets') .then((res: any) => { console.log('字典接口返回数据:', res); - const info = res.data?.info; - let infoList: DictInfo[] = []; - // 处理info可能是对象或数组的情况 - if (Array.isArray(info)) { - infoList = info; - } else if (info && typeof info === 'object') { - infoList = [info]; - } - // 过滤掉name为空的项 - dictTypeOptions.value = infoList.filter((item: DictInfo) => item && item.name); - // 保存字典值列表 - dictValueOptions.value = res.data?.values ?? []; + const list = res.data?.list ?? []; + // 提取所有字典类型信息 + dictTypeOptions.value = list + .map((item: any) => item.info) + .filter((info: DictInfo) => info && info.name); + // 保存完整的字典数据列表(包含info和values) + dictValueOptions.value = list; console.log('字典类型选项:', dictTypeOptions.value); console.log('字典值选项:', dictValueOptions.value); }) @@ -213,15 +208,15 @@ const fetchDictTypeOptions = () => { // 根据字典类型获取对应的字典值 const getDictValuesByType = (dictKey: string) => { if (!dictKey) return []; - // 暂时返回所有字典值,后续可根据实际数据结构调整过滤逻辑 - // 如果需要按类型过滤,可以根据后端返回的数据结构调整 - return dictValueOptions.value; + // 根据字典类型名称找到对应的字典数据 + const dictItem = dictValueOptions.value.find((item: any) => item.info?.name === dictKey); + return dictItem?.values ?? []; }; // 字典类型选择变化时 const onDictKeyChange = (attr: CustomAttr) => { // 清空已选的字典值 - attr.dictValues = []; + attr.options = []; }; // 添加自定义属性 @@ -281,7 +276,18 @@ const openDialog = (row?: CategoryRow | string, edit?: boolean) => { ruleForm.name = data.name || ''; ruleForm.sort = data.sort || 0; ruleForm.status = data.status || 'enabled'; - ruleForm.attrs = data.attrs || []; + // 处理 attrs 中的 options 字段(后端可能返回字符串格式) + ruleForm.attrs = (data.attrs || []).map((attr: any) => { + let options = attr.options; + if (typeof options === 'string') { + try { + options = JSON.parse(options); + } catch { + options = []; + } + } + return { ...attr, options: options || [] }; + }); // 如果有单选/多选属性,预加载字典类型数据 if (ruleForm.attrs.some((attr: CustomAttr) => attr.type === 'select' || attr.type === 'multi_select')) { fetchDictTypeOptions(); @@ -311,13 +317,32 @@ const onSubmit = () => { formRef.value.validate((valid: boolean) => { if (valid) { submitLoading.value = true; - const submitData = { ...ruleForm }; + // 处理 attrs:单选/多选类型不传递 name,并转换 options 格式 + const processedAttrs = ruleForm.attrs.map((attr) => { + if (attr.type === 'select' || attr.type === 'multi_select') { + const { name, ...rest } = attr; + // 将 options 从 ["red"] 转换为 [{"label":"红色","value":"red"}] + const dictValues = getDictValuesByType(attr.description || ''); + const formattedOptions = (attr.options || []).map((optValue: string) => { + const dictItem = dictValues.find((d: any) => d.key === optValue); + return { + label: dictItem?.value || optValue, + value: optValue, + }; + }); + return { ...rest, options: formattedOptions }; + } + return attr; + }); + const submitData = { ...ruleForm, attrs: processedAttrs }; if (isEdit.value) { // 修改 updateCategory(submitData) .then(() => { ElMessage.success('修改成功'); + console.log(submitData,'111'); + closeDialog(); emit('getCategoryList'); }) diff --git a/src/views/assets/category/index.vue b/src/views/assets/category/index.vue index 9b1c9fb..361a829 100644 --- a/src/views/assets/category/index.vue +++ b/src/views/assets/category/index.vue @@ -7,7 +7,7 @@ - + 查询 From 4e7cf020298cd844f39e9425cca31671fd79c891 Mon Sep 17 00:00:00 2001 From: WUSIJIAN <13825895+wsj0228@user.noreply.gitee.com> Date: Tue, 16 Dec 2025 18:01:32 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=B1=9E=E6=80=A7=E9=80=89=E9=A1=B9=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91,=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E6=95=B0=E7=BB=84=E4=B8=8E=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E6=95=B0=E7=BB=84=E5=8F=8C=E5=90=91=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../category/component/editCategory.vue | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/views/assets/category/component/editCategory.vue b/src/views/assets/category/component/editCategory.vue index c3f20dc..bfb06ae 100644 --- a/src/views/assets/category/component/editCategory.vue +++ b/src/views/assets/category/component/editCategory.vue @@ -276,9 +276,10 @@ const openDialog = (row?: CategoryRow | string, edit?: boolean) => { ruleForm.name = data.name || ''; ruleForm.sort = data.sort || 0; ruleForm.status = data.status || 'enabled'; - // 处理 attrs 中的 options 字段(后端可能返回字符串格式) + // 处理 attrs 中的 options 字段 ruleForm.attrs = (data.attrs || []).map((attr: any) => { let options = attr.options; + // 后端可能返回字符串格式,先解析 if (typeof options === 'string') { try { options = JSON.parse(options); @@ -286,6 +287,11 @@ const openDialog = (row?: CategoryRow | string, edit?: boolean) => { options = []; } } + // 如果是对象数组 [{label, value}],转换为简单数组 [value] + // 这样前端选择器才能正确工作 + if (Array.isArray(options) && options.length > 0 && typeof options[0] === 'object') { + options = options.map((opt: any) => opt.value || opt.key || opt); + } return { ...attr, options: options || [] }; }); // 如果有单选/多选属性,预加载字典类型数据 @@ -321,15 +327,24 @@ const onSubmit = () => { const processedAttrs = ruleForm.attrs.map((attr) => { if (attr.type === 'select' || attr.type === 'multi_select') { const { name, ...rest } = attr; - // 将 options 从 ["red"] 转换为 [{"label":"红色","value":"red"}] - const dictValues = getDictValuesByType(attr.description || ''); - const formattedOptions = (attr.options || []).map((optValue: string) => { - const dictItem = dictValues.find((d: any) => d.key === optValue); - return { - label: dictItem?.value || optValue, - value: optValue, - }; - }); + const options = attr.options || []; + let formattedOptions: any[] = []; + + // 判断 options 是否已经是对象数组格式 + if (options.length > 0 && typeof options[0] === 'object') { + // 已经是对象数组格式,直接使用 + formattedOptions = options; + } else { + // 是字符串数组,需要转换为 [{label, value}] 格式 + const dictValues = getDictValuesByType(attr.description || ''); + formattedOptions = options.map((optValue: string) => { + const dictItem = dictValues.find((d: any) => d.key === optValue); + return { + label: dictItem?.value || optValue, + value: optValue, + }; + }); + } return { ...rest, options: formattedOptions }; } return attr; From bf3156fcce75d99cb7081d1b60dfeb517fb011dc Mon Sep 17 00:00:00 2001 From: WUSIJIAN <13825895+wsj0228@user.noreply.gitee.com> Date: Wed, 17 Dec 2025 09:19:50 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=B1=9E=E6=80=A7=E5=AD=97=E5=85=B8=E9=80=89=E9=A1=B9?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E9=97=AE=E9=A2=98,=E5=B0=86=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E4=BB=8Ekey=E6=94=B9=E4=B8=BAvalue=E4=BB=A5=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE=E6=98=BE=E7=A4=BA=E5=AD=97=E5=85=B8=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/assets/category/component/editCategory.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/views/assets/category/component/editCategory.vue b/src/views/assets/category/component/editCategory.vue index bfb06ae..390f850 100644 --- a/src/views/assets/category/component/editCategory.vue +++ b/src/views/assets/category/component/editCategory.vue @@ -78,7 +78,7 @@