dict.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { defineStore } from 'pinia'
  2. import { store } from '../index'
  3. import { DictDataVO } from '@/api/system/dict/types'
  4. export interface DictValueType {
  5. value: string
  6. label: string
  7. clorType: string
  8. cssClass: string
  9. }
  10. export interface DictTypeType {
  11. dictType: string
  12. dictValue: DictValueType[]
  13. }
  14. export interface DictState {
  15. dictMap: Recordable
  16. }
  17. export const useDictStore = defineStore('dict', {
  18. state: (): DictState => ({
  19. dictMap: {}
  20. }),
  21. getters: {
  22. getDictMap(): Recordable {
  23. return this.dictMap
  24. },
  25. getHasDictData(): boolean {
  26. if (this.dictMap.length > 0) {
  27. return true
  28. } else {
  29. return false
  30. }
  31. }
  32. },
  33. actions: {
  34. setDictMap(dictMap: Recordable) {
  35. // 设置数据
  36. const dictDataMap = {}
  37. dictMap.forEach((dictData: DictDataVO) => {
  38. // 获得 dictType 层级
  39. const enumValueObj = dictDataMap[dictData.dictType]
  40. if (!enumValueObj) {
  41. dictDataMap[dictData.dictType] = []
  42. }
  43. // 处理 dictValue 层级
  44. dictDataMap[dictData.dictType].push({
  45. value: dictData.value,
  46. label: dictData.label,
  47. colorType: dictData.colorType,
  48. cssClass: dictData.cssClass
  49. })
  50. })
  51. this.dictMap = dictMap
  52. }
  53. }
  54. })
  55. export const useDictStoreWithOut = () => {
  56. return useDictStore(store)
  57. }