import type { DictDataOption } from '@/service/api/system/dict' export type DictValue = string | number | boolean | null | undefined /** * 将字典选项转换为 Naive UI Select 所需结构 */ export function toSelectOptions(dictOptions: DictDataOption[] = []) { return dictOptions.map(option => ({ label: option.dictLabel, value: option.dictValue, })) } /** * 通过字典值获取对应的完整选项对象 */ export function findDictOption(dictOptions: DictDataOption[] = [], value: DictValue) { if (value === undefined || value === null) return undefined const target = String(value) return dictOptions.find(option => option.dictValue === target) } /** * 获取字典标签,找不到时返回默认值或原始值 */ export function findDictLabel(dictOptions: DictDataOption[] = [], value: DictValue, fallback?: string) { const target = findDictOption(dictOptions, value) if (target) return target.dictLabel if (fallback !== undefined) return fallback if (value === undefined || value === null || value === '') return '' return String(value) } /** * 获取字典颜色配置 */ export function findDictColor(dictOptions: DictDataOption[] = [], value: DictValue) { const target = findDictOption(dictOptions, value) if (!target) return undefined return { tag: target.dictTag, color: target.dictColor, label: target.dictLabel, } } /** * 将字典数组转为值 -> 选项的 Map,方便重复查询 */ export function createDictMap(dictOptions: DictDataOption[] = []) { const map = new Map() dictOptions.forEach(option => map.set(option.dictValue, option)) return map }