useDict.ts 695 B

1234567891011121314151617181920212223
  1. import { useQuery } from '@tanstack/vue-query'
  2. import { getDict } from '@/service/system'
  3. import { DictRecord } from '@/interface/dict'
  4. import { Ref } from 'vue'
  5. /**
  6. * 根据类型获取字典
  7. * @param dictType
  8. */
  9. export const useDict = (
  10. dictType: string
  11. ): [data: Ref<DictRecord[] | undefined>, isLoading: Ref<boolean>] => {
  12. const { data, isLoading } = useQuery(['dictList', dictType], async () => await getDict(dictType))
  13. return [data, isLoading]
  14. }
  15. /**
  16. * 获取对应字典label
  17. * */
  18. export const getDictName = (value: number, targetDict: DictRecord[]): string => {
  19. const target = targetDict?.find((item) => String(item.value) === String(value))
  20. return target?.label ?? ''
  21. }