ec-admin/src/composables/useDict.ts
liuyun 23e228783c feat: dict-driven rendering — useDict + component dictCode
- 新增 dict store 与 useDict Hook(登录后缓存、登出清空)
- ty-form/ty-table 支持 dictCode 自动解析下拉与标签
- 用户/角色/资源/组织/租户/审计/登录日志页去硬编码映射
- 字典管理页内置标记、value 保护、标签色编辑

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-11 14:29:28 +08:00

28 lines
910 B
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 { computed } from 'vue'
import { useDictStore } from '@/stores/modules/dict'
import type { TagType } from '@/types/crud'
/**
* 读取某个字典类型的字典项,返回三种视图:
* - items: 响应式字典项数组(label/value/colorType/sort)
* - tagMap: value → { label, type },供表格标签列使用
* - options: { label, value }[],供下拉选择使用
*/
export function useDict(code: string) {
const store = useDictStore()
const items = computed(() => store.dicts[code] ?? [])
const tagMap = computed(() => {
const map: Record<string, { label: string; type: TagType }> = {}
for (const it of items.value) {
map[it.value] = { label: it.label, type: (it.colorType ?? 'info') as TagType }
}
return map
})
const options = computed(() => items.value.map((it) => ({ label: it.label, value: it.value })))
return { items, tagMap, options }
}