From cafff38a6e86e7f6db12d3b4405b0c0d52082e52 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Wed, 20 Jul 2022 19:38:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AD=97=E5=85=B8=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E4=BD=BF=E7=94=A8store=E5=AD=98=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/modules/app.js | 1 - src/store/modules/dict.js | 57 +++++++++++++++++++++++++++++++++ src/utils/dict.js | 17 +++++++--- src/views/system/dict/data.vue | 4 +++ src/views/system/dict/index.vue | 2 ++ 5 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 src/store/modules/dict.js diff --git a/src/store/modules/app.js b/src/store/modules/app.js index fc34125..456ea78 100644 --- a/src/store/modules/app.js +++ b/src/store/modules/app.js @@ -1,6 +1,5 @@ import Cookies from 'js-cookie' - const useAppStore = defineStore( 'app', { diff --git a/src/store/modules/dict.js b/src/store/modules/dict.js new file mode 100644 index 0000000..7e62a67 --- /dev/null +++ b/src/store/modules/dict.js @@ -0,0 +1,57 @@ +const useDictStore = defineStore( + 'dict', + { + state: () => ({ + dict: new Array() + }), + actions: { + // 获取字典 + getDict(_key) { + if (_key == null && _key == "") { + return null; + } + try { + for (let i = 0; i < this.dict.length; i++) { + if (this.dict[i].key == _key) { + return this.dict[i].value; + } + } + } catch (e) { + return null; + } + }, + // 设置字典 + setDict(_key, value) { + if (_key !== null && _key !== "") { + this.dict.push({ + key: _key, + value: value + }); + } + }, + // 删除字典 + removeDict(_key) { + var bln = false; + try { + for (let i = 0; i < this.dict.length; i++) { + if (this.dict[i].key == _key) { + this.dict.splice(i, 1); + return true; + } + } + } catch (e) { + bln = false; + } + return bln; + }, + // 清空字典 + cleanDict() { + this.dict = new Array(); + }, + // 初始字典 + initDict() { + } + } + }) + +export default useDictStore diff --git a/src/utils/dict.js b/src/utils/dict.js index 0f9144a..40d0691 100644 --- a/src/utils/dict.js +++ b/src/utils/dict.js @@ -1,3 +1,4 @@ +import useDictStore from '@/store/modules/dict' import { getDicts } from '@/api/system/dict/data' /** @@ -6,11 +7,17 @@ import { getDicts } from '@/api/system/dict/data' export function useDict(...args) { const res = ref({}); return (() => { - args.forEach((d, index) => { - res.value[d] = []; - getDicts(d).then(resp => { - res.value[d] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass })) - }) + args.forEach((dictType, index) => { + res.value[dictType] = []; + const dicts = useDictStore().getDict(dictType); + if (dicts) { + res.value[dictType] = dicts; + } else { + getDicts(dictType).then(resp => { + res.value[dictType] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass })) + useDictStore().setDict(dictType, res.value[dictType]); + }) + } }) return toRefs(res.value); })() diff --git a/src/views/system/dict/data.vue b/src/views/system/dict/data.vue index 37242f3..12c6492 100644 --- a/src/views/system/dict/data.vue +++ b/src/views/system/dict/data.vue @@ -185,6 +185,7 @@