- 添加工具函数库(src/utils/) - 添加TypeScript类型定义(src/typings/) - 添加全局常量定义(src/constants/) - 添加组合式函数(src/hooks/) - 添加自定义指令(src/directives/) - 添加公共组件(src/components/common/) - 添加应用入口文件(src/App.vue, src/main.ts)
66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
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
|
||
}
|