Просмотр исходного кода

优化字典数据使用store存取

RuoYi лет назад: 3
Родитель
Сommit
9f90b4eba7

+ 29 - 1
ruoyi-ui/src/components/DictData/index.js

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

+ 1 - 0
ruoyi-ui/src/store/getters.js

@@ -2,6 +2,7 @@ const getters = {
2 2
   sidebar: state => state.app.sidebar,
3 3
   size: state => state.app.size,
4 4
   device: state => state.app.device,
5
+  dict: state => state.dict.dict,
5 6
   visitedViews: state => state.tagsView.visitedViews,
6 7
   cachedViews: state => state.tagsView.cachedViews,
7 8
   token: state => state.user.token,

+ 2 - 0
ruoyi-ui/src/store/index.js

@@ -1,6 +1,7 @@
1 1
 import Vue from 'vue'
2 2
 import Vuex from 'vuex'
3 3
 import app from './modules/app'
4
+import dict from './modules/dict'
4 5
 import user from './modules/user'
5 6
 import tagsView from './modules/tagsView'
6 7
 import permission from './modules/permission'
@@ -12,6 +13,7 @@ Vue.use(Vuex)
12 13
 const store = new Vuex.Store({
13 14
   modules: {
14 15
     app,
16
+    dict,
15 17
     user,
16 18
     tagsView,
17 19
     permission,

+ 50 - 0
ruoyi-ui/src/store/modules/dict.js

@@ -0,0 +1,50 @@
1
+const state = {
2
+  dict: new Array()
3
+}
4
+const mutations = {
5
+  SET_DICT: (state, { key, value }) => {
6
+    if (key !== null && key !== "") {
7
+      state.dict.push({
8
+        key: key,
9
+        value: value
10
+      })
11
+    }
12
+  },
13
+  REMOVE_DICT: (state, key) => {
14
+    try {
15
+      for (let i = 0; i < state.dict.length; i++) {
16
+        if (state.dict[i].key == key) {
17
+          state.dict.splice(i, i)
18
+          return true
19
+        }
20
+      }
21
+    } catch (e) {
22
+    }
23
+  },
24
+  CLEAN_DICT: (state) => {
25
+    state.dict = new Array()
26
+  }
27
+}
28
+
29
+const actions = {
30
+  // 设置字典
31
+  setDict({ commit }, data) {
32
+    commit('SET_DICT', data)
33
+  },
34
+  // 删除字典
35
+  removeDict({ commit }, key) {
36
+    commit('REMOVE_DICT', key)
37
+  },
38
+  // 清空字典
39
+  cleanDict({ commit }) {
40
+    commit('CLEAN_DICT')
41
+  }
42
+}
43
+
44
+export default {
45
+  namespaced: true,
46
+  state,
47
+  mutations,
48
+  actions
49
+}
50
+

+ 3 - 0
ruoyi-ui/src/views/system/dict/data.vue

@@ -364,12 +364,14 @@ export default {
364 364
         if (valid) {
365 365
           if (this.form.dictCode != undefined) {
366 366
             updateData(this.form).then(response => {
367
+              this.$store.dispatch('dict/removeDict', this.queryParams.dictType);
367 368
               this.$modal.msgSuccess("修改成功");
368 369
               this.open = false;
369 370
               this.getList();
370 371
             });
371 372
           } else {
372 373
             addData(this.form).then(response => {
374
+              this.$store.dispatch('dict/removeDict', this.queryParams.dictType);
373 375
               this.$modal.msgSuccess("新增成功");
374 376
               this.open = false;
375 377
               this.getList();
@@ -386,6 +388,7 @@ export default {
386 388
       }).then(() => {
387 389
         this.getList();
388 390
         this.$modal.msgSuccess("删除成功");
391
+        this.$store.dispatch('dict/removeDict', this.queryParams.dictType);
389 392
       }).catch(() => {});
390 393
     },
391 394
     /** 导出按钮操作 */

+ 1 - 0
ruoyi-ui/src/views/system/dict/index.vue

@@ -339,6 +339,7 @@ export default {
339 339
     handleRefreshCache() {
340 340
       refreshCache().then(() => {
341 341
         this.$modal.msgSuccess("刷新成功");
342
+        this.$store.dispatch('dict/cleanDict');
342 343
       });
343 344
     }
344 345
   }