heritage-frontend/src/utils/dict.ts
Leo 715270aa49 核心架构:添加基础架构层代码
- 添加工具函数库(src/utils/)
- 添加TypeScript类型定义(src/typings/)
- 添加全局常量定义(src/constants/)
- 添加组合式函数(src/hooks/)
- 添加自定义指令(src/directives/)
- 添加公共组件(src/components/common/)
- 添加应用入口文件(src/App.vue, src/main.ts)
2025-10-08 02:25:33 +08:00

66 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<string, DictDataOption>()
dictOptions.forEach(option => map.set(option.dictValue, option))
return map
}