dict.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { defineStore } from 'pinia'
  2. import { store } from '../index'
  3. import { DictDataVO } from '@/api/system/dict/types'
  4. import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
  5. const { wsCache } = useCache('sessionStorage')
  6. import { listSimpleDictData } from '@/api/system/dict/dict.data'
  7. export interface DictValueType {
  8. value: any
  9. label: string
  10. clorType?: string
  11. cssClass?: string
  12. }
  13. export interface DictTypeType {
  14. dictType: string
  15. dictValue: DictValueType[]
  16. }
  17. export interface DictState {
  18. dictMap: Map<string, any>
  19. isSetDict: boolean
  20. }
  21. export const useDictStore = defineStore('dict', {
  22. state: (): DictState => ({
  23. dictMap: new Map<string, any>(),
  24. isSetDict: false
  25. }),
  26. getters: {
  27. getDictMap(): Recordable {
  28. const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
  29. if (dictMap) {
  30. this.dictMap = dictMap
  31. }
  32. return this.dictMap
  33. },
  34. getIsSetDict(): boolean {
  35. return this.isSetDict
  36. }
  37. },
  38. actions: {
  39. async setDictMap() {
  40. const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
  41. if (dictMap) {
  42. this.dictMap = dictMap
  43. this.isSetDict = true
  44. } else {
  45. const res = await listSimpleDictData()
  46. // 设置数据
  47. const dictDataMap = new Map<string, any>()
  48. res.forEach((dictData: DictDataVO) => {
  49. // 获得 dictType 层级
  50. const enumValueObj = dictDataMap[dictData.dictType]
  51. if (!enumValueObj) {
  52. dictDataMap[dictData.dictType] = []
  53. }
  54. // 处理 dictValue 层级
  55. dictDataMap[dictData.dictType].push({
  56. value: dictData.value,
  57. label: dictData.label,
  58. colorType: dictData.colorType,
  59. cssClass: dictData.cssClass
  60. })
  61. })
  62. this.dictMap = dictDataMap
  63. this.isSetDict = true
  64. wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期
  65. }
  66. },
  67. getDictByType(type: string) {
  68. if (!this.isSetDict) {
  69. this.setDictMap()
  70. }
  71. return this.dictMap[type]
  72. },
  73. async resetDict() {
  74. wsCache.delete(CACHE_KEY.DICT_CACHE)
  75. const res = await listSimpleDictData()
  76. // 设置数据
  77. const dictDataMap = new Map<string, any>()
  78. res.forEach((dictData: DictDataVO) => {
  79. // 获得 dictType 层级
  80. const enumValueObj = dictDataMap[dictData.dictType]
  81. if (!enumValueObj) {
  82. dictDataMap[dictData.dictType] = []
  83. }
  84. // 处理 dictValue 层级
  85. dictDataMap[dictData.dictType].push({
  86. value: dictData.value,
  87. label: dictData.label,
  88. colorType: dictData.colorType,
  89. cssClass: dictData.cssClass
  90. })
  91. })
  92. this.dictMap = dictDataMap
  93. this.isSetDict = true
  94. wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期
  95. }
  96. }
  97. })
  98. export const useDictStoreWithOut = () => {
  99. return useDictStore(store)
  100. }