dict.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { defineStore } from 'pinia'
  2. import { ref } from 'vue'
  3. import { getDictTree, buildDictOptions } from '../api/dict'
  4. export const useDictStore = defineStore('dict', () => {
  5. const maps = ref<Record<string, Record<string, string>>>({})
  6. async function loadDict(type: string) {
  7. if (maps.value[type]) return
  8. try {
  9. const data = await getDictTree(type)
  10. const opts = buildDictOptions(data)
  11. maps.value[type] = opts.labelMap
  12. } catch {
  13. maps.value[type] = {}
  14. }
  15. }
  16. function getLabel(type: string, value: string): string {
  17. return maps.value[type]?.[value] || value
  18. }
  19. /** 加载任务/项目展示常用的字典映射 */
  20. async function loadCommonDicts() {
  21. await Promise.all([
  22. loadDict('facility_type'),
  23. loadDict('construction_facility'),
  24. loadDict('construction_type'),
  25. loadDict('service_method'),
  26. loadDict('suggested_vehicle'),
  27. loadDict('water_distance'),
  28. loadDict('acceptance_method'),
  29. ])
  30. }
  31. return {
  32. maps,
  33. loadDict,
  34. loadCommonDicts,
  35. getLabel,
  36. }
  37. })