dict.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. export interface DictValueType {
  7. value: any
  8. label: string
  9. clorType?: string
  10. cssClass?: string
  11. }
  12. export interface DictTypeType {
  13. dictType: string
  14. dictValue: DictValueType[]
  15. }
  16. export interface DictState {
  17. dictMap: Map<string, any>
  18. }
  19. export const useDictStore = defineStore('dict', {
  20. state: (): DictState => ({
  21. dictMap: new Map<string, any>()
  22. }),
  23. getters: {
  24. getDictMap(): Recordable {
  25. const dictMap = wsCache.get(CACHE_KEY.DICT_CACHE)
  26. return dictMap ? dictMap : this.dictMap
  27. },
  28. getHasDictData(): boolean {
  29. if (this.dictMap.size > 0) {
  30. return true
  31. } else {
  32. return false
  33. }
  34. }
  35. },
  36. actions: {
  37. setDictMap(dictMap: Recordable) {
  38. // 设置数据
  39. const dictDataMap = new Map<string, any>()
  40. dictMap.forEach((dictData: DictDataVO) => {
  41. // 获得 dictType 层级
  42. const enumValueObj = dictDataMap[dictData.dictType]
  43. if (!enumValueObj) {
  44. dictDataMap[dictData.dictType] = []
  45. }
  46. // 处理 dictValue 层级
  47. dictDataMap[dictData.dictType].push({
  48. value: dictData.value,
  49. label: dictData.label,
  50. colorType: dictData.colorType,
  51. cssClass: dictData.cssClass
  52. })
  53. })
  54. this.dictMap = dictDataMap
  55. wsCache.set(CACHE_KEY.DICT_CACHE, dictDataMap, { exp: 60 }) // 60 秒 过期
  56. }
  57. }
  58. })
  59. export const useDictStoreWithOut = () => {
  60. return useDictStore(store)
  61. }