useDict.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import type { DictItem } from '@/store/dict'
  2. import { useDictStore } from '@/store/dict'
  3. type ColorType = 'error' | 'info' | 'primary' | 'success' | 'warning'
  4. export interface DictDataType {
  5. dictType?: string
  6. label: string
  7. value: boolean | number | string
  8. colorType?: string
  9. cssClass?: string
  10. }
  11. export interface NumberDictDataType extends DictDataType {
  12. value: number
  13. }
  14. export interface StringDictDataType extends DictDataType {
  15. value: string
  16. }
  17. /**
  18. * 获取字典标签
  19. *
  20. * @param dictType 字典类型
  21. * @param value 字典值
  22. * @returns 字典标签
  23. */
  24. export function getDictLabel(dictType: string, value: any): string {
  25. const dictStore = useDictStore()
  26. const dictObj = dictStore.getDictData(dictType, value)
  27. return dictObj ? dictObj.label : ''
  28. }
  29. /**
  30. * 获取字典对象
  31. *
  32. * @param dictType 字典类型
  33. * @param value 字典值
  34. * @returns 字典对象
  35. */
  36. export function getDictObj(dictType: string, value: any): DictItem | null {
  37. const dictStore = useDictStore()
  38. const dictObj = dictStore.getDictData(dictType, value)
  39. return dictObj || null
  40. }
  41. export function getIntDictOptions(dictType: string): NumberDictDataType[] {
  42. // 获得通用的 DictDataType 列表
  43. const dictOptions: DictDataType[] = getDictOptions(dictType)
  44. // 转换成 number 类型的 NumberDictDataType 类型
  45. // why 需要特殊转换:避免 IDEA 在 v-for="dict in getIntDictOptions(...)" 时,el-option 的 key 会告警
  46. const dictOption: NumberDictDataType[] = []
  47. dictOptions.forEach((dict: DictDataType) => {
  48. dictOption.push({
  49. ...dict,
  50. value: Number.parseInt(`${dict.value}`),
  51. })
  52. })
  53. return dictOption
  54. }
  55. export function getStrDictOptions(dictType: string) {
  56. // 获得通用的 DictDataType 列表
  57. const dictOptions: DictDataType[] = getDictOptions(dictType)
  58. // 转换成 string 类型的 StringDictDataType 类型
  59. // why 需要特殊转换:避免 IDEA 在 v-for="dict in getStrDictOptions(...)" 时,el-option 的 key 会告警
  60. const dictOption: StringDictDataType[] = []
  61. dictOptions.forEach((dict: DictDataType) => {
  62. dictOption.push({
  63. ...dict,
  64. value: `${dict.value}`,
  65. })
  66. })
  67. return dictOption
  68. }
  69. export function getBoolDictOptions(dictType: string) {
  70. const dictOption: DictDataType[] = []
  71. const dictOptions: DictDataType[] = getDictOptions(dictType)
  72. dictOptions.forEach((dict: DictDataType) => {
  73. dictOption.push({
  74. ...dict,
  75. value: `${dict.value}` === 'true',
  76. })
  77. })
  78. return dictOption
  79. }
  80. /**
  81. * 获取字典数组,用于 picker、radio 等
  82. *
  83. * @param dictType 字典类型
  84. * @param valueType 字典值类型,默认 string 类型
  85. * @returns 字典数组
  86. */
  87. export function getDictOptions(
  88. dictType: string,
  89. valueType: 'boolean' | 'number' | 'string' = 'string',
  90. ): DictDataType[] {
  91. const dictStore = useDictStore()
  92. const dictOpts = dictStore.getDictOptions(dictType)
  93. const dictOptions: DictDataType[] = []
  94. if (dictOpts.length > 0) {
  95. let dictValue: boolean | number | string = ''
  96. dictOpts.forEach((dict) => {
  97. switch (valueType) {
  98. case 'boolean': {
  99. dictValue = `${dict.value}` === 'true'
  100. break
  101. }
  102. case 'number': {
  103. dictValue = Number.parseInt(`${dict.value}`)
  104. break
  105. }
  106. case 'string': {
  107. dictValue = `${dict.value}`
  108. break
  109. }
  110. }
  111. dictOptions.push({
  112. value: dictValue,
  113. label: dict.label,
  114. colorType: dict.colorType as ColorType,
  115. cssClass: dict.cssClass,
  116. })
  117. })
  118. }
  119. return dictOptions
  120. }