index.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import Vue from 'vue'
  2. import store from '@/store'
  3. import DataDict from '@/utils/dict'
  4. import { getDicts as getDicts } from '@/api/system/dict/data'
  5. function searchDictByKey(dict, key) {
  6. if (key == null && key == "") {
  7. return null
  8. }
  9. try {
  10. for (let i = 0; i < dict.length; i++) {
  11. if (dict[i].key == key) {
  12. return dict[i].value
  13. }
  14. }
  15. } catch (e) {
  16. return null
  17. }
  18. }
  19. function install() {
  20. Vue.use(DataDict, {
  21. metas: {
  22. '*': {
  23. labelField: 'dictLabel',
  24. valueField: 'dictValue',
  25. request(dictMeta) {
  26. const storeDict = searchDictByKey(store.getters.dict, dictMeta.type)
  27. if (storeDict) {
  28. return new Promise(resolve => { resolve(storeDict) })
  29. } else {
  30. return new Promise((resolve, reject) => {
  31. getDicts(dictMeta.type).then(res => {
  32. store.dispatch('dict/setDict', { key: dictMeta.type, value: res.data })
  33. resolve(res.data)
  34. }).catch(error => {
  35. reject(error)
  36. })
  37. })
  38. }
  39. },
  40. },
  41. },
  42. })
  43. }
  44. export default {
  45. install,
  46. }