优化字典数据使用store存取

master
RuoYi 2 years ago
parent 6c19bfbd90
commit cafff38a6e
  1. 1
      src/store/modules/app.js
  2. 57
      src/store/modules/dict.js
  3. 15
      src/utils/dict.js
  4. 4
      src/views/system/dict/data.vue
  5. 2
      src/views/system/dict/index.vue

@ -1,6 +1,5 @@
import Cookies from 'js-cookie' import Cookies from 'js-cookie'
const useAppStore = defineStore( const useAppStore = defineStore(
'app', 'app',
{ {

@ -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

@ -1,3 +1,4 @@
import useDictStore from '@/store/modules/dict'
import { getDicts } from '@/api/system/dict/data' import { getDicts } from '@/api/system/dict/data'
/** /**
@ -6,11 +7,17 @@ import { getDicts } from '@/api/system/dict/data'
export function useDict(...args) { export function useDict(...args) {
const res = ref({}); const res = ref({});
return (() => { return (() => {
args.forEach((d, index) => { args.forEach((dictType, index) => {
res.value[d] = []; res.value[dictType] = [];
getDicts(d).then(resp => { const dicts = useDictStore().getDict(dictType);
res.value[d] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass })) 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); return toRefs(res.value);
})() })()

@ -185,6 +185,7 @@
</template> </template>
<script setup name="Data"> <script setup name="Data">
import useDictStore from '@/store/modules/dict'
import { optionselect as getDictOptionselect, getType } from "@/api/system/dict/type"; import { optionselect as getDictOptionselect, getType } from "@/api/system/dict/type";
import { listData, getData, delData, addData, updateData } from "@/api/system/dict/data"; import { listData, getData, delData, addData, updateData } from "@/api/system/dict/data";
@ -319,12 +320,14 @@ function submitForm() {
if (valid) { if (valid) {
if (form.value.dictCode != undefined) { if (form.value.dictCode != undefined) {
updateData(form.value).then(response => { updateData(form.value).then(response => {
useDictStore().removeDict(queryParams.value.dictType);
proxy.$modal.msgSuccess("修改成功"); proxy.$modal.msgSuccess("修改成功");
open.value = false; open.value = false;
getList(); getList();
}); });
} else { } else {
addData(form.value).then(response => { addData(form.value).then(response => {
useDictStore().removeDict(queryParams.value.dictType);
proxy.$modal.msgSuccess("新增成功"); proxy.$modal.msgSuccess("新增成功");
open.value = false; open.value = false;
getList(); getList();
@ -341,6 +344,7 @@ function handleDelete(row) {
}).then(() => { }).then(() => {
getList(); getList();
proxy.$modal.msgSuccess("删除成功"); proxy.$modal.msgSuccess("删除成功");
useDictStore().removeDict(queryParams.value.dictType);
}).catch(() => {}); }).catch(() => {});
} }
/** 导出按钮操作 */ /** 导出按钮操作 */

@ -182,6 +182,7 @@
</template> </template>
<script setup name="Dict"> <script setup name="Dict">
import useDictStore from '@/store/modules/dict'
import { listType, getType, delType, addType, updateType, refreshCache } from "@/api/system/dict/type"; import { listType, getType, delType, addType, updateType, refreshCache } from "@/api/system/dict/type";
const { proxy } = getCurrentInstance(); const { proxy } = getCurrentInstance();
@ -313,6 +314,7 @@ function handleExport() {
function handleRefreshCache() { function handleRefreshCache() {
refreshCache().then(() => { refreshCache().then(() => {
proxy.$modal.msgSuccess("刷新成功"); proxy.$modal.msgSuccess("刷新成功");
useDictStore().cleanDict();
}); });
} }

Loading…
Cancel
Save