dictionary.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { dictionary, dictionaryDefine } from './dictionary-define'
  2. import { dictionaryOther, dictionaryOtherDefine } from './dictionary-other-define'
  3. import { defineStore } from 'pinia'
  4. import {dictGetAllSysDictsByValue} from "@/api/modules/manage/dict";
  5. const listToMap = (list) => {
  6. const map = new Map()
  7. list.forEach(v => {
  8. map.set(v.dictValue, v.dictLabel)
  9. })
  10. return map
  11. }
  12. export const useDictionaryStore = defineStore('dictionary', {
  13. state: () => ({
  14. ...dictionary,
  15. ...dictionaryOther,
  16. typeLevelList: [
  17. {dictLabel: '1级', dictValue: 1},
  18. {dictLabel: '2级', dictValue: 2},
  19. {dictLabel: '3级', dictValue: 3},
  20. {dictLabel: '4级', dictValue: 4},
  21. ]
  22. }),
  23. getters: {
  24. typeLevelMap() {
  25. return listToMap(this.typeLevelList)
  26. },
  27. },
  28. actions: {
  29. initDict(type) {
  30. return new Promise((resolve, reject) => {
  31. if (this[dictionaryDefine[type][0]].length === 0) {
  32. dictGetAllSysDictsByValue(`dictValue=${type}`).then((res: any) => {
  33. const data = res.data
  34. const map = new Map()
  35. const objMap = new Map()
  36. if (data.length > 0) {
  37. data.forEach((v: any) => {
  38. v.selectLabel = v.dictLabel
  39. v.selectValue = v.dictValue
  40. map.set(v.dictValue, v.dictLabel)
  41. objMap.set(v.dictValue, v)
  42. })
  43. }
  44. this[dictionaryDefine[type][0]] = data
  45. this[dictionaryDefine[type][1]] = map
  46. this[dictionaryDefine[type]?.[2]] = objMap
  47. resolve(res.data)
  48. }).catch((e: any) => {
  49. reject('获取'+dictionaryDefine[type][0]+'字典错误')
  50. })
  51. } else {
  52. resolve(this[dictionaryDefine[type][0]])
  53. }
  54. })
  55. },
  56. initOtherDict(type, data) {
  57. return new Promise((resolve, reject) => {
  58. if (this[dictionaryOtherDefine[type][0]].length === 0) {
  59. const map = new Map()
  60. const objMap = new Map()
  61. if (data.length > 0) {
  62. data.forEach((v: any) => {
  63. v.selectLabel = v.dictLabel
  64. v.selectValue = v.dictValue
  65. map.set(v.dictValue, v.dictLabel)
  66. objMap.set(v.dictValue, v)
  67. })
  68. }
  69. this[dictionaryOtherDefine[type][0]] = data
  70. this[dictionaryOtherDefine[type][1]] = map
  71. this[dictionaryOtherDefine[type]?.[2]] = objMap
  72. } else {
  73. resolve(this[dictionaryOtherDefine[type][0]])
  74. }
  75. })
  76. }
  77. },
  78. })