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..390f850 100644 --- a/src/views/assets/category/component/editCategory.vue +++ b/src/views/assets/category/component/editCategory.vue @@ -51,7 +51,7 @@ @@ -126,7 +126,7 @@ interface CustomAttr { type: string; required?: boolean; multiple?: boolean; - options?: string; + options?: string[]; description?: string; sort?: number; dictKey?: string; @@ -163,11 +163,11 @@ const isAddChild = ref(false); const submitLoading = ref(false); const categoryData = ref([]); 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,24 @@ 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 = []; + } + } + // 如果是对象数组 [{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 || [] }; + }); // 如果有单选/多选属性,预加载字典类型数据 if (ruleForm.attrs.some((attr: CustomAttr) => attr.type === 'select' || attr.type === 'multi_select')) { fetchDictTypeOptions(); @@ -311,13 +323,41 @@ 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; + 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; + }); + 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 @@ - + 查询