作者 RuoYi

优化字典数据使用store存取

1 import Vue from 'vue' 1 import Vue from 'vue'
  2 +import store from '@/store'
2 import DataDict from '@/utils/dict' 3 import DataDict from '@/utils/dict'
3 import { getDicts as getDicts } from '@/api/system/dict/data' 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 function install() { 21 function install() {
6 Vue.use(DataDict, { 22 Vue.use(DataDict, {
7 metas: { 23 metas: {
@@ -9,7 +25,19 @@ function install() { @@ -9,7 +25,19 @@ function install() {
9 labelField: 'dictLabel', 25 labelField: 'dictLabel',
10 valueField: 'dictValue', 26 valueField: 'dictValue',
11 request(dictMeta) { 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 },
@@ -2,6 +2,7 @@ const getters = { @@ -2,6 +2,7 @@ const getters = {
2 sidebar: state => state.app.sidebar, 2 sidebar: state => state.app.sidebar,
3 size: state => state.app.size, 3 size: state => state.app.size,
4 device: state => state.app.device, 4 device: state => state.app.device,
  5 + dict: state => state.dict.dict,
5 visitedViews: state => state.tagsView.visitedViews, 6 visitedViews: state => state.tagsView.visitedViews,
6 cachedViews: state => state.tagsView.cachedViews, 7 cachedViews: state => state.tagsView.cachedViews,
7 token: state => state.user.token, 8 token: state => state.user.token,
1 import Vue from 'vue' 1 import Vue from 'vue'
2 import Vuex from 'vuex' 2 import Vuex from 'vuex'
3 import app from './modules/app' 3 import app from './modules/app'
  4 +import dict from './modules/dict'
4 import user from './modules/user' 5 import user from './modules/user'
5 import tagsView from './modules/tagsView' 6 import tagsView from './modules/tagsView'
6 import permission from './modules/permission' 7 import permission from './modules/permission'
@@ -12,6 +13,7 @@ Vue.use(Vuex) @@ -12,6 +13,7 @@ Vue.use(Vuex)
12 const store = new Vuex.Store({ 13 const store = new Vuex.Store({
13 modules: { 14 modules: {
14 app, 15 app,
  16 + dict,
15 user, 17 user,
16 tagsView, 18 tagsView,
17 permission, 19 permission,
  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 +
@@ -364,12 +364,14 @@ export default { @@ -364,12 +364,14 @@ export default {
364 if (valid) { 364 if (valid) {
365 if (this.form.dictCode != undefined) { 365 if (this.form.dictCode != undefined) {
366 updateData(this.form).then(response => { 366 updateData(this.form).then(response => {
  367 + this.$store.dispatch('dict/removeDict', this.queryParams.dictType);
367 this.$modal.msgSuccess("修改成功"); 368 this.$modal.msgSuccess("修改成功");
368 this.open = false; 369 this.open = false;
369 this.getList(); 370 this.getList();
370 }); 371 });
371 } else { 372 } else {
372 addData(this.form).then(response => { 373 addData(this.form).then(response => {
  374 + this.$store.dispatch('dict/removeDict', this.queryParams.dictType);
373 this.$modal.msgSuccess("新增成功"); 375 this.$modal.msgSuccess("新增成功");
374 this.open = false; 376 this.open = false;
375 this.getList(); 377 this.getList();
@@ -386,6 +388,7 @@ export default { @@ -386,6 +388,7 @@ export default {
386 }).then(() => { 388 }).then(() => {
387 this.getList(); 389 this.getList();
388 this.$modal.msgSuccess("删除成功"); 390 this.$modal.msgSuccess("删除成功");
  391 + this.$store.dispatch('dict/removeDict', this.queryParams.dictType);
389 }).catch(() => {}); 392 }).catch(() => {});
390 }, 393 },
391 /** 导出按钮操作 */ 394 /** 导出按钮操作 */
@@ -339,6 +339,7 @@ export default { @@ -339,6 +339,7 @@ export default {
339 handleRefreshCache() { 339 handleRefreshCache() {
340 refreshCache().then(() => { 340 refreshCache().then(() => {
341 this.$modal.msgSuccess("刷新成功"); 341 this.$modal.msgSuccess("刷新成功");
  342 + this.$store.dispatch('dict/cleanDict');
342 }); 343 });
343 } 344 }
344 } 345 }