diff --git a/src/views/assets/category/component/editCategory.vue b/src/views/assets/category/component/editCategory.vue index 6f009b1..8c09c00 100644 --- a/src/views/assets/category/component/editCategory.vue +++ b/src/views/assets/category/component/editCategory.vue @@ -84,7 +84,7 @@ :max-collapse-tags="2" > { // 获取字典类型数据 const fetchDictTypeOptions = () => { dictLoading.value = true; - getDicts('assets') + return getDicts('assets') .then((res: any) => { const list = res.data?.list ?? []; // 提取所有字典类型信息 @@ -231,11 +233,18 @@ const fetchDictTypeOptions = () => { }); }; -// 根据字典类型获取对应的字典值 -const getDictValuesByType = (dictKey: string) => { - if (!dictKey) return []; - // 根据字典类型名称找到对应的字典数据 - const dictItem = dictValueOptions.value.find((item: any) => item.info?.name === dictKey); +// 根据字典类型获取对应的字典值(优先使用 dictType 匹配) +const getDictValuesByType = (dictName: string, dictType?: string) => { + if (!dictName && !dictType) return []; + // 优先使用 dictType 匹配,这样即使字典名称修改也能正确匹配 + let dictItem; + if (dictType) { + dictItem = dictValueOptions.value.find((item: any) => item.info?.type === dictType); + } + // 如果 dictType 没匹配到,再用 name 匹配(兼容旧数据) + if (!dictItem && dictName) { + dictItem = dictValueOptions.value.find((item: any) => item.info?.name === dictName); + } return dictItem?.values ?? []; }; @@ -246,13 +255,19 @@ const isDictType = (type?: string) => type === 'select' || type === 'multi_selec const onDictKeyChange = (attr: CustomAttr) => { // 清空已选的字典值 attr.options = []; + // 根据选择的字典名称,找到对应的 dictType 并保存 + const selectedDict = dictTypeOptions.value.find((item) => item.name === attr.name); + attr.dictType = selectedDict?.type || ''; }; // 判断字典选项是否应被禁用 const isDictOptionDisabled = (dictName: string, currentAttr: CustomAttr) => { if (!dictName) return false; - // 检查该字典名称是否已被其他属性使用 - return ruleForm.attrs.some((attr) => attr !== currentAttr && isDictType(attr.type) && attr.name === dictName); + // 找到该字典对应的 type + const dictInfo = dictTypeOptions.value.find((item) => item.name === dictName); + const dictType = dictInfo?.type || ''; + // 检查该字典是否已被其他属性使用(使用 dictType 判断) + return ruleForm.attrs.some((attr) => attr !== currentAttr && isDictType(attr.type) && (attr.dictType === dictType || (!attr.dictType && attr.name === dictName))); }; // 添加自定义属性 @@ -335,9 +350,19 @@ const openDialog = (row?: CategoryRow | string, edit?: boolean) => { } return { ...attr, options: options || [] }; }); - // 如果有单选/多选属性,预加载字典类型数据 + // 如果有单选/多选属性,预加载字典类型数据并更新字典名称 if (ruleForm.attrs.some((attr: CustomAttr) => attr.type === 'select' || attr.type === 'multi_select')) { - fetchDictTypeOptions(); + fetchDictTypeOptions().then(() => { + // 根据 dictType 更新字典名称(字典名称可能已被修改) + ruleForm.attrs.forEach((attr: CustomAttr) => { + if (isDictType(attr.type) && attr.dictType) { + const dictInfo = dictTypeOptions.value.find((item) => item.type === attr.dictType); + if (dictInfo) { + attr.name = dictInfo.name; + } + } + }); + }); } }); } else if (row && typeof row === 'string') { @@ -368,7 +393,7 @@ const formatDictOptions = (attr: CustomAttr) => { value: opt.value ?? opt.key ?? '', })); } - const dictValues = getDictValuesByType(attr.name || ''); + const dictValues = getDictValuesByType(attr.name || '', attr.dictType); return options.map((optValue: string) => { const dictItem = dictValues.find((d: any) => d.key === optValue); return { @@ -396,6 +421,7 @@ const onSubmit = () => { return { ...base, name: attr.name || '', + dictType: attr.dictType || '', options: formatDictOptions(attr), }; }