dict.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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({
  18. id: 'dict',
  19. state: (): DictState => ({
  20. dictMap: {}
  21. }),
  22. persist: {
  23. enabled: true
  24. },
  25. getters: {
  26. getDictMap(): Recordable {
  27. return this.dictMap
  28. },
  29. getHasDictData(): Boolean {
  30. if (this.dictMap.length > 0) {
  31. return true
  32. } else {
  33. return false
  34. }
  35. }
  36. },
  37. actions: {
  38. setDictMap(dictMap: Recordable) {
  39. // 设置数据
  40. const dictDataMap = {}
  41. dictMap.forEach((dictData: DictDataVO) => {
  42. // 获得 dictType 层级
  43. const enumValueObj = dictDataMap[dictData.dictType]
  44. if (!enumValueObj) {
  45. dictDataMap[dictData.dictType] = []
  46. }
  47. // 处理 dictValue 层级
  48. dictDataMap[dictData.dictType].push({
  49. value: dictData.value,
  50. label: dictData.label,
  51. colorType: dictData.colorType,
  52. cssClass: dictData.cssClass
  53. })
  54. })
  55. this.dictMap = dictMap
  56. }
  57. }
  58. })
  59. export const useDictStoreWithOut = () => {
  60. return useDictStore(store)
  61. }